參考代碼:直接用以前忘了是寫的還是拷的C++代碼
#include<iostream>
#include<string>
#include<algorithm>
#include<memory.h>
using namespace std;
int main(){
string X,Y;
int dp[110],x,y,temp1,temp2;
while(cin>>X>>Y){
memset(dp,0,sizeof(dp));
for(x=1;x<=X.length();++x){
temp1=temp2=0;
for(y=1;y<=Y.length();y++){
temp2=dp[y];
if(X[x-1]==Y[y-1]) dp[y]=temp1+1;
else dp[y]=max(dp[y],dp[y-1]);
temp1=temp2;
}
}
cout<<dp[Y.length()]<<endl;
}
}