码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1159 Common Subsequence

时间:2015-07-22 12:31:44      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:

最长公共子序列

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn=1111;
int dp[maxn][maxn];
char s[maxn],t[maxn];
int lens,lent;

int main()
{
    int i,j;
    while(~scanf("%s%s",s,t))
    {
        lens=strlen(s);
        lent=strlen(t);
        for(i=-1; i<lens; i++)
            for(j=-1; j<lent; j++)
            {
                if(i==-1||j==-1)
                {
                    dp[i+1][j+1]=0;
                    continue;
                }
                if(s[i]==t[j])
                    dp[i+1][j+1]=dp[i][j]+1;
                else
                    dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
            }
        printf("%d\n",dp[lens][lent]);
    }
    return 0;
}

 

HDU 1159 Common Subsequence

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/4666555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!