标签:style blog class c code java
概念
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 |
package
com.mzule.al; public class LevenshteinDistance { public
double distance(String w1,String w2){ double [][] m = new
double [w1.length()+ 1 ][w2.length()+ 1 ]; for ( int
i= 0 ;i<m.length;i++){ m[i][ 0 ]=i; } for ( int
i= 0 ;i<m[ 0 ].length;i++){ m[ 0 ][i]=i; } for ( int
i= 1 ;i<m.length;i++){ for ( int
j= 1 ;j<m[ 0 ].length;j++){ m[i][j] = min(m[i][j- 1 ]+ 1 ,m[i- 1 ][j]+ 1 ,m[i- 1 ][j- 1 ]+cost(w1.charAt(i- 1 ),w2.charAt(j- 1 ))); } } return
m[w1.length()][w2.length()]; } protected
double cost( char
c1, char c2) { return
c1==c2? 0 : 1 ; } protected
double min( double
i, double
j, double
k) { double
t = i<j?i:j; return
t<k?t:k; } } |
测试代码:
1
2
3
4 |
public static void main(String[] args) { double
d = new LevenshteinDistance().distance( "Lavensting" , "Levenshtein" ); System.out.println(d); } |
标签:style blog class c code java
原文地址:http://www.cnblogs.com/tibetanmastiff/p/3738489.html