Description
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Input
Output
Sample Input
abcfbc abfcab programming contest abcd mnp
Sample Output
4 2 0
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
char a[2010];
char b[2010];
int dp[2010][2010];
int main(){
while(scanf("%s%s",&a,&b)!=EOF){
int a1=strlen(a);
int b1=strlen(b);
memset(dp,0,sizeof(dp));
for(int i=1;i<a1+1;i++)
{
for(int j=1;j<b1+1;j++)
{
if(a[i-1]==b[j-1])
{
dp[i][j]=dp[i-1][j-1]+1;
}
else
{
if(dp[i-1][j]>dp[i][j-1])
{
dp[i][j]=dp[i-1][j];
}
else
{
dp[i][j]=dp[i][j-1];
}
}
}
}
printf("%d\n",dp[a1][b1]);
}
return 0;
}
5万+




被折叠的 条评论
为什么被折叠?



