-
Notifications
You must be signed in to change notification settings - Fork 0
/
Edit Distance.cpp
58 lines (48 loc) · 1.23 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define vi vector<int>
#define vl vector<long long
#define pii pair<int, int>
#define pll pair<ll, ll>
const long long LLINF = LLONG_MAX;
const int INF = INT_MAX;
const int MOD = 1e9 + 7;
// Edit distance
int Lav_dis(string a, string b) {
int n = a.size(), m = b.size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for (int i = 0; i <= n; i++)
dp[i][0] = i;
for (int j = 0; j <= m; j++)
dp[0][j] = j;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i - 1] == b[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else {
dp[i][j] = min({
dp[i - 1][j] + 1, // Deletion
dp[i][j - 1] + 1, // Insertion
dp[i - 1][j - 1] + 1 // Substitution
});
}
}
}
return dp[n][m];
}
void solve() {
string a, b;
cin >> a >> b;
cout << Lav_dis(a, b) << endl;
}
int main() {
fastio;
int tc = 1;
// cin >> tc;
while (tc--) {
solve();
}
return 0;
}