-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_distance_rec.cpp
More file actions
33 lines (29 loc) · 828 Bytes
/
edit_distance_rec.cpp
File metadata and controls
33 lines (29 loc) · 828 Bytes
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
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> dp; // 1 indexed
int edit_dist(string &a, string &b, int i, int j)
{
if (!i) return j;
if (!j) return i;
if (dp[i][j] != -1)
return dp[i][j];
if (a[i - 1] == b[j - 1])
return dp[i][j] = edit_dist(a, b, i - 1, j - 1);
else return dp[i][j] = 1 + min({
edit_dist(a, b, i, j - 1), // inserting at b[j-1] after i-1 in a
edit_dist(a, b, i - 1, j), // deleting a[i-1]
edit_dist(a, b, i - 1, j - 1) // replacing a[i-1] by b[j-1]
});
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
string w1, w2;
cin >> w1 >> w2;
int n = w1.size();
int m = w2.size();
dp.resize(n + 1, vector<int>(m + 1, -1));
int minCost = edit_dist(w1, w2, n, m);
cout << minCost << endl;
}