-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_distance.cpp
More file actions
39 lines (34 loc) · 954 Bytes
/
Copy pathedit_distance.cpp
File metadata and controls
39 lines (34 loc) · 954 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
34
35
36
37
38
39
#include <bits/stdc++.h>
using namespace std;
void solve(string& str1, string& str2) {
int n = str1.size();
int m = str2.size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= m; ++j) {
if (i == 0 && j == 0) {
dp[i][j] = 0;
} else if (i == 0) {
dp[i][j] = j;
} else if (j == 0) {
dp[i][j] = i;
} else if (str1[i - 1] == str2[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;
}
}
}
cout << dp[n][m] << endl;
}
int main() {
/* Faster Input/Output */
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
/* Input */
string str1, str2;
cin >> str1 >> str2;
solve(str1, str2);
return 0;
}