链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159

题解

#include<iostream>
#include<cstring>
using namespace std;

],B[]; //存放字符串
][]; //存放到字符串a第 i+1个字符,字符串b第 j+1个字符为止的最大长度公共子序列的长度

int main(){
    while(~scanf("%s%s",A,B)){
        int alen=strlen(A),blen=strlen(B); //注意头文件
        ;i<alen;i++){
            ;j<blen;j++){
                if(A[i]==B[j])
                    dp[i+][j+]=dp[i][j]+;
                else
                    dp[i+][j+]=max(dp[i][j+],dp[i+][j]);
            }
        }
        printf("%d\n",dp[alen][blen]);
    }
    ;
}

定义 dp[ i ][ j ]为 A1...Ai 和 B1...Bj 对应的 LCS(最长公共子序列)长度

那么,A1...Ai+1 和 B1...Bj+1 对应的公共子列可能是

  • 当 Ai+1==Bj+1时,在 A1...Ai 和 B1...Bj 的公共子列末尾追加上 Ai+1
  • A1...Ai+1 和 B1...Bj 的公共子列
  • A1...Ai 和 B1...Bj+1 的公共子列

三者中的一个,则有以下递推关系成立

该递推式时间复杂度O(nm),dp[n][m]就是LCS的长度

HDU 1159——Common Subsequence(DP)的更多相关文章

  1. HDU 1159 Common Subsequence (dp)

    题目链接 Problem Description A subsequence of a given sequence is the given sequence with some elements ...

  2. hdu 1159 Common Subsequence (dp乞讨LCS)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. hdu 1159:Common Subsequence(动态规划)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. hdu 1159 Common Subsequence(LCS)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  6. HDU 1159 Common Subsequence(裸LCS)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  7. hdu 1159 Common Subsequence (最长公共子序列 +代码)

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  8. 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...

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

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. HDU-6356 Glad You Came 线段树 ST表

    HDU-6356 题意:有m次操作,每次操作通过给定的随机函数生成 l , r , v,使得在 l 到 r 区间内,所有的a[i]变为max(a[i] , v). 最后输出n个a[i]* i的异或和. ...

  2. HDU-3549Flow Problem 最大流模板题

    传送门 这里是Ford-Fulkerson写的最大流模板 #include <iostream> #include <cstdio> #include <algorith ...

  3. CodeForces 785 D Anton and School - 2 范德蒙恒等式

    Anton and School - 2 题解: 枚举每个左括号作为必选的. 那么方案数就应该是下面的 1 , 然后不断化简, 通过范德蒙恒等式 , 可以将其化为一个组合数. 代码: #include ...

  4. poj 1182 食物链(种类并查集 ‘初心者’)

    题目链接:http://poj.org/problem?id=1182 借着这题可以好好理解一下种类并查集,这题比较简单但挺经典的. 题意就不解释了,中问题. 关于种类并查集结局方法也是挺多的 1扩增 ...

  5. Python 之父的解析器系列之五:左递归 PEG 语法

    原题 | Left-recursive PEG grammars 作者 | Guido van Rossum(Python之父) 译者 | 豌豆花下猫("Python猫"公众号作者 ...

  6. Python远程连接MySQL数据库

    使用Python连接数据库首先需要安装Python的数据库驱动. 我的本地只装了Python,并没有装MySQL,当我使用命令: sudo pip install mysql-python 安装驱动( ...

  7. 通过脚本实现将服务器的Log实时传送到Telegram群组

    首先说下需求,IT老大提出的一个需求,实现将php-laravel的应用日志实时传送到telegram的监控群组中,不用登陆服务器就可以实时查看应用的日志. 具体思路是: 先要将日志切割,并实时更新这 ...

  8. 深入理解SQL Server数据库Select查询原理(一)

    使用SQL Server十年有余,但是一直对其Select查询机制原理一致不明,直到最近有个通讯录表,很简单的一张表(但因简单,所以当时并没有考虑按部门排序问题),结果想查询某个单位所有部门(不重复) ...

  9. Linux安装yum install gcc-c++出错:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was 14: curl#6 - "Could not resolve host: mirrorlist.centos...

    错误如图: 解决办法: 1.修改配置文件 /etc/resolv.conf,该配置文件如下: 2.输入:gedit resolv.conf,修改配置文件内容如下: 3.然后重启: 4.重新进行安装: ...

  10. guava multimap介绍

    引用一篇别人的博客,理解理解 http://vipcowrie.iteye.com/blog/1517338