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

当 i = 0 , j = 0 时 , dp[i][j] = 0

当 i , j > 0 ; xi = yi 时 ,dp[i][j] = dp[i-1][j-1] + 1

当 i , j > 0 ; xi != yi 时 ,dp[i][j] = max { dp[i][j-1] , dp[i-1][j] }

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<cmath>

using namespace std;

char str1[1005];
char str2[1005];
int dp[1005][1005];

int main()
{
    while(scanf("%s %s",str1+1,str2+1)!=EOF)
    {
        memset(dp, 0, sizeof(dp));
        int len1=strlen(str1+1);
        int len2=strlen(str2+1);

        for(int i=1; i<=len1; i++)
            for(int j=1; j<=len2; j++)
            {
                if(str1[i]==str2[j])
                    dp[i][j]=dp[i-1][j-1]+1;
                else
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);

            }
        cout<<dp[len1][len2]<<endl;

    }
    return 0;
}

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

时间: 2024-08-11 22:46:49

HDU 1159:Common Subsequence(最长公共子序列)的相关文章

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

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

#include<bits/stdc++.h> using namespace std; int dp[1024][1024]; int main() { int i,j; char s1[1024],s2[1024]; while(~scanf("%s %s",s1,s2)) { memset(dp,0,sizeof(dp)); int len1=strlen(s1); int len2=strlen(s2); for(i=0;i<len1;i++) { for(j

hdu 1159 common sequence (最长公共子序列 dp)

http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意 : 给出两个字符串 求出最长公共子序列 思路: if(str1[i]==str2[j]) { dp[i][j]=max(dp[i-1][j-1]+1,max(dp[i-1][j],dp[i][j-1])); } else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); #include<cstdio> #include<cstring> #include&l

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[

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

问题描述 最长公共子序列,英文缩写为LCS(Longest Com #include <bits/stdc++.h> const int MAX=1010; char x[MAX]; char y[MAX]; int DP[MAX][MAX]; int b[MAX][MAX]; using namespace std; int PRINT_LCS(int b[][MAX],char *x,int i,int j) { if(i==0||j==0) return 1; if(b[i][j]==1

POJ 1458 Common Subsequence 最长公共子序列

题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<iostream> #include<algorithm> #define INF 0x3f3f3f3f #define MAXSIZE 10

LCS修改版(Longest Common Subsequence 最长公共子序列)

题目描述 作为一名情报局特工,Nova君(2号)有着特殊的传达情报的技巧.为了避免被窃取情报,每次传达时,他都会发出两句旁人看来意义不明话,实际上暗号已经暗含其中.解密的方法很简单,分别从两句话里删掉任意多个字母,使得两句话剩余的部分相同,通过一定的删除手法,可以让剩余的部分相同且长度最大,就得到了可能的暗号.暗号可能有多个,还要进行筛选,现在情报局人手不够,希望你能助一臂之力,筛选工作不用你完成,你只需计算出暗号长度以及个数即可.(注意,字母的位置也是暗号的重要信息,位置不同的字母组成的暗号不

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

LCS 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #define clc(a,b) memset(a,b,sizeof(a)) 6 #define LL long long 7 #include<cmath> 8 using namespace std; 9 int dp[1010][1010];//表示到i-1,j-1的

hdu 1159 经典dp最长公共子序列

背景:上次比赛就没有做出来,回来根据实际意义半天也想不出如何dp,结果从猜转移方程入手,竟然想对了!开始想把空间优化到一维数组,没有想到要用同维度左边的值wa了. 思路: dp[i][j]=max{max[i-1][j],max[i][j-1],max[i-1][j-1]+(a[i] == b[j])} //dp[i][j]定以为,a串的前i个字符和b串的前b个字符的最大字串和,为选a串的第i个字符而不选b串的第j个字符,不选a串的第i个字符而选b串的第j个字符,既选a串的第i个字符又选b串的第