-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_distance.cpp
More file actions
44 lines (40 loc) · 793 Bytes
/
edit_distance.cpp
File metadata and controls
44 lines (40 loc) · 793 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
/*
https://practice.geeksforgeeks.org/problems/edit-distance/0
*/
#include<bits/stdc++.h>
using namespace std;
int n, m;
string s1, s2;
void solve()
{
int dp[n+5][m+5];
int i, j;
for(i = 0 ; i <= n ; i++)
dp[i][0] = i;
for(j = 0 ; j <= m ; j++)
dp[0][j] = j;
for(i = 1 ; i <= n ; i++)
{
for(j = 1 ; j <= m ; j++)
{
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1;
if(s1[i-1] == s2[j-1])
dp[i][j] = min(dp[i][j], dp[i-1][j-1]);
else
dp[i][j] = min(dp[i][j], dp[i-1][j-1] + 1);
}
}
cout << dp[n][m] << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
{
cin >> n >> m;
cin >> s1 >> s2;
solve();
}
return 0;
}