-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevenshteinDistance.cpp
More file actions
32 lines (28 loc) · 1.02 KB
/
LevenshteinDistance.cpp
File metadata and controls
32 lines (28 loc) · 1.02 KB
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
31
32
#include <vector>
#include <iostream>
#include <string>
using namespace std;
/* ldistance of string x(i),y(j)
/ max(i,j) if min(i,j) = 0
ldiatance(i,j) = | / ldistance(i-1, j) + 1 (delete at the end of x or pend at the end of y)
| min | ldistance(i-1, j-1) + 1(x(i)!=y(j)) (substitute one of the x or y) else
\ \ ldistance(i, j-1) + 1 (delete at the end of y or pend at the end of x)
*/
int ldistance(const string& s1, const string& s2){
vector<vector<int>> dp(s1.length()+1, vector<int>(s2.length()+1, 0));
for (int i = 0; i <= s1.length(); i++){
for (int j = 0; j <= s2.length(); j++){
if(min(i, j) == 0) dp[i][j] = max(j,j);
else {
int ind = s1[i-1] == s2[j-1] ? 0 : 1;
dp[i][j] = min(dp[i-1][j] + 1, min(dp[i][j-1] + 1, dp[i-1][j-1] + ind));
}
}
}
return dp[s1.length()][s2.length()];
}
int main(){
string s1, s2;
cin >> s1 >> s2;
cout << ldistance(s1, s2) << endl;
}