直接模板!
View Code
#include " iostream " using namespace std; char a[ 1001 ],b[ 1001 ]; int c[ 1001 ][ 1001 ]; int Max( int a, int b){ return a > b ? a:b;} void LCS( char * aa , char * bb, int x, int y){ int i,j; for (i = 0 ;i <= x;i ++ ) c[i][ 0 ] = 0 ; for (j = 0 ;j <= y;j ++ ) c[ 0 ][j] = 0 ; for (i = 1 ;i <= x;i ++ ) { for (j = 1 ;j <= y;j ++ ) { if (aa[i - 1 ] == bb[j - 1 ]) {c[i][j] = c[i - 1 ][j - 1 ] + 1 ;} else c[i][j] = Max(c[i - 1 ][j],c[i][j - 1 ]); } }} int main(){ while (cin >> a >> b) { int La = strlen(a); int Lb = strlen(b); LCS(a,b,La,Lb); cout << c[La][Lb] << endl; } return 0 ;}