Created
December 19, 2010 20:49
-
-
Save raliste/747675 to your computer and use it in GitHub Desktop.
Levenshtein distance (substitution and insertion)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int cambios(string s1, string s2) { | |
int l1 = s1.size(); | |
int l2 = s2.size(); | |
int *A = new int[l2]; | |
int i, j; | |
for(i=0; i<=l2; i++) { | |
A[i] = i; | |
} | |
for(i=0; i<l1; i++) { | |
A[0] = i+1; | |
int x = i; | |
for(j=0; j<l2; j++) { | |
int y = A[j+1]; | |
if(s1[i] == s2[j]) | |
A[j+1] = x; | |
else { | |
int n; | |
if(y < x) | |
n = y; | |
else | |
n = x; | |
if(A[j] < n) | |
A[j+1] = A[j]; | |
else | |
A[j+1] = n + 1; | |
} | |
x = y; | |
} | |
} | |
return A[l2]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment