博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Edit Distance (编辑距离) .NET 实现
阅读量:7090 次
发布时间:2019-06-28

本文共 1598 字,大约阅读时间需要 5 分钟。

///     /// Calculate Text Edit Distance Utility Class    ///     public static class TextEditDistanceUtility    {        ///         /// get edit distance between two string        ///         ///         ///         /// 
public static int GetEditDistance(string str1, string str2) { if (str1 == str2) return 0; else if (String.IsNullOrEmpty(str1) && String.IsNullOrEmpty(str2)) return 0; else if (String.IsNullOrEmpty(str1) && !String.IsNullOrEmpty(str2)) return str2.Length; else if (!String.IsNullOrEmpty(str1) && String.IsNullOrEmpty(str2)) return str1.Length; int[,] d = new int[str1.Length + 1, str2.Length + 1]; d.Initialize(); int cost = 0; for (int i = 0; i < d.GetLength(0); i++) { d[i, 0] = i; } for (int j = 0; j < d.GetLength(1); j++) { d[0, j] = j; } for (int i = 0; i < str1.Length; i++) { for (int j = 0; j < str2.Length; j++) { if (str1[i] == str2[j]) cost = 0; else cost = 1; d[i + 1, j + 1] = Math.Min(Math.Min(d[i, j + 1] + 1, d[i + 1, j] + 1), Math.Min(d[i + 1, j] + 1, d[i, j] + cost)); } } return d[str1.Length, str2.Length]; } }

Edit Distance是比较两个字符串之间需要多少次基础操作才能变成对方的操作,增加一个字符,删除一个字符,修改一个字符,均算作一次操作

比如abb和bbb,编辑距离就是1,abc变成aa,编辑距离就是2

这个算法比较多的应用就是比较两次结果之间差别有多大

转载于:https://www.cnblogs.com/vanpan/archive/2012/11/14/3583036.html

你可能感兴趣的文章
Deduplication去重算法基础之可变长度数据分片
查看>>
Tomcat 不同端口配置两个应用程序
查看>>
Dubbo学习(一)
查看>>
SASS界面编译工具——Koala的使用
查看>>
JSP放入Jar包支持
查看>>
Mysql日期和时间函数总结
查看>>
Servlet容器启动过程
查看>>
CentOS安装配置nagios(1)
查看>>
RedHat 6.4 搭建rhcs集群
查看>>
我的友情链接
查看>>
Exchange Server 2010的俩种版本比较
查看>>
asp.net 插入视频
查看>>
11、网络--Linux Bridge(网桥基础)
查看>>
参观迅达云成观后感
查看>>
linux(ubuntu)查看硬件设备命令
查看>>
一八年第三天晚上十点半的thinking
查看>>
keepalived 组播的配置
查看>>
《深入PHP:面向对象、模式与实践》(一)
查看>>
工控系统安全问题汇总(一)
查看>>
yii2.0-rules验证规则应用实例
查看>>