添加链接描述
想法:
如果s1[i]==s2[j] 则 c[i][j]=c[i-1][j-1]+1
如果s1[i]!=s2[j] 则 c[i][j]=max(c[i-1][j],c[i][j-1])
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int dp[10010][10010];
string s1,s2;
int len1,len2;
int main()
{
while(cin>>s1>>s2)
{
len1=s1.length();
len2=s2.length();
for(int i=0;i<=len1;i++)
{
for(int j=0;j<=len2;j++)
{
if(i==0||j==0)
{
dp[i][j]=0;
continue;
}
if(s1[i-1]==s2[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
cout << dp[len1][len2] << endl;
}
return 0;
}