Problem Description
给定两个序列 X={x1,x2,…,xm} 和 Y={y1,y2,…,yn},找出X和Y的最长公共子序列。
Input
输入数据有多组,每组有两行 ,每行为一个长度不超过500的字符串(输入全是大写英文字母(A,Z)),表示序列X和Y。
Output
每组输出一行,表示所求得的最长公共子序列的长度,若不存在公共子序列,则输出0。
Sample Input
ABCBDAB BDCABA
Sample Output
4
Hint
#include <stdio.h>
#include <stdlib.h>
#define N 500
#include <string.h>
int main()
{
int i,j,m,n,c[N][N];
char x[N],y[N];
while(gets(x)&&gets(y))
{
m=strlen(x);
c[i][0]=0;
n=strlen(y);
c[0][j]=0;
for(i=1; i<=m; i++)
for(j=1; j<=n; j++)
{
if(x[i-1]==y[j-1])
c[i][j]=c[i-1][j-1]+1;
else if(c[i-1][j]>c[i][j-1])
c[i][j]=c[i-1][j];
else c[i][j]=c[i][j-1];
}
printf("%d\n",c[m][n]);
}
return 0;
}
加油喔!!···