POJ 1458 Common Subsequence

原创 2016年08月29日 16:57:31

Question:
Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, …, xm > another sequence Z = < z1, z2, …, zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, …, ik > of indices of X such that for all j = 1,2,…,k, x ij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
Input
The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.
Output
For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
题目大意:给出两个字符串,求其最大公共子序列的长度。
解题思路:就是一道裸的求最长公共子序列的题。
(http://acm.hust.edu.cn/vjudge/contest/125402#problem/E)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
LL dp[1005][1005];  
char s[1005],z[1005];
int main()
{
    while(~scanf("%s%s",s,z))
    {
        memset(dp,0,sizeof(dp));
        int len1=strlen(s);
        int len2=strlen(z);  //以前容易把这一步放进循环中,造成时间复杂度很高
        for(int i=1;i<=len1;i++)
        {
            for(int j=1;j<=len2;j++)
            {
                if(s[i-1]==z[j-1])
                    dp[i][j]=dp[i-1][j-1]+1;   //dp[i][j]表示i长度的字符串与j长度的字符串的相同字母
                else
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            }
        }
        cout<<dp[len1][len2]<<endl;
    }
}

体会:这是一道裸的求最大公共子序列的题,只需记下模板,方便以后套用

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

poj 1458 Common Subsequence(lcs模板)

Description A subsequence of a given sequence is the given sequence with some elements (possible no...

poj1458 Common Subsequence 最长公共序列 解题报告(附详细分析)

这道题是动态规划的典型题目。 动态方程: if(str1[i] == str2[j]) MaxLen[i][j] = MaxLen[i-1][j-1] + 1; //比较到两个字母相同时,把前一状态的...

动态规划总结五 poj 1458 Common Subsequence(最长公共子序列)

Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissio...

HDOJ 1159(POJ 1458)Common Subsequence (最长公共子序列 LCS)

最大公共子序列长度 (LCS)Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32...

【北大夏令营笔记-动态规划】poj1458-Common Subsequence

Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37543 Accepted: 1501...

POJ 1458 Common Subsequence

Description A subsequence of a given sequence is the given sequence with some elements (possible no...

Poj 1458 Common Subsequence 最长非连续序列

题目描述 求最长相同序列,非连续,abc与ac 最长相同序列为2 相关问题, 戳: 最长公共子序列长度 最长上升子序列长度 思路: dp[i][j] 表示:a串中前i个元素 与 b...

POJ--1458:Common Subsequence (DP求最长公共子序列)

1. 题目源地址:http://poj.org/problem?id=1458 2. 基本题意: 给出两个序列,求出最长子序列的长度并输出。经典的动态规划求解,DP方程如下:

POJ 1458 Common Subsequence

nlogn求最长公共子序列

POJ 1458 Common Subsequence

题目大意:         一个序列的子序列为原序列去掉若干元素所得的序列(可以去掉0个或全部,空序列也为子序列,去掉的元素可以为原序列中任意位置的元素)。        现有多个测例(测例数无上...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)