HDU 1159 Common Subsequence 动态规划

2017-08-06 15:41:04

writer:pprp

刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正)

题意如下:

给两个字符串,找到其中大的公共子序列,每个样例输出一个数;

最长公共子串(Longest Common Substirng)和最长公共子序列(Longest Common Subsequence,LCS)的区别为:

  子串是串的一个连续的部分,子序列则是从不改变序列的顺序,而从序列中去掉任意的元素而获得新的序列;

  也就是说,子串中字符的位置必须是连续的,子序列则可以不必连续。

动态规划的思想:abcfbc 和 abfcab找匹配值(图是大佬画的,借用一下^_^)

  

可以看出:

状态的定义:

  当前匹配到某一位置时已经匹配的数目

状态转移:设记录匹配状态的二维数组叫a[1001][1001]

    如果str1[i] == str2[j] 那么a[i][i] = a[i-1][j-1] + 1;

    如果str1[i] != str2[j] 那么a[i][j] = max(a[i-1][j], a[i][j-1]);

状态结束:

    匹配完成



代码如下:

  

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int a[1001][1001];

int _max(int a, int b)
{
      return a > b ? a : b;
}

int main()
{
    string str1,str2;
    while(cin >> str1 >> str2)
    {
        int len1 = str1.length();
        int len2 = str2.length();

        memset(a,0,sizeof(a));

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

        cout << a[len1][len2] << endl;
    }
    return 0;
}

提交状态:ac

时间: 2024-10-09 19:06:52

HDU 1159 Common Subsequence 动态规划的相关文章

POJ 1159 Palindrome &amp;&amp; HDU 1159 Common Subsequence

1.先说说杭电的1159吧! 这道题是基础动规,比较简单! 就是要你求最长的公共子序列(不要优化) 动态转移方程: dp[i+1][j+1]=(a[i]=b[i])?dp[i][j]+1:max(dp[i+1][j],dp[i][j+1]) AC代码: #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define N 520 char a[N],b[N]; in

HDU 1159——Common Subsequence(DP)

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

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

题目链接: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(裸LCS)

传送门: 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): 47676    Accepted Submission(s): 21890 Problem Description A subsequence of

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 el

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 最长公共子序列问题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]) }  

HDU 1159 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 there exists a stri