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

先找第一串第一个字符与第二串的长度, 再找第一串前两个字符与第二串的最长公共子序列长度, 以此类推。 以abcfb 与 abfcab为例,附空间辅助变化示意图,求这两串的最长公共子序列 图中设置的是二维数组,格内的数为行列号,比如,第一个00表示第0行第0列, 由图可以很清晰的看出表达式: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])(a[i]!=b[j]); 当a[i]==b[j]时,长度为 二维数组[i-1][j-1]+1, 不相等时为上格子与前一格子最大值。



代码如下

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1000;
int dp[maxn][maxn];
int main()
{
string x,z;
int max1,lx,lz;
while(cin>>x>>z)
{
memset(dp,0,sizeof(dp));
max1=0;
lx=x.size(),lz=z.size();
for(int i=1;i<=lx;i++)
{
for(int t=1;t<=lz;t++)
{
dp[i][t]=max(dp[i][t-1],dp[i-1][t]);
if(x[i-1]==z[t-1]) dp[i][t]=dp[i-1][t-1]+1;
if(dp[i][t]>max1) max1=dp[i][t];
}
}
cout<<max1<<endl;
}
return 0;
}

HDU 1159.Common Subsequence【动态规划DP】的更多相关文章

  1. HDU 1159 Common Subsequence【dp+最长公共子序列】

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

  2. HDU 1159 Common Subsequence 动态规划

    2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...

  3. HDU 1159 Common Subsequence (dp)

    题目链接 Problem Description A subsequence of a given sequence is the given sequence with some elements ...

  4. hdu 1159 Common Subsequence (dp乞讨LCS)

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

  5. HDU 1159——Common Subsequence(DP)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题解 #include<iostream> #include<cstring> ...

  6. HDU 1159 Common Subsequence

    HDU 1159 题目大意:给定两个字符串,求他们的最长公共子序列的长度 解题思路:设字符串 a = "a0,a1,a2,a3...am-1"(长度为m), b = "b ...

  7. HDU 1159 Common Subsequence 最长公共子序列

    HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...

  8. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

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

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

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

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

随机推荐

  1. MyBatis(十):Mybatis缓存的重要内容

    本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接 https://space.bilibili.com/95256449?spm_id_from=33 ...

  2. Tcl编成第二天,set与unset

    代码如下: #!/usr/bin/tclsh set value "one" puts $value unset value puts $value set表示创建一个变量第一个参 ...

  3. 2020 PHP 初级 / 基础面试题,祝你金三银四跳槽加薪 (适合基础不牢固的 PHPer)

    1.PHP 语言的一大优势是跨平台,什么是跨平台? PHP 的运行环境最优搭配为 Apache+MySQL+PHP,此运行环境可以在不同操作系统(例如 windows.Linux 等)上配置,不受操作 ...

  4. Python Requests-学习笔记(11)-请求与响应对象

    任何时候调用requests.*()你都在做两件主要的事情.其一,你在构建一个 Request 对象, 该对象将被发送到某个服务器请求或查询一些资源.其二,一旦 requests 得到一个从 服务器返 ...

  5. sql 系统表协助集合

    一.判断字段是否存在: select * from syscolumns where id=object_id('表') and name='字段'

  6. windows/linux下如何更换Python的pip源

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:xlixiaohui PS:如有需要Python学习资料的小伙伴可以 ...

  7. L11注意力机制和Seq2seq模型

    注意力机制 在"编码器-解码器(seq2seq)"⼀节⾥,解码器在各个时间步依赖相同的背景变量(context vector)来获取输⼊序列信息.当编码器为循环神经⽹络时,背景变量 ...

  8. Extended Traffic LightOJ - 1074 (经典SPFA问题)

    题目大意:每一个城市都有一定的繁荣度,然后给出m条有向边i->j,定义这条边的权值为pow(arr[j]-arr[i],3),然后给你q个询问,每个询问输入一个x. 然后问你点1到x的距离,如果 ...

  9. reactnavigation 5.x简单例子

    随着RN和reactnavigation的版本更新,网上很多老版的例子都不能用了. 自己摸索着跑通了一些简单的功能. 使用的是最新的    "react-native": &quo ...

  10. 今天我们来讨论一下CSS3属性中的transition属性;

    transition属性是CSS3属性:顾名思义英文为过渡的意思:主要有四个值与其一一对应:分别是property(CSS属性名称),duration过渡的时长,timimg-function转速曲线 ...