poj 1458

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

420

题意:

我们称序列 Z = < z1, z2, ..., zk >是序列X = < x1, x2, ..., xm >的子序列当且仅当存在严格上升的序列< i1, i2, ..., ik >,使得对j = 1, 2, ... ,k, 有xij = zj。比如Z = < a, b, f, c > 是X = < a, b,c, f, b, c >的子序列。现在给出两个序列X
和Y,你的任务是找到X 和Y 的最大公共子序列,也就是说要找到一个最长的序列Z,使得Z 既是X 的子序列也是Y 的子序列.

思路:

设d(i,j)为A1,A2,...Ai和B1,B2,..Bj的LCS的长度,则当A[i]=A[j]时d(I,j)d(i-1,j-1)+1,否则d(i,j)=max{d(i-1,j),d(i,j-1)},时间复杂度为O(nm),其中n和m分别是序列A和B的长度。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
int const maxn=1000;
char s[maxn],t[maxn];
int dp[maxn+1][maxn+1];
void solve()
{
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(s[i]==t[j])
            {
                dp[i+1][j+1]=dp[i][j]+1;
            }
            else
            {
                dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);

            }
        }
    }
    printf("%d\n",dp[n][m]);
}
int main()
{

    while(scanf("%s%s",&s,&t)!=EOF)
    {
        n=strlen(s);
        m=strlen(t);
        solve();
    }
    return 0;

}

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

时间: 2024-10-12 02:46:30

poj 1458的相关文章

[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 动态规划DP

//  poj 1458  zoj 1733  最长公共子序列  DP #include <iostream>#include <string.h>#define N 1005using namespace std ;char  s1[N],s2[N];   int dp[N][N];int max(int a,int b)   {    return a>b ? a : b ;  }void f(int n,int m){   int i,j;    for (i=0; i

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

POJ 1458 LCS模板

LCS模板 存一个 #include "stdio.h" #include "string.h" int main() { char a[1010],b[1010]; int i,j,Max,dp[1010]; while (scanf("%s",a)!=EOF) { scanf("%s",b); memset(dp,0,sizeof(dp)); for (i=0;b[i];i++) { Max=0; for (j=0;a[j

POJ 1458 Common Subsequence (动态规划)

题目传送门 POJ 1458 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 ex

POJ 1458 Common Subsequence DP

http://poj.org/problem?id=1458 用dp[i][j]表示处理到第1个字符的第i个,第二个字符的第j个时的最长LCS. 1.如果str[i] == sub[j],那么LCS长度就可以+1,是从dp[i - 1][j - 1] + 1,因为是同时捂住这两个相同的字符,看看前面的有多少匹配,+1后就是最大长度. 2.如果不同,那怎么办? 长度是肯定不能增加的了. 可以考虑下删除str[i] 就是dp[i - 1][j]是多少,因为可能i - 1匹配了第j个.也可能删除sub

POJ 1458 - Common Subsequence(最长公共子串)

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=1458 AC代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 using namespace std; 7 int ans,lena,lenb,f[201

POJ 1458 - Common Subsequence(最长公共子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=1458 题目大意: 有若干组数据,每组给出两个字符串(中间用任意数量的空格间隔),输出这两个字符串最长公共子序列的长度.每次输出后换行. 分析: 动态规划求LCS,f[i][j]表示第一个字符串匹配到第i位,第二个字符串匹配到第j位时最长公共子序列的长度. 转移方程:当a[i] = b[i]时,f[i][j] = f[i-1][j-1]+1,其他情况时f[i][j

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

POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列长度. 分析: 本题不用输出子序列,非常easy,直接处理就可以. 首先令dp[i][j]==x表示A串的前i个字符和B串的前j个字符的最长公共子序列长度为x. 初始化: dp全为0. 状态转移: IfA[i]==B[j] then dp[i][j]= dp[i-1][j-1]+1 else dp[

POJ 1458 &amp;&amp; HDU 1159 Common Subsequence (最長公共子序列)dp

鏈接:http://poj.org/problem?id=1458 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 subseque