Common Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 57902    Accepted Submission(s): 26737

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
 
理解题意:
首先我们的了解什么是子序列,一个给定序列的子序列是在该序列中删去若干元素后的得到的序列,可以是不连续的。
 
做题思路:
找到两个序列的最长公共子序列,用最基础的DP模板即可
 
代码:
#include <bits/stdc++.h>

using namespace std;

int dp[][];
string str1,str2; int LCS(){
for(int i = ;i < str1.length(); i++){
for(int j = ;j < str2.length(); j++){
if(str1[i] == str2[j])
dp[i+][j+] = dp[i][j] + ;
else
dp[i+][j+] = max(dp[i][j+],dp[i+][j]);
}
}
return dp[str1.length()][str2.length()];
} int main()
{
while(cin>>str1>>str2){
cout<<LCS()<<endl;
}
return ;
}

最长公共子序列-Hdu1159的更多相关文章

  1. 动态规划---最长公共子序列 hdu1159

    hdu1159 题目要求两个字符串最长公共子序列, 状态转换方程   f[i][j]=f[i-1][j-1]+1; a[i]=b[j]时 f[i][j]=MAX{f[i-1][j],f[i][j-1] ...

  2. LCS最长公共子序列HDU1159

    最近一直在学习算法,基本上都是在学习动态规划以及字符串.当然,两者交集最经典之一则是LCS问题. 首先LCS的问题基本上就是在字符串a,b之间找到最长的公共子序列,比如 YAOLONGBLOG 和 Y ...

  3. ACMDP之最长公共子序列长度—HDU1159

    Common Subsequence Problem Description A subsequence of a given sequence is the given sequence with ...

  4. 【水:最长公共子序列】【HDU1159】【Common Subsequence】

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

  5. hdu1159 dp(最长公共子序列)

    题意:给两个字符串,求这两个字符串的最长公共子序列的长度 因为之前集训的时候做过,所以现在即使会做也并不是什么稀奇的事,依旧为了自己的浅薄感到羞愧啊``` 解法就是通过两个字符串的每个字符互相比较,根 ...

  6. hdu1503 最长公共子序列变形

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1503 题意:给出两个字符串 要求输出包含两个字符串的所有字母的最短序列.注意输出的顺序不能 ...

  7. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

  8. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  9. [Data Structure] LCSs——最长公共子序列和最长公共子串

    1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...

随机推荐

  1. 解决Creating Server TCP listening socket 54.179.160.162:7001: bind: Cannot assign requested address

    背景:之前在测试环境搭过一个redis集群,运维把服务器重启之后我重新开启redis集群始终起不来,但是有没有任何日志,经过如下步骤最终解决问题 1.修改日志路径,根据日志查看为什么会启动失败[前期操 ...

  2. spring_boot 加入 mybatis

    第一步: <!-- mybatis启动器 自动包含jdbc所以不需要再次引入jdbc依赖 --> <dependency> <groupId>org.mybatis ...

  3. 【Python】数值运算函数

  4. C# RichTextBox实现背景透明

    这几天在做一个文本编辑器,要将RichTextBox的背景透明,但是发现C#的RichTextBox是不支持将背景设置为Transparent(透明). 网上找了好多方法,但都不行. 后来自己想了个办 ...

  5. redis架构

    hash槽16384个,0-16383 master1(slave101,slave102)     master2  (slave201,slave202)    master 3 (slave30 ...

  6. 题解 P5587 【打字练习】

    P5587 打字练习 想发一篇较为简洁易懂的题解,代码看起来长,实际上还是很好理解的,而且很多对称着写就行了 一道字符串签到题,比赛的时候小蒟蒻调了一个小时都没调出来一直RE,坑点还是不少的(主要是我 ...

  7. 安装ipython[win/linux]

    首先以win7  64位系统, python2.7.9为例,linux见底部 1.下载材料http://files.cnblogs.com/files/smileyes/ipython-win64.z ...

  8. IntelliJ IDEA 2017.3尚硅谷-----修改当前主题字体、字体大小、行间距、控制台、注释

  9. AcWing STL初步学习

    vector, 变长数组,倍增的思想 size() 返回元素个数 empty() 返回是否为空 clear() 清空 front()/back() push_back()/pop_back() beg ...

  10. js location.href 的用法

    self.location.href="/url" 当前页面打开URL页面: this.location.href="/url" 当前页面打开URL页面: pa ...