1 #include<cstdio> 2 #include<cstring> 3 4 int a[510][510]; 5 char m[510],z[510]; 6 7 int main() 8 { 9 int lm,lz,im,iz,i,t; 10 while(gets(m+1) != NULL) 11 { 12 gets(z+1); 13 lm = strlen(m+1); 14 lz = strlen(z+1); 15 lm++; 16 lz++; 17 if(lm > lz) t = lm; 18 else t = lz; 19 for(i = 0;i < t; i++) 20 a[i][0] = a[0][i] = 0; 21 for(iz = 1;iz < lz; iz++) 22 for(im = 1;im < lm; im++) 23 { 24 if(m[im] == z[iz]) 25 { 26 a[im][iz] = a[im-1][iz-1] +1; 27 } 28 else 29 { 30 if(a[im][iz-1] > a[im-1][iz]) 31 a[im][iz] = a[im][iz-1]; 32 else a[im][iz] = a[im-1][iz]; 33 } 34 } 35 printf("%d\n",a[lm-1][lz-1]); 36 } 37 return 0; 38 }
这篇文章讲的很详细