题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1346
这题纯水题,写他的原因是因为被坑死了。
字符串里面有空格,所以用scanf是不可行的,用gets可以搞定。
下面上代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL int
const int maxn = 1010;
char a[maxn];
char b[maxn];
LL dp[maxn][maxn];
void havefun(LL len1,LL len2)
{
for(LL i=0;i<=len1;i++)
dp[i][0] = 0;
for(LL i=0;i<=len2;i++)
dp[0][i] = 0;
for(LL i=1;i<=len1;i++)
{
for(LL j=1;j<=len2;j++)
{
dp[i][j] = max(dp[i][j-1],dp[i-1][j]);
if(a[i-1]==b[j-1])
{
dp[i][j] = max(dp[i-1][j-1]+1,dp[i][j]);
}
}
}
}
int main()
{
while(gets(a) && gets(b))
{
LL len1 = strlen(a);
LL len2 = strlen(b);
havefun(len1,len2);
printf("%d\n",dp[len1][len2]);
}
return 0;
}