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. 【高并发】你知道吗?大家都在使用Redisson实现分布式锁了!!

    写在前面 忘记之前在哪个群里有朋友在问:有出分布式锁的文章吗-@冰河?我的回答是:这周会有,也是[高并发]专题的.想了想,还是先发一个如何使用Redisson实现分布式锁的文章吧?为啥?因为使用Red ...

  2. CVPR 2020论文收藏(转知乎:https://zhuanlan.zhihu.com/p/112337176)

    CVPR 2020 共收录 1470篇文章,根据当前的公布情况,人工智能学社整理了以下约100篇,分享给读者. 代码开源情况:详见每篇注释,当前共15篇开源.(持续更新中,可关注了解). 算法主要领域 ...

  3. 区间dp入门+例题

    区间dp作为线性dp的一种,顾名思义是以区间作为阶段进行dp的,使用它的左右端点描述每个维度,决策往往是从小状态向大状态转移中推得的.它跟st表等树状结构有着相似的原理---向下划分,向上递推. dp ...

  4. zookeeper的下载安装和选举机制(zookeeper一)

    1. 简要概述 Zookeeper是一个开源的分布式的,为分布式应用提供协调服务的框架.Zookeeper从设计模式角度来理解:是一个基于观察者模式设计的分布式服务管理框架它负责存储和管理大家都关心的 ...

  5. STC15W串口通信的一些梳理

    由于控制串口1进行通信移植到串口3出现了阻力,因此很有必要对串口通信进行更进一步的梳理>>>> 一 STC15W串口对应引脚: 由此我们得到四个串口引脚分别为:串口1:P3 . ...

  6. 06-移动web之flex布局

    一.基本概念 flex布局又叫伸缩布局 .弹性布局 .伸缩盒布局 .弹性盒布局 Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. ...

  7. Mitmproxy教程

    本文是一个较为完整的 mitmproxy教程,侧重于介绍如何开发拦截脚本,帮助读者能够快速得到一个自定义的代理工具. 本文假设读者有基本的 python 知识,且已经安装好了一个 python 3 开 ...

  8. 【Canvas】(2)---绘制折线图

    绘制折线图 之前在工作的时候,用过百度的ECharts绘制折线图,上手很简单,这里通过canvas绘制一个简单的折线图.这里将一整个绘制过程分为几个步骤: 1.绘制网格 2.绘制坐标系 3.绘制点 4 ...

  9. Mysql 错误 Connection is read-only 解决方式

    环境Spring+Mybatis <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.s ...

  10. 落谷 P1734 最大约数和

    题目描述 选取和不超过S的若干个不同的正整数,使得所有数的约数(不含它本身)之和最大. 输入格式 输入一个正整数S. 输出格式 输出最大的约数之和. 输入输出样例 输入 #1复制 11 输出 #1复制 ...