-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDIST.cpp
37 lines (37 loc) · 833 Bytes
/
EDIST.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
#include<bits/stdc++.h>
using namespace std;
void solve(string str1, string str2, int N, int M) {
vector<vector<int>> dp(N + 1, vector<int>(M + 1, INT_MAX));
for (int i = 1; i <= N; i++) {
dp[i][0] = i;
}
for (int i = 1; i <= M; i++) {
dp[0][i] = i;
}
dp[0][0] = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1;
if (str1[i - 1] == str2[j - 1]) {
dp[i][j] = min(dp[i - 1][j - 1], dp[i][j]);
} else {
dp[i][j] = min(dp[i - 1][j - 1] + 1, dp[i][j]);
}
}
}
cout << dp[N][M] << endl;
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int T;
cin >> T;
while (T--) {
string str1, str2;
cin >> str1 >> str2;
int N = str1.size();
int M = str2.size();
solve(str1, str2, N, M);
}
return 0;
}