Common Subsequence

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

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

【题意】

  一个给定序列的子序列是一个给定序列,其中有些元素(可能没有)被省略。给定一个序列 X = <x1, x2, ..., xm> 另一个序列 Z = <z1, z2, ..., zk> 是X的子序列,如果存在严格递增序列, <i1, i2, ..., ik> 对所有 j = 1,2,…,k, xij = zj。例如,Z = <a, b, f, c> 是 X = <a, b, c, f, b, c>  的子序列,索引序列< 1,2,4,6 >。给定两个序列X和Y,问题是求X和Y的最大长度公共子序列的长度。

  序列由任意数量的空格分隔。输入数据是正确的。对于每组数据,程序在标准输出上打印从单独行开始的最大长度公共子序列的长度。

  注意:最长公共子序列 ,可以不连续。

【代码】

#include<bits/stdc++.h>
#define MAX 5005
using namespace std;
int f[MAX][MAX];
string s,t;
int main(){
while(cin>>s>>t){
for(int i=;i<=s.length();i++)
for(int j=;j<=t.length();j++){
f[i][j]=max(f[i-][j],f[i][j-]);
if(s[i-]==t[j-])
f[i][j]=max(f[i][j],f[i-][j-]+);
}
cout<<f[s.length()][t.length()]<<endl;
}
return ;
}

HDU 1159 最长公共子序列的更多相关文章

  1. HDU 1159 最长公共子序列(n*m)

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

  2. POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  3. HDU - 1503 最长公共子序列记录路径

    题意:先给两个水果的名字然后得出一个最短的序列包含这两个词. 思路:我一开始的思路是先求出最长公共子序列,然后做一些处理将其他的部分输出来:两种水果的字符串和最长公共子序列的字符串这三个字符串做对比, ...

  4. HDU 1159 Common Subsequence 公共子序列 DP 水题重温

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

  5. hdu 4681 最长公共子序列+枚举

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4681 #include<cstdio> #include<cstring> # ...

  6. hdu 1503 最长公共子序列

    /* 给两个串a,b.输出一个最短的串(含等于a的子序列且含等于b的子序列) */ #include <iostream> #include <cstdio> #include ...

  7. hdoj 1159最长公共子序列

     /*Common Subsequence A subsequence of a given sequence is the given sequence with some elements ( ...

  8. HDU 1159 Common Subsequence:LCS(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...

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

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

随机推荐

  1. [转载]yarn的安装和使用

    yarn的安装和使用 2018-08-02 10:45:41 yw00yw 阅读数 50696 文章标签: yarn 更多 分类专栏: 工具   版权声明:本文为博主原创文章,遵循CC 4.0 BY- ...

  2. mysql查看当前所有数据库中的表大小和元信息information_schema

    查看所有mysql数据库表和索引大小 mysql查看当前所有的数据库和索引大小 ,),' mb') as data_size, concat(,),'mb') as index_size from i ...

  3. JMeter性能测试工具

    1.官网资源 地址:https://jmeter.apache.org/download_jmeter.cgi window下载zip版本 options配置-选择语言-简体(jmeter.prope ...

  4. 富文本编辑器粘贴word

    很多时候我们用一些管理系统的时候,发布新闻.公告等文字类信息时,希望能很快的将word里面的内容直接粘贴到富文本编辑器里面,然后发布出来.减少排版复杂的工作量. 下面是借用百度doc 来快速实现这个w ...

  5. [Luogu] 线段树 2

    https://www.luogu.org/problemnew/show/P3373 双懒标记下放 先乘后加 #include <bits/stdc++.h> using namespa ...

  6. 【概率论】1-1:概率定义(Definition of Probability)

    title: [概率论]1-1:概率定义(Definition of Probability) categories: Mathematic Probability keywords: Sample ...

  7. Jumbled String (Kattis - jumbledstring)(思维题)

    Problem Recall that a subsequence of a string is any string obtained by removing some subset of char ...

  8. 十一、FHS基础原理

      文件系统: http://note.youdao.com/noteshare?id=298f02714da5b9483429a40dda667f35&sub=6120396419BA477 ...

  9. while 循环 continue break 用法例子

    py2 temp = "理解" # utf- 8 #解码, 需要指定原来的是什么编码 temp_unicode = temp.decode("utf-8") # ...

  10. java试题复盘——9月26日

    5.在 JAVA 编程中, Java 编译器会将 Java 程序转换为(A) A.  字节码 B.  可执行代码 C.  机器代码 D.  以上都不对 解析: 编译器将Java源代码编译成字节码cla ...