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. 【洛谷p1190】接水问题

    接水问题[题目链接] 咱也不知道为啥咱就是想写博客emmm 尽管这个题是2010年的普及组但是我太菜了并不会写emm 其实感觉这道题是纯纯正正的大模拟: 算法标签中的贪心是没有意义的啊?? SOLUT ...

  2. 使用class 自动创建设备节点

    #include <linux/init.h>// __init __exit #include <linux/module.h> // module_init module_ ...

  3. 经典的最大流题POJ1273(网络流裸题)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  4. SQL结构化查询语言

    一.SQL 结构化查询语言 1.T-SQL 和 SQL的关系 T-SQL是SQL的增强版 2.SQL的组成 2.1 DML (数据操作语言) 增加,修改,删除等数据操作 2.2 DCL (数据控制语言 ...

  5. sqlserver 高版本迁移到低版本

    奇葩事不少,  这不, 得把 sqlserver 2014 迁移到 2012 开始以为用备份再还原的方法就可以, 谁知道最终兼容性的问题无法解决(低版本不兼容高版本备份的文件, 即便在高版本中选择了兼 ...

  6. vue动态生成表单

    vue动态生成表单 一.下拉框 <!DOCTYPE html> <html> <head> <meta charset="utf-8" / ...

  7. kernel32.dll提供的接口分析

    HANDLE hToken = NULL; DWORD cursessionid; HMODULE hInstKernel32 = NULL; hInstKernel32 = LoadLibrary( ...

  8. [php代码审计] php://filter

    筛选过滤应用: 1. 字符串过滤器: string.rot13 对字符串执行ROT13转换 string.toupper转换为大写 string.tolower 转换为小写 string.strip_ ...

  9. hive之wordcount

    1.创建一张表,记录文件数据,使用换行符作为分隔符 create table file_data(content string) row format delimited fields termina ...

  10. 向Hive中导入数据的方式

    一.Hive客户端:根据数据源不同划分 1.从本地文件系统中导入数据到hive表中: load data local inpath "path" [OVERWRITE] into ...