-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMC.cpp
47 lines (36 loc) · 854 Bytes
/
MC.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
// AC , ALGO : Dynamic Programming - Length of LCS.
/*
Helpful Link : http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
*/
// For any clarifications, contact me at : [email protected]
#include<cstdio>
#include<cstring>
using namespace std ;
int main( )
{
char x[2000] , y[2000] ;
int i , j , m , n , lcs_len ;
while( scanf("%s",x) , strcmp( x , "#" ) )
{
scanf("%s",y) ;
m = strlen( x ) ;
n = strlen( y ) ;
int c[m+1][n+1] ;
memset( c , 0 , sizeof( c ) ) ;
for( i = 1 ; i <= m ; i++ )
{
for( j = 1 ; j <= n ; j++ )
{
if( x[i-1] == y[j-1] )
c[i][j] = c[i-1][j-1] + 1 ;
else if( c[i-1][j] >= c[i][j-1] )
c[i][j] = c[i-1][j] ;
else
c[i][j] = c[i][j-1] ;
}
}
lcs_len = c[m][n] ;
printf("%d\n",15 * ( m - lcs_len ) + 30 * ( n - lcs_len ) ) ;
}
return 0 ;
}