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. sql server和eclipse连接问题

    最近学习java,需要用的数据库sql sever,这就有一个连接问题需要设置 首先需要下载sql server,可查看我的博客sql sever下载教程: 连接教程:eclipse连接sql ser ...

  2. SpringBoot:静态资源的访问和配置(转载)

    默认静态资源访问Spring Boot的默认静态资源的路径为: spring.resources.static-locations=classpath:/META-INF/resources/,cla ...

  3. PyInstaller用法

    pyinstaller定义:PyInstaller是一个压缩python文件成为可执行程序的一个软件. pyinstaller工作原理:① 它会扫描你所有的Python文档,并分析所有代码从而找出所有 ...

  4. CF div2

    这是一道二进制思维题: 将所有数字列成二进制形式,然后找出最大的一位“1”出现一次的位数: 然后把这个数提到前面,其他照常输出即可 #include<bits/stdc++.h> usin ...

  5. ansible笔记(6):常用模块之系统模块

    1.cron模块 cron命令是计划任务功能,与crontab功能类似. 示例:时间格式--->>>分  时  日  月 星期 10 12 27 * *  tar -cvzf log ...

  6. 每天进步一点点------Allegro 原理图到PCB网表导入

  7. SDP协议介绍

    一.SDP协议介绍 SDP 完全是一种会话描述格式 ― 它不属于传输协议 ― 它只使用不同的适当的传输协议,包括会话通知协议(SAP).会话初始协议(SIP).实时流协议(RTSP).MIME 扩展协 ...

  8. 16进制 32进制 base64之间的区别

    Base64: 包含大写字母(A-Z),小写字母(a-z),数字(0-9)以及+/; Base32: 而Base32中只有大写字母(A-Z)和数字234567: Base16: 而Base16就是16 ...

  9. PHP导出身份证号科学计数法

    最近在做PHP的数据导入和导出,到处身份证号的时候,直接变成了科学计算法,找了一个很简单的方法就是这样 $obj= " ".$v['idcard']; 但是这样有空格啊,网上搜了一 ...

  10. 10day rpm简单用法qa ql qf "`"用法

    查看软件是否安装: [root@oldboyedu ~]# rpm -qa sl -q表示查询 -a表示所有 sl-5.02-1.el7.x86_64 查看软件包中有哪些信息 [root@oldboy ...