forked from Ouditchya/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEDIST.cpp
More file actions
52 lines (37 loc) · 986 Bytes
/
EDIST.cpp
File metadata and controls
52 lines (37 loc) · 986 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
47
48
49
50
51
52
// AC , ALGO : Dynamic Programming, Edit distance.
/* Some Helpful Links :
http://en.wikipedia.org/wiki/Edit_distance
http://www.geeksforgeeks.org/dynamic-programming-set-5-edit-distance/
*/
// For any clarifications, contact me at : osinha6792@gmail.com
#include<cstdio>
#include<cstring>
using namespace std ;
inline int min( int a , int b , int c )
{
return ( c > ( ( a > b ) ? b : a ) ) ? ( ( a > b ) ? b : a ) : c ;
}
int a[2101][2101] ;
int main( )
{
int t , i , j , n , m ;
char x[2101] , y[2101] ;
for( scanf("%d",&t) ; t ; t-- )
{
scanf("%s %s",x,y) ;
n = strlen( x ) ;
m = strlen( y ) ;
a[0][0] = 0 ;
for( i = 1 ; i <= n ; i++ )
a[i][0] = a[i-1][0] + 1 ;
for( j = 1 ; j <= m ; j++ )
a[0][j] = a[0][j-1] + 1 ;
for( i = 1 ; i <= n ; i++ )
{
for( j = 1 ; j <= m ; j++ )
a[i][j] = min( a[i-1][j] + 1 , a[i][j-1] + 1 , a[i-1][j-1] + ( ( x[i-1] == y[j-1] ) ? 0 : 1 ) ) ;
}
printf("%d\n",a[n][m]) ;
}
return 0 ;
}