hdu 1159 dp 最长公共子序列

#include <stdio.h>
#include <string.h>

#define M 1000
char b[M],c[M];
int a[M][M];

int main()
{
    int i,j,n,m;    

    while(scanf("%s%s",b+1,c+1)!=EOF)
    {
            
        n=strlen(b+1);

        m=strlen(c+1);

        for(i=0;i<=n;i++)
            a[i][0]=0;

        for(i=0;i<=m;i++)
            a[0][i]=0;

        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(b[i]==c[j])
                {
                    a[i][j]=a[i-1][j-1]+1;
                }
                else
                {
                    if(a[i-1][j]>a[i][j-1])
                    {
                        a[i][j]=a[i-1][j];
                    }
                    else
                    {
                        a[i][j]=a[i][j-1];
                    }
                }
            }
        }

        printf("%d\n",a[n][m]);
    }

    return 0;
}

  

原文地址:https://www.cnblogs.com/jackes/p/2454800.html