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. 微信小程序 使用include导入wxml文件注意的问题

    (1)使用inlucde的时,要注意将最后的终止符 / 添加上去,否则不能正常的导入界面内容 <include src="header.wxml"/> (2)引入文件注 ...

  2. 7L-双线链表实现

    链表是基本的数据结构,尤其双向链表在应用中最为常见,LinkedList 就实现了双向链表.今天我们一起手写一个双向链表. 文中涉及的代码可访问 GitHub:https://github.com/U ...

  3. go 编译protobuf

    D:\project\bin\protoc.exe --plugin=protoc-gen-go=%GOPATH%\bin\protoc-gen-go.exe --go_out=. *.proto 编 ...

  4. 28.1 api-- Object(toString equals)

    /* * String toString() : 返回该对象的字符串表示 * return getClass().getName() + "@" + Integer.toHexSt ...

  5. json === dict

    import requests import json ''' json.loads(json_str) json字符串转换成字典 json.dumps(dict) 字典转换成json字符串 ''' ...

  6. matplotlib Bbox类

    Bbox 类是一个可变的(相对于BboxBase)限位框, 继承自BboxBase 2020-04-07 22:54:57  --Edit by yangray 方法: __init__(points ...

  7. Java的多线程编程模型5--从AtomicInteger开始

    Java的多线程编程模型5--从AtomicInteger开始 2011-06-23 20:50 11393人阅读 评论(9) 收藏 举报 java多线程编程jniinteger测试 AtomicIn ...

  8. 最长公共子窜和最长公共子序列(LCS)

    他们都是用dp做;复杂度都是O(N方) 有一个大佬的博客写的很详细,是关于最长公共子序列的:https://blog.csdn.net/hrn1216/article/details/51534607 ...

  9. Python-元组tuple、列表list、字典dict

    1.元组tuple(1)元组是有序列表,有不可见的下标,下标从0开始(2)元组的数据是相对固定的,数据不能增删改,他的一个重要用途是保存固定的.安全要求高的数据(3)元组用小括号()括起来,空元组定义 ...

  10. stand up meeting 12/22/2015 && 用户体验收录

    part 组员                工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云  完善页面切换,尝试子页面设计    4  完善页面切换和子页面 ...