Common Subsequence
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<string> 5 #include<cstring> 6 #include<algorithm> 7 using namespace std; 8 int a[500][500]; 9 int main() 10 { 11 string s1,s2; 12 while(cin>>s1>>s2) 13 { 14 int k1=s1.length(); 15 int k2=s2.length(); 16 memset(a,0,sizeof(a)); 17 for(int i=1; i<=k1; i++) 18 { 19 for(int j=1; j<=k2; j++) 20 { 21 if(s1[i-1]==s2[j-1]) 22 { 23 a[i%2][j]=a[(i-1)%2][j-1]+1; 24 } 25 else 26 { 27 a[i%2][j]=max(a[(i-1)%2][j],a[i%2][j-1]); 28 } 29 } 30 } 31 printf("%d\n",a[k1%2][k2]); 32 } 33 return 0; 34 }