-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathedit_distance.cpp
36 lines (32 loc) · 913 Bytes
/
edit_distance.cpp
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
33
34
35
36
// added
#include <bits/stdc++.h>
using namespace std;
int edit_distance(const string &s, const string &t) {
vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1));
for (int i = 0; i <= s.size(); i++) {
for (int j = 0; j <= t.size(); j++) {
if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else {
dp[i][j] = INT_MAX;
}
}
}
for (int i = 1; i <= s.size(); i++) {
for (int j = 1; j <= t.size(); j++) {
if (s[i - 1] == t[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}) + 1;
}
}
}
return dp[s.size()][t.size()];
}
int main() {
string s, t;
cin >> s >> t;
cout << edit_distance(s, t) << endl;
}