Levenshtein's eidit distance
来源:互联网 发布:手机上淘宝电脑版登录 编辑:程序博客网 时间:2024/05/18 02:13
From Wiki, the definition of Levenshtein distance is astring metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is equal to the number of single-character edits required to change one word into the other.
Mathematically, the Levenshtein distance between two strings is given by where
Note that the first element in the minimum corresponds to deletion(from to), the second to insertion and the third to match or mismatch, depending on whether the respective symbols are the same.
Below is an implementation in java.
package test.LevenshteinDistance;public class LevenshteinDistance {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString s = "kitten";String t = "sitting";LevenshteinDistance ld = new LevenshteinDistance();//int recurlen = ld.recurLevenDistance(s, t);int dynlen = ld.dynLevenDistance(s, t);//System.out.println("The length is : " + recurlen);System.out.println("The length is : " + dynlen);System.out.println(s.length());}/*public int minimum(int a, int b, int c){return Math.min(Math.min(a, b), c);}*/public int minimum(int a, int b, int c){if(a < b && a < c) return a;if(b < a && b < c) return b;return c;}// compute the distance use recursive waypublic int recurLevenDistance(String s, String t){int slen = s.length();int tlen = t.length();int ins = 0; // for recording insert lengthint del = 0; // for recording delete lengthint sub = 0; // for substitution lengthif(slen == 0 && tlen == 0) return 0;if(slen == 0) return tlen;if(tlen == 0) return slen;/*if(s.charAt(slen - 1) == t.charAt(tlen - 1)){sub = recurLevenDistance(s.substring(0, slen - 1), t.substring(0, tlen - 1));}else{sub = recurLevenDistance(s.substring(0, slen - 1), t.substring(0, tlen - 1)) + 1;}*/sub = recurLevenDistance(s.substring(0, slen - 1), t.substring(0, tlen - 1)) + ((s.charAt(slen - 1) == t.charAt(tlen - 1)) ? 0 : 1);ins = recurLevenDistance(s.substring(0, slen - 1), t) + 1;del = recurLevenDistance(s, t.substring(0, tlen - 1)) + 1;return minimum(ins, del, sub);}// compute the distance use dynamic waypublic int dynLevenDistance(String s, String t){int[][] distance = new int[s.length() + 1][t.length() + 1];int i, j;for(i = 0; i <= s.length(); i++)distance[i][0] = i;for(j = 0; j <= t.length(); j++)distance[0][j] = j;for(i = 1; i <= s.length(); i++){for(j = 1; j <= t.length(); j++){distance[i][j] = minimum(distance[i - 1][j] + 1,distance[i][j - 1] + 1,distance[i - 1][j - 1] + ((s.charAt(i - 1) == t.charAt(j - 1)) ? 0 : 1));}}return distance[s.length()][t.length()];}}
- Levenshtein's eidit distance
- Levenshtein Distance
- Levenshtein Distance
- Levenshtein distance
- Levenshtein distance
- Levenshtein distance
- levenshtein distance
- Levenshtein distance
- Edit Distance (Levenshtein Distance)
- Levenshtein distance(Edit distance)
- Levenshtein Distance 算法
- Levenshtein Distance 算法
- Levenshtein distance (相似度)
- Levenshtein Edit distance
- Levenshtein距离,Edit Distance
- Levenshtein Distance, in Three Flavors
- 编辑距离(Levenshtein Distance)
- 编辑距离算法 Levenshtein Distance
- 编程之美2.1扩展问题2
- Linux进程通信
- 关闭WIN7下的IPV6
- ANDROID 静音与振动
- windows程序设计(20):MFC绘图
- Levenshtein's eidit distance
- 一个前百度工程师看百度的狼性【2012-11-11】
- HTML css 2 css
- 使用php-excel-reader读取excel文件
- Xcode 4.1/4.2/4.3 免证书(iDP)开发+真机调试+生成IPA全攻略
- 基于MINA构建简单高性能的NIO应用-优化指南
- java swing 实现贪吃蛇小游戏
- 关于J2EE项目中三层架构如何在开发中得到正确的实施
- ruby 使用mysql API