PKU POJ 1458 Common Subsequence LCA 最长公共子序列

View Code
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

int LCS(char x[],int l1,char y[],int l2)
{
    int i,j,p,dp[2][1005];
    memset(dp[0],0,sizeof(dp[0]));
    for (j=0,p=1;j<l2;++j)
    {
        dp[p][0]=0;
        for (i=0;i<l1;++i)
        {
            if (x[i]==y[j])
                dp[p][i+1]=dp[1-p][i]+1;
            else
                dp[p][i+1]=dp[p][i]>dp[1-p][i+1]?dp[p][i]:dp[1-p][i+1];
        }
        p=1-p;
    }
    return dp[1-p][l1];
}
int main()
{
    char x[1000],y[1000];
    while(scanf("%s %s",x,y)!=EOF)
    {
        printf("%d\n",LCS(x,strlen(x),y,strlen(y)));
    }
    return 0;
}

 

posted on 2013-03-28 16:07  Deller  阅读(161)  评论(0)    收藏  举报

导航