C#的实现方法,同样是调用第一个函数
public double Levenshtein_Distance(string str1, string str2)
{
long d = 0L;
if (str1.Length > str2.Length)
{
d = str1.Length;
}
else
{
d = str2.Length;
}
return (1.0 - (((double) this.LD(str1, str2)) / ((double) d)));
}
public int LD(string s, string t)
{
int i;
int n = Strings.Len(s);
int m = Strings.Len(t);
if (n == 0)
{
return m;
}
if (m == 0)
{
return n;
}
int[,] dis = new int[n + 1, m + 1];
int VB$t_i4$L0 = n;
for (i = 0; i <= VB$t_i4$L0; i++)
{
dis[i, 0] = i;
}
int VB$t_i4$L1 = m;
int j = 0;
while (j <= VB$t_i4$L1)
{
dis[0, j] = j;
j++;
}
int VB$t_i4$L2 = n;
for (i = 1; i <= VB$t_i4$L2; i++)
{
string s_i = Strings.Mid(s, i, 1);
int VB$t_i4$L3 = m;
for (j = 1; j <= VB$t_i4$L3; j++)
{
int cost;
string t_j = Strings.Mid(t, j, 1);
if (s_i == t_j)
{
cost = 0;
}
else
{
cost = 1;
}
dis[i, j] = this.Minimum(dis[i - 1, j] + 1, dis[i, j - 1] + 1, dis[i - 1, j - 1] + cost);
}
}
int LD = dis[n, m];
dis = null;
return LD;
}
private int Minimum(int a, int b, int c)
{
int min = a;
if (b < min)
{
min = b;
}
if (c < min)
{
min = c;
}
return min;
}