-
Notifications
You must be signed in to change notification settings - Fork 13
/
edit distance.cpp
56 lines (39 loc) · 1.01 KB
/
edit distance.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int min(int x,int y,int z)
{
int k=(x>y)?y:x;
k=(k>z)?z:k;
return k;
}
int diff(char p,char q)
{
if(p==q)
return 0;
return 1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
string s1,s2;
cin>>s1>>s2;
int m=s1.length();
int n=s2.length();
short e[m+1][n+1];
for(int i=0;i<=m;i++)
e[i][0]=i;
for(int j=1;j<=n;j++)
e[0][j]=j;
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
e[i][j]=min(e[i-1][j]+1,e[i][j-1]+1,e[i-1][j-1]+diff(s1[i-1],s2[j-1]));
printf("%d\n",e[m][n]);
}
system ("pause");
return 0;
}