题解:裸的LCS
/*LCS*/ #include <iostream> #include <string> using namespace std; int f[1000][1000]={0}; int main() { string a,b; while(cin>>a>>b) { int m,n; n=a.length(); m=b.length(); memset(f,0,sizeof(f)); for (int i=0; i<n; i++) for (int j=0; j<m; j++) { if (a[i]==b[j]) f[i+1][j+1]=f[i][j]+1; else (f[i][j+1]>f[i+1][j])?f[i+1][j+1]=f[i][j+1]:f[i+1][j+1]=f[i+1][j]; } printf("%d\n",f[n][m]); } return 0; }