栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何计算给定2个字符串的距离相似性度量?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何计算给定2个字符串的距离相似性度量?

您要查找的内容称为 编辑距离
Levenshtein距离

。维基百科文章解释了它是如何计算的,并且在底部有一段不错的伪代码,可帮助您非常轻松地用C#编写此算法。

这是第一个链接在下面的站点的实现:

private static int  CalcLevenshteinDistance(string a, string b)    {    if (String.IsNullOrEmpty(a) && String.IsNullOrEmpty(b)) {        return 0;    }    if (String.IsNullOrEmpty(a)) {        return b.Length;    }    if (String.IsNullOrEmpty(b)) {        return a.Length;    }    int  lengthA   = a.Length;    int  lengthB   = b.Length;    var  distances = new int[lengthA + 1, lengthB + 1];    for (int i = 0;  i <= lengthA;  distances[i, 0] = i++);    for (int j = 0;  j <= lengthB;  distances[0, j] = j++);    for (int i = 1;  i <= lengthA;  i++)        for (int j = 1;  j <= lengthB;  j++) { int  cost = b[j - 1] == a[i - 1] ? 0 : 1; distances[i, j] = Math.Min     (     Math.Min(distances[i - 1, j] + 1, distances[i, j - 1] + 1),     distances[i - 1, j - 1] + cost     ); }    return distances[lengthA, lengthB];    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/576163.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号