LCS,输出还是没有想出来。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int f[110][110],dp[110][110];
char s1[110][35],s2[110][35];
bool flag;
void fun(int i,int j)
{
if(!i||!j) return;
if(f[i][j]==1) {fun(i-1,j);return ;}
if(f[i][j]==-1) {fun(i,j-1);return ;}
fun(i-1,j-1);
if(flag) printf(" %s",s1[i-1]);
else{printf("%s",s1[i-1]);flag++;}
}
int main()
{
int i,j,n1,n2;
n1=n2=0;
while(~scanf("%s",s1[n1]))
{
n1++;
while(~scanf("%s",s1[n1])&&s1[n1][0]!='#') n1++;
while(~scanf("%s",s2[n2])&&s2[n2][0]!='#') n2++;
memset(dp,0,sizeof(dp));
for(i=1;i<=n1;i++)
for(j=1;j<=n2;j++)
{
if(!strcmp(s1[i-1],s2[j-1]))
{
dp[i][j]=dp[i-1][j-1]+1;
f[i][j]=0;
}
else
if(dp[i][j-1]>dp[i-1][j])
{
dp[i][j]=dp[i][j-1];
f[i][j]=-1;
}
else
{
dp[i][j]=dp[i-1][j];
f[i][j]=1;
}
}
flag=0;
fun(n1,n2);
printf("
");
n1=n2=0;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/