#include
#include
#include
#define len 100
void LCSLength(int m,int n,char *x,char *y,int c[len][len],int b[len][len]){
int i,j;
for(i=0;i<=m;i++) c[i][0]=0;
for(i=0;i<=n;i++) c[0][i]=0;
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
if(x[i]==y[j]){
c[i][j]=c[i-1][j-1]+1;
b[i][j]=1;
}
else{
if(c[i][j-1]>=c[i-1][j]){
c[i][j]=c[i][j-1];
b[i][j]=2;
}
else{
c[i][j]=c[i-1][j];
b[i][j]=3;
}
}
}
}
}
void LCS(int i,int j,char *x,int b[len][len]){
if(i==0||j==0)return ;
else{
if(b[i][j]==1){
LCS(i-1,j-1,x,b);
printf("%c",x[i]);
}
else if(b[i][j]==2){
LCS(i,j-1,x,b);
}
else{
LCS(i-1,j,x,b);
}
}
}
int main(){
char x[len],y[len];
int m,n;
char *xx,*yy;
int c[len][len]={0},b[len][len];
xx=(char*)malloc(len*sizeof(char));
yy=(char*)malloc(len*sizeof(char));
printf("Please Input A List:");
scanf("%s",x);
printf("Please Input B List:");
scanf("%s",y);
m=strlen(x);
n=strlen(y);
strcpy(xx+1,x);
strcpy(yy+1,y);
LCSLength(m,n,xx,yy,c,b);
LCS(m,n,xx,b);
printf("\n");
return 0;
}
阅读(795) | 评论(0) | 转发(0) |