Chinaunix首页 | 论坛 | 博客
  • 博客访问: 388913
  • 博文数量: 70
  • 博客积分: 1919
  • 博客等级: 上尉
  • 技术积分: 1179
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 20:05
文章分类

全部博文(70)

文章存档

2014年(2)

2013年(29)

2012年(20)

2011年(1)

2010年(13)

2009年(5)

分类: C/C++

2010-11-10 13:50:33

#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;
}
阅读(761) | 评论(0) | 转发(0) |
0

上一篇:矩阵相乘算法

下一篇:man手册

给主人留下些什么吧!~~