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
 
现附上AC代码:

#include<iostream>
#include<cstring>
using namespace std;
const int maxn=1000+10;
int s[maxn][maxn];
char str1[maxn],str2[maxn];

void solve()
{
memset(s,0,sizeof(s));
while(~scanf("%s%s",str1+1,str2+1))
{
int s1=strlen(str1+1),s2=strlen(str2+1);
for(int i=1;i<=s1;i++)
{
for(int j=1;j<=s2;j++)
{
if(str1[i]==str2[j]) s[i][j]=s[i-1][j-1]+1;
else s[i][j]=max(s[i-1][j],s[i][j-1]);
}
}
cout<<s[s1][s2]<<endl;
}
}
int main()
{
solve();
return 0;
}

在做动态规划问题时,有不少情况都是需要申请一个二维数组存储每个状态。例如这道题中s[i][j]存储的是第一个字符串前i个字符与第二个字符串前j个字符的最长公共子序列,而这也是动态规划的主要思想,多阶段决策。有时二维数组也可用一维数组进行代替,使用滚动数组,但这样就不能直到最有方案的具体步骤。

做动态规划重要的是找好二维数组,明确两个下标的具体意义,并找到递推公式,那么这道题就基本可以完成了。

hdu1159Common Subsequence——动态规划(最长公共子序列(LCS))的更多相关文章

  1. 动态规划 最长公共子序列 LCS,最长单独递增子序列,最长公共子串

    LCS:给出两个序列S1和S2,求出的这两个序列的最大公共部分S3就是就是S1和S2的最长公共子序列了.公共部分 必须是以相同的顺序出现,但是不必要是连续的. 选出最长公共子序列.对于长度为n的序列, ...

  2. 动态规划----最长公共子序列(LCS)问题

    题目: 求解两个字符串的最长公共子序列.如 AB34C 和 A1BC2   则最长公共子序列为 ABC. 思路分析:可以用dfs深搜,这里使用到了前面没有见到过的双重循环递归.也可以使用动态规划,在建 ...

  3. 动态规划——最长公共子序列LCS及模板

    摘自 https://www.cnblogs.com/hapjin/p/5572483.html 这位大佬写的对理解DP也很有帮助,我就直接摘抄过来了,代码部分来自我做过的题 一,问题描述 给定两个字 ...

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

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

  5. 动态规划之最长公共子序列LCS(Longest Common Subsequence)

    一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...

  6. C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解

    版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...

  7. 《算法导论》读书笔记之动态规划—最长公共子序列 & 最长公共子串(LCS)

    From:http://my.oschina.net/leejun2005/blog/117167 1.先科普下最长公共子序列 & 最长公共子串的区别: 找两个字符串的最长公共子串,这个子串要 ...

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

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  9. 编程算法 - 最长公共子序列(LCS) 代码(C)

    最长公共子序列(LCS) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定两个字符串s,t, 求出这两个字符串最长的公共子序列的长度. 字符 ...

  10. 1006 最长公共子序列Lcs

    1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdks ...

随机推荐

  1. HDU-3665 Seaside

    XiaoY is living in a big city, there are N towns in it and some towns near the sea. All these towns ...

  2. java synchronized实现可见性对比volatile

    问题: 大家可以先看看这个问题,看看这个是否有问题呢? 那里有问题呢? public class ThreadSafeCache { int result; public int getResult( ...

  3. C++对象构造时,构造函数运行时并不知道VT的存在

    class A {public: A() { init(); } virtual void init() { printf("A::init\n"); }}; class B : ...

  4. moongoose对象无法新增删除属性

    昨天用nodes中的moongoose去查询一个结果遇到一个大坑,这个坑貌似用moongoose可能会遇到.背景是这样的,我在nodejs中去查询document,得到的可以看作是一个对象list.在 ...

  5. for循环延伸

    经典面试题解析: for(var i = 1 ; i < 5 ; i++){ console.log(i) } //1 2 3 4 ------------------------------- ...

  6. element 弹框关闭报错

    <template> <el-container style="padding: 00px 20px 0px 20px"> <el-dialog ti ...

  7. mysql导出函数或者存储过程 设置显示方式

    mysql导出函数或者存储过程 mysqldump -hhostname -uusername -ppassword -ntd -R databasename > /app/backupflie ...

  8. left join on and和left join on where条件的困惑[转]

    外连接:left join(左联接) left outer join 返回包括左表中的所有记录和右表中联结字段相等的记录right join(右联接) right outer join返回包括右表中的 ...

  9. Java EE的优越性主要表现在哪些方面

    J2 EE的优越性主要表现在哪些方面 J2EE基于JAVA 技术,与平台无关. J2EE拥有开放标准,许多大型公司实现了对该规范支持的应用服务器.如BEA ,IBM,ORACLE等. J2EE提供相当 ...

  10. SSM三大框架整合梳理

    整合步骤 0.搭建动态web项目 1.需要的jar包 spring(包括springmvc) mybatis相关jar包 mybatis与spring的整合包(个人建议尽量使用高版本的,避免出现一些奇 ...