HDU 1159 Common Subsequence (动态规划、最长公共子序列)

Common Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 55301    Accepted Submission(s): 25537

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 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.
The program input is from a text file. Each
data set in the file contains two strings representing the given
sequences. The sequences are separated by any number of white spaces.
The input data are correct. 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

题目大意与分析

就是求最长公共子序列,动态规划即可。

dp[i][j]代表A序列前i-1个元素与B序列前j-1个元素的最长公共子序列的个数,两层循环,相等就++,否则取当前最大的

代码

#include<bits/stdc++.h>

using namespace std;

string x,y;
int i,j,dp[1005][1005];

int main()
{
    while(cin>>x>>y)
    {
        memset(dp,0,sizeof(dp));
        for(i=0;i<x.size();i++)
        {
            for(j=0;j<y.size();j++)
            {
                if(x[i]==y[j])
                dp[i+1][j+1]=dp[i][j]+1;
                else
                dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
            }
        }
        cout<<dp[x.size()][y.size()]<<endl;
    }
}

原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/11392138.html

时间: 2024-09-29 05:22:49

HDU 1159 Common Subsequence (动态规划、最长公共子序列)的相关文章

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

题目链接: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): 37551    Accepted Submission(s): 17206 Problem Description A subsequence of

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

Common Subsequence 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

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

题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ while(scanf("%s%s",s1,s2)!=EOF){ int l1=strlen(s1); int l2=strlen(s2); mem(dp,0); dp[0][0]=((s1[0]==s2[0])?1:0); rep(i,1,l1-1) if(s1[i]==s2[0]) 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

杭电1159 Common Subsequence【最长公共子序列】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 解题思路:任意先给出两个字符串 abcfbc abfcab,用dp[i][j]来记录当前最长的子序列,则如果有x[i]与y[j]相等的话,则相当于公共子序列的长度在dp[i-1][j-1]上增加1, 如果x[i]与y[j]不相等的话,那么dp[i][j]就取得dp[i][j-1]和dp[i-1][j]中的最大值即可.时间复杂度为O(mn) 反思:大概思路想出来之后,因为dp数组赋初值调了很久,

HDU 1159:Common Subsequence(最长公共子序列)

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

HDU 1159 Common Subsequence 动态规划

2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中大的公共子序列,每个样例输出一个数: 最长公共子串(Longest Common Substirng)和最长公共子序列(Longest Common Subsequence,LCS)的区别为: 子串是串的一个连续的部分,子序列则是从不改变序列的顺序,而从序列中去掉任意的元素而获得新的序列: 也就是说

uva 10405 Longest Common Subsequence (最长公共子序列)

uva 10405 Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences: abcdgh a

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(最长公共子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接: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