Description
Input
Output
Sample Input
abcfbc abfcab programming contest abcd mnp
Sample Output
420
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
int const maxn=1000;
char s[maxn],t[maxn];
int dp[maxn+1][maxn+1];
void solve()
{
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(s[i]==t[j])
{
dp[i+1][j+1]=dp[i][j]+1;
}
else
{
dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
}
}
}
printf("%d\n",dp[n][m]);
}
int main()
{
while(scanf("%s%s",&s,&t)!=EOF)
{
n=strlen(s);
m=strlen(t);
solve();
}
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/a1967919189/article/details/47666187