Common Subsequence

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

求两个字符串的最长公共子序列。
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;

char a[5000];
char b[1050];
char c[1050];
int dp[1050][1050]={0};

int main()
{
    while(scanf("%s %s",b,c)!=EOF)
    {
    memset(dp,0,sizeof(dp));
    int i;
   /* for(i=0;a[i]>=‘a‘&&a[i]<=‘z‘;i++)
        b[i]=a[i];
    b[i]=‘\0‘;
    int l=0;
    for(;a[i]!=‘\0‘;i++)
        if(a[i]>=‘a‘&&a[i]<=‘z‘)
            c[l++]=a[i];
    c[l]=‘\0‘;*/
    for(i=0;c[i]!=‘\0‘;i++)
        if(c[i]==b[0])
        {
            dp[i][0]=1;
            break;
        }
    for(;c[i]!=‘\0‘;i++)
        dp[i][0]=1;
     for(i=0;b[i]!=‘\0‘;i++)
        if(c[0]==b[i])
        {
            dp[0][i]=1;
            break;
        }
    for(;b[i]!=‘\0‘;i++)
        dp[0][i]=1;
    for(int i=1;b[i]!=‘\0‘;i++)
    {
        for(int j=0;c[j]!=‘\0‘;j++)
        {
            if(c[j]==b[i])
            {
                dp[j][i]=max(dp[j-1][i-1]+1,dp[j][i-1]);
            }
            else
                dp[j][i]=max(dp[j-1][i],dp[j][i-1]);
        }
    }
    int l1=strlen(b);
    int l2=strlen(c);
    printf("%d\n",dp[l2-1][l1-1]);
    }
    return 0;
}
时间: 2024-10-22 17:56:01

Common Subsequence的相关文章

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

hdu-1159 Common Subsequence (dp中的lcs问题)

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

hdu 1159 Common Subsequence(dp 最长公共子序列问题LCS)

最长公共子序列问题(LCS,Longerst Common Subsequence). s1s2……si+1和t1t2……tj+1的公共子序列可能是: ①当si+1=tj+1时,在s1s2……si+1和t1t2……tj+1的公共子序列末尾追加一个. ②s1s2……si+1和t1t2……tj的公共子序列 ③s1s2……si和t1t2……tj+1的公共子序列 所以易得到递推关系dp[i+1][j+1]=  max{ dp[i][j]+1 , dp[i][j+1] , dp[i+1][j]) }  

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

[2016-03-28][POJ][1458][Common Subsequence]

时间:2016-03-28 12:56:39 星期一 题目编号:[2016-03-28][POJ][1458][Common Subsequence] 题目大意:最长公共序列 #include <cstring> #include <iostream> #include <string> using namespace std; typedef long long LL; const int maxn = 1000 + 100; int dp[maxn][maxn];

poj 1458 Common Subsequence

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

【POJ1458】Common Subsequence

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

Dynamic Programming | Set 4 (Longest Common Subsequence)

首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", "bdf", "aeg", '"acefg"等都是"abcdefg"的子序列.因此,一个长度为n的序列拥有2^n中可能的子序列(序列中的每一个元素只有选或者不选两种可能,因此是2^n). Example: LCS for inp

hdu 1159 Common Subsequence(最长公共子序列 DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25416    Accepted Submission(s): 11276 Problem Description A subsequence of