HDU 1159 Common Subsequence

 

http://acm.hdu.edu.cn/showproblem.php?pid=1159

dp[i][j]代表长度为i的串s1与长度为j的串s2的最长公共子串

View Code
#include <iostream>

using namespace std ;

int dp[1001][1001] ;

int main()

{

    char s1[1001],s2[1001] ;

    while(~scanf("%s%s",s1,s2))

    {

        int len1=strlen(s1) ;

        int len2=strlen(s2) ;

        memset(dp,0,sizeof(dp)) ;

        for(int i=1;i<=len1;i++)

            for(int j=1;j<=len2;j++)

            {

                if(s1[i-1]==s2[j-1])

                    dp[i][j]=dp[i-1][j-1]+1 ;

                else

                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]) ;

            }

        printf("%d\n",dp[len1][len2]) ;

    }

    return 0 ;

}

 

你可能感兴趣的:(sequence)