-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdit Distance.cpp
More file actions
46 lines (34 loc) · 876 Bytes
/
Copy pathEdit Distance.cpp
File metadata and controls
46 lines (34 loc) · 876 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
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
int dp[5005][5005];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s1, s2;
char c1, c2;
cin >> s1 >> s2;
int a = s1.size();
int b = s2.size();
for(int i = 0; i <= a; i ++){
for(int j = 0; j <= b; j ++){
if(i == 0){
dp[i][j] = j;
continue;
}
else if(j == 0){
dp[i][j] = i;
continue;
}
c1 = c2 = '#';
if(i > 0) c1 = s1[i - 1];
if(j > 0) c2 = s2[j - 1];
if(c1 == c2 && c1 != '#'){
dp[i][j] = dp[i - 1][j - 1];
}
else dp[i][j] = 1 + min({dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]});
}
}
cout << dp[a][b];
return 0;
}