HDU 1159 Common Subsequence
思路参考这里
#include<iostream> #include<string> #include<cstring> using namespace std; #define Size 1000 int table[Size+1][Size+1]; int main() { string A, B; while( cin>>A>>B ) { memset( table, 0, sizeof(table) ); int LenA = A.length(); int LenB = B.length(); for( int i=1; i<=LenA; i++ ) { for( int j=1; j<=LenB; j++ ) { if( A[i-1]==B[j-1] ) table[i][j]=table[i-1][j-1]+1; else table[i][j]= table[i-1][j]>table[i][j-1] ? table[i-1][j] : table[i][j-1]; } } cout<<table[LenA][LenB]<<endl; } return 0; }