此题经典线性动态规划。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int d[maxn][maxn];
int main(void){
string a,b;
while(cin>>a>>b){
memset(d,,sizeof(d));
for(int i=;i<=a.length();i++){
for(int j=;j<=b.length();j++){
if(a[i-]==b[j-]){
d[i][j]=d[i-][j-]+;
}
else{
d[i][j]=max(d[i][j-],d[i-][j]);
}
}
}
cout<<d[a.length()][b.length()]<<endl;
}
return ;
}