POJ1458 && HDOJ1159 Common Subsequence【LCS】

题目链接(POJ) :http://poj.org/problem?id=1458

题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=1159

Common Subsequence

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 40156   Accepted: 16162

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, xij = 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

Source

Southeastern Europe 2003

题意: 求最长公共子序列长度

题解: 模板~~~

AC代码:

#include<iostream>
#include<cstring>
#include<string>
#define N 1005
using namespace std;
int dp[N][N],res;
string x,y;
int main()
{
    cin.sync_with_stdio(false);
    while(cin>>x>>y){
        res=0;
        memset(dp,0,sizeof(dp));
        int lenx=x.size(),leny=y.size();
        for(int i=1;i<=lenx;i++)
            for(int j=1;j<=leny;j++)
            if(x[i-1]==y[j-1])dp[i][j]=dp[i-1][j-1]+1;
            else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
        cout<<dp[lenx][leny]<<endl;
    }
    return 0;
}
时间: 2024-10-07 23:38:02

POJ1458 && HDOJ1159 Common Subsequence【LCS】的相关文章

hdoj 1159 Common Subsequence【LCS】【DP】

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28494    Accepted Submission(s): 12735 Problem Description A subsequence of a given sequence is the given sequence with some e

AOJ_ALDS1_10_C Longest Common Subsequence【LCS+DP】

Longest Common Subsequence Aizu - ALDS1_10_C For given two sequences X and Y, a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y. For example, if X={a,b,c,b,d,a,b} and Y={b,d,c,a,b,a}, the sequence {b,c,a} is a comm

UVA10405 Longest Common Subsequence【LCS+DP】

Given two sequences of characters, print the length of the longest common subsequence of both sequences. ????Sequence 1: ????Sequence 2: ????For example, the longest common subsequence of the following two sequences 'abcdgh' ans 'aedfhr' is 'adh' of

Poj 1458 Common Subsequence(LCS)

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

【算法导论学习-29】动态规划经典问题02:最长公共子序列问题(Longest common subsequence,LCS)

问题描述:序列X={x1,x2,-,xn},Y={y1,y2,-,yn},当Z={z1,z2-,zn}是X的严格递增下标顺序(可以不连续)的子集,也是Y的严格递增下标顺序(可以不连续)的子集,则Z是X和Y的公共子序列.例如X={A,B,C,B,D,A,B},Y={B,D,C,A,B,A},{B,C,A}.{B,C,B,A}.{B,D,A,B}都是X和Y的公共子序列.其中最长的公共子序列叫做Longest common subsequence,即经典的LCS. 具体点:char[]xArray和c

POJ1458 Common Subsequence 【最长公共子序列】

Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37614   Accepted: 15058 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..

HDU1159 &amp;&amp; POJ1458:Common Subsequence(LCS)

Problem 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 stri

POJ 1458 Common Subsequence 【最长公共子序列】

解题思路:先注意到序列和串的区别,序列不需要连续,而串是需要连续的,先由样例abcfbc         abfcab画一个表格分析,用dp[i][j]储存当比较到s1[i],s2[j]时最长公共子序列的长度 a    b    f    c    a    b 0    0    0    0    0   0    0 a  0    1     1    1    1   1    1 b  0    1     2    2    2   2    2 c  0    1     2  

POJ 1458:Common Subsequence【最长子序列】

Common Subsequence Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submission(s) : 1   Accepted Submission(s) : 1 Problem Description A subsequence of a given sequence is the given sequence with some elements (p