链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=36
经典dp题:
#include <iostream> #include<cstring> using namespace std; int dp[1005][1005]; char a[1005]; char b[1005]; int main() { int n; int i,j; int la,lb; cin>>n; while(n--) { memset(dp,0,sizeof(dp)); cin>>a>>b; la=strlen(a); lb=strlen(b); for(i=0; i<la; i++) for(j=0; j<lb; j++) { if(a[i]==b[j]) dp[i+1][j+1]=dp[i][j]+1; else dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]); } cout<<dp[la][lb]<<endl; } return 0; }