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. python学习之非空即为真,函数,集合

    python基础 非空即真,非零即真 #之前代码:sex = input("请输入性别:") if sex != '': print("输入成功") else: ...

  2. 使用Django 测试客户端一起测试视图,模板和URL

    Django 测试客户端 self.client.get(url)  测试客户端是Django中TestCase类的一个属性名. 至此已经在网站中可以创建一个清单了. 那么,现在是可以在unit te ...

  3. Pollard-Rho 总结

    将一个大数\(N\)分解质因子. 试除法,暴力枚举\(1-\sqrt{N}\)的数.时间复杂度:\(O(\sqrt{N})\). 通常,这个复杂度够了,但有时,\(N\leq10^{18}\). 这就 ...

  4. 001_STM32程序移植之_DS1302

    1. 测试环境:STM32C8T6 2. 测试模块:DS1302时钟模块 3. 测试接口: 1. DS1302模块接口: DS1302引脚 单片机引脚 VCC--------------------3 ...

  5. HTML的列表,表格与媒体元素

    一.无序列表 <ul>                            <li>无序列表</li>                            &l ...

  6. HashMap判断键是否为null

    用containsKey(),而不用get(): HashMap中,null可以作为键,这样的键只有一个:可以有一个或多个键所对应的值为null.当get()方法返回null值时,即可以表示HashM ...

  7. vue-ckeditor-word粘贴

    我司需要做一个需求,就是使用富文本编辑器时,不要以上传附件的形式上传图片,而是以复制粘贴的形式上传图片. 在网上找了一下,有一个插件支持这个功能. WordPaster 安装方式如下: 直接使用Wor ...

  8. date/clock/hwclock/cal

    date 显示日期与时间 date +%Y/%m/%d/%H:%M 2018/10/08/17:35 格式化输出 时间的设置 查看时区 date -R 时间戳转化 time1=$(date +%s - ...

  9. Python—“helloworld”

    接触一门计算机新语言,第一件事就是要准备好一个编译器用来打代码. 网上很多环境搭建的方法,具体参照https://www.runoob.com/python/python-install.html 由 ...

  10. python的协程,monkeyPatch

    monkey patch 一般指运行时候进行动态替换. 基本上我们使用gevent,会在最开头的地方加入gevent.monkey.patch_all();把标准库中的thread/socket等给替 ...