最长公共子序列 记录子序列~

最长公共子序列 记录子序列~
 1 #include < stdio.h >
 2 #include < string .h >
 3 char  a[ 101 ],b[ 101 ],d[ 101 ];
 4 int  c[ 101 ][ 101 ];
 5 int  max( int  x, int  y)
 6 {
 7    if(x>y)
 8    return x;
 9    else
10    return y;
11}

12 int  main()
13 {
14    int lena,lenb,i,j,k,m;
15    while(scanf("%s%s",a+1,b+1)!=EOF)
16    {
17        m=0;
18        memset(d,0,sizeof(d));
19        lena=strlen(a+1);
20        lenb=strlen(b+1);
21        for(i=0;i<=lena;i++)
22             c[0][i]=0;
23             for(j=0;j<=lenb;j++)
24                c[j][0]=0;
25                k=0;
26                for(i=1;i<=lena;i++)
27                for(j=1;j<=lenb;j++)
28                 {
29                     if(a[i]==b[j])
30                       {
31                           c[i][j]=c[i-1][j-1]+1;
32                           if(c[i][j]>m)
33                           {
34                               d[k]=a[i];
35                              k++;
36                           }

37
38                       }

39                    else
40                        c[i][j]=max(c[i-1][j],c[i][j-1]);
41                    if(c[i][j]>m)
42                        m=c[i][j];
43                 }

44                 printf("%d\n",c[lena][lenb]);
45                 printf("%s\n",d);
46    }

47    return 0;
48}

49

你可能感兴趣的:(最长公共子序列 记录子序列~)