相关资料:
点击打开链接
输入
第1行:字符串A 第2行:字符串B (A,B的长度 <= 1000)
输出
输出最长的子序列,如果有多个,随意输出1个。
输入示例
abcicba
abdkscab
输出示例
abca
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
char a[1005],b[1005];
int c[1005][1005],book[1005][1005];
void LCS(int len1,int len2)
{
if(len1==0||len2==0)
return ;
else if(book[len1][len2]==1)
{
LCS(len1-1,len2-1);
printf("%c",a[len1-1]);
}
else if(book[len1][len2]==2)
LCS(len1-1,len2);
else
LCS(len1,len2-1);
}
int main()
{
int i,j,k,len1,len2;
gets(a);
gets(b);
len1=strlen(a);
len2=strlen(b);
for(i=1;i<=len1;i++)
{
for(j=1;j<=len2;j++)
{
if(a[i-1]==b[j-1])
{
c[i][j]=c[i-1][j-1]+1;
book[i][j]=1;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
book[i][j]=2;
}
else
{
c[i][j]=c[i][j-1];
book[i][j]=3;
}
}
}
int t1=len1,t2=len2;
LCS(t1,t2);
}