原题及翻译

A subsequence of a given sequence is the given sequence with some elements (possible none) left out.
给定序列的子序列是给定序列,其中遗漏了一些元素(可能没有元素)。
Given a sequence X = < x1, x2, …, xm >
给定序列x=<x1,x2,…,xm>
another sequence Z = < z1, z2, …, zk >
另一个序列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, x ij = zj.
是x的子序列,如果x的指数存在一个严格递增的序列<i1,i2,…,ik>,因此对于所有j=1,2,…,k,x ij=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 >.
例如,z=<a,b,f,c>是索引序列<1,2,4,6>的x=<a,b,c,f,b,c>的子序列。
Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
给定两个序列x和y,问题是求x和y的最大长度公共子序列的长度。

Input

The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.
程序输入来自标准输入。输入中的每个数据集包含两个表示给定序列的字符串。序列由任意数量的空格分隔。输入数据正确。

Output

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

思路

输入两个串s1,s2,设MaxLen(i,j)表示:
s1的左边i个字符形成的子串,与s2左边的j个字符形成的子串的最长公共子序列的长度(i,j从0开始算)
MaxLen(i,j) 就是本题的“状态”
假定 len1 = strlen(s1),len2 = strlen(s2)那么题目就是要求 MaxLen(len1,len2)
显然:
MaxLen(n,0) = 0 ( n= 0…len1)
MaxLen(0,n) = 0 ( n= 0…len2)
递推公式:
if ( s1[i-1] == s2[j-1] ) //s1的最左边字符是s1[0]
MaxLen(i,j) = MaxLen(i-1,j-1) + 1;
else
MaxLen(i,j) = Max(MaxLen(i,j-1),MaxLen(i-1,j) );
时间复杂度O(mn) m,n是两个字串

S1[i-1]!= s2[j-1]时,MaxLen(S1,S2)不会比MaxLen(S1,S2j-1) 和MaxLen(S1i-1,S2)两者之中任何一个小,也不会比两者都大。

代码分析

#include <iostream>
#include <cstring>
using namespace std;
char sz1[1000];
char sz2[1000];
int maxLen[1000][1000];
int main()
{while( cin >> sz1 >> sz2 ){int length1 = strlen( sz1);int length2 = strlen( sz2);int nTmp;int i,j;for( i = 0;i <= length1; i ++ )maxLen[i][0] = 0;for( j = 0;j <= length2; j ++ )maxLen[0][j] = 0;for( i = 1;i <= length1;i ++ ){for( j = 1; j <= length2; j ++ ){if( sz1[i-1] == sz2[j-1] )maxLen[i][j] = maxLen[i-1][j-1] + 1;elsemaxLen[i][j] = max(maxLen[i][j-1],maxLen[i-1][j]);}}cout << maxLen[length1][length2] << endl;}return 0;
}

Common Subsequence相关推荐

  1. 动态规划—最长公共子序列问题 HDU-1159 Common Subsequence

    动态规划-最长公共子序列问题 Common Subsequence [ HDU - 1159 ] A subsequence of a given sequence is the given sequ ...

  2. HD 1159 Common Subsequence (最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description A subsequence of a given seq ...

  3. C++longest common subsequence最长公共子序列的实现(附完整源码)

    C++longest common subsequence最长公共子序列 longest common subsequence最长公共子序列的完整源码(定义,实现,main函数测试) longest ...

  4. HDU 1159.Common Subsequence【动态规划DP】

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  5. HDU 1159 Common Subsequence 动态规划

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

  6. 【算法导论学习-29】动态规划经典问题02:最长公共子序列问题(Longest common subsequence,LCS)...

    2019独角兽企业重金招聘Python工程师标准>>> 问题描述:序列X={x1,x2,-,xn},Y={y1,y2,-,yn},当Z={z1,z2-,zn}是X的严格递增下标顺序( ...

  7. UVA10405 Longest Common Subsequence【LCS+DP】

    Given two sequences of characters, print the length of the longest common subsequence of both sequen ...

  8. CF346B Lucky Common Subsequence 题解

    CF346B Lucky Common Subsequence 题解 题目链接:CF346B Lucky Common Subsequence 题意:通过删除一个字符串中的某些元素而不改变其余元素的顺 ...

  9. 最长公共子串LCS (Longest Common Subsequence) 算法

    三个方法都有所借鉴,但代码部分是自己试着写出来的,虽然最后的运行结果都是正确的,但此过程中难免会有考虑不周全的地方,如发现代码某些地方有误,欢迎指正.同时有新的想法,也可以提出! 采用顺序结构存储串, ...

最新文章

  1. Ribbon_窗体_实现Ribbon风格的窗体
  2. 安利一个我爱不释手的PDF神器网站
  3. 03 HttpServletRequest_HttpServletResponse
  4. 【转】ABP源码分析三十七:ABP.Web.Api Script Proxy API
  5. ppt复制切片器_【PPT】高端人物活动介绍页PPT创意设计制作
  6. python的xml.dom学习笔记
  7. 解决IDEA中Maven加载依赖包慢的问题
  8. IP互动电视的坚强后盾
  9. Silverligth API for ArcGIS应用程序IIS发布
  10. Combinations leetcode 组合问题
  11. 删除数组中重复数字的算法
  12. springboot集成webservice接口及调用
  13. 如何用CMD查看电脑详细配置
  14. mysql逻辑模型的概念_概念模型、逻辑模型、物理模型区别?
  15. linux x200 黑屏,Thinkpad x200黑屏(一长两短) 复原
  16. springboot项目日志记录访问客户端ip地址
  17. python函数文档说明调用方式_调用函数方法
  18. rust建造一键升级_rust一键升级指令 | 手游网游页游攻略大全
  19. MySQL学习(一)
  20. 一文读懂Codex:基于Cosmos的跨链DeFi平台

热门文章

  1. 移动端0.5px的实现
  2. seajs打包部署工具spm的使用总结
  3. (原创).Net将EF运用于Oralce一 准备工作
  4. mysql auto position_MHA-Failover(GTID,Auto_Position=0)
  5. mysql主从数据库设计_mysql数据库主从库镜像原理及配置
  6. 今日头条适配方案_今日头条信息流广告创意优化方案!
  7. 服务器find寻找文件路径,Linux find 查找文件
  8. android n等分 layout,RecyclerView GridLayoutManager 等分间距
  9. bootstrap 文字加边框_word文档怎么加边框和底纹-给文档化个妆
  10. 视觉编码(Visual Encoding)