Реализации алгоритмов/Расстояние Левенштейна: различия между версиями

Содержимое удалено Содержимое добавлено
Строка 219:
<source lang="Java">
 
public static int editdistlevenstain(@Nonnull String S1str1, @Nonnull String S2str2) {
int m = S1.length(), n int[] D1 = S2new int[str2.length() + 1];
int[] D2 = new int[nstr2.length() + 1];
int[] D1;
int[] D2 = new int[n + 1];
 
for (int ij = 0; ij <= nstr2.length(); i j++) {
D2[ij] = ij;
}
 
for (int i = 1; i <= mstr1.length(); i ++) {
System.arraycopy(D2, 0, D1, 0, D1.length);
D1 = D2;
D2[0] = i;
D2 = new int[n + 1];
for(int j = 0; j <= n; j ++) {
if(j == 0) D2[j] = i;
else {
int cost = (S1.charAt(i - 1) != S2.charAt(j - 1)) ? 1 : 0;
if(D2[j - 1] < D1[j] && D2[j - 1] < D1[j - 1] + cost)
D2[j] = D2[j - 1] + 1;
else if(D1[j] < D1[j - 1] + cost)
D2[j] = D1[j] + 1;
else
D2[j] = D1[j - 1] + cost;
}
}
}
return D2[n];
}
 
for (int j = 01; j <= nstr2.length(); j ++) {
</source>
int cost = (S1str1.charAt(i - 1) != S2str2.charAt(j - 1)) ? 1 : 0;
D2[j] = min(
D1[j] + 1,
D2[j - 1] + 1,
D1[j - 1] + cost
);
}
}
 
return D2[D2.length - 1];
}
 
private static int min(int n1, int n2, int n3) {
return Math.min(Math.min(n1, n2), n3);
}
 
</source>
 
== [[w:Scala|Scala]] ==