DP———1002

来源:互联网 发布:四大行网络银行业务 编辑:程序博客网 时间:2024/06/05 20:42

题目:最长公共子序列

题意:给出两个序列X,Z,两者是否存在严格递增序列。

思路:LCS问题,找到的最大长度序列的共同的x和y

代码:

#include <stdio.h>
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[1005][1005];
int main()
{
    int i,j;
    char s1[1000],s2[1000];
    int len1,len2;
    while(~scanf("%s%s",s1,s2))
    {
len1=strlen(s1);
len2=strlen(s2);
        memset(dp,0,sizeof(dp));


        for(i = 1; i<=len1; i++)
{
            for(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;
}


0 0
原创粉丝点击