-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathMinEdit.cpp
More file actions
35 lines (29 loc) · 808 Bytes
/
MinEdit.cpp
File metadata and controls
35 lines (29 loc) · 808 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
#include<bits/stdc++.h>
using namespace std;
int solve(string& s1, string& s2, int n, int m){
int DP[n + 1][m + 1];
for(int i = 0; i <= n; i++){
for(int j = 0; j <= m; j++){
if(i == 0)
DP[i][j] = j;
else if(j == 0)
DP[i][j] = i;
else if(s1[i - 1] == s2[j - 1])
DP[i][j] = DP[i - 1][j - 1];
else
DP[i][j] = 1 + min(DP[i - 1][j - 1], min(DP[i][j - 1], DP[i - 1][j]));
}
}
return DP[n][m];
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t; cin >> t; while(t--){
int n, m; cin >> n >> m;
string s1, s2;
cin >> s1 >> s2;
cout << solve(s1, s2, n, m) << "\n";
}
return 0;
}