求最长公共子序列-DP问题
Longest common subsequence problem
The longest common subsequence (LCS) problem is the problem of finding the longest subsequence common to all sequences in a set of sequences (often just two sequences). It differs from the longest common substring problem: unlike substrings, subsequences are not required to occupy consecutive positions within the original sequences.
Example
- LCS for input Sequences
ABCDGHandAEDFHRisADHof length 3. - LCS for input Sequences
AGGTABandGXTXAYBisGTABof length 4
以s1={1,3,4,5,6,7,7,8},s2={3,5,7,4,8,6,7,8,2}为例。
1.构建二维矩阵A[n+1][n+1] s1为列,s2为行
(1)A[0][any]=0,A[any][0]=0
(2)if(s1[columnIndex-1]==s2[rowIndex-1])则A[rowIndex][columnIndex]=A[rowIndex-1][columnIndex-1]+1;
else A[rowIndex][columnIndex]=max(A[rowIndex][columnIndex-1],A[rowIndex-1][columnIndex])
(3)由A[8][9]可知最大子序列长度。
图如下:

2.针对构建的二维矩阵求最长子序列
(1)当columnIndex>0或者rowIndex>0时。
(2)如果s1[columnIndex-1]==s2[rowIndex-1]则longestSequence.unshift(s1[columnIndex-1]).rowIndex--;columnIndex--.
(3)如果不等,
则1.如果A[rowIndex][columnIndex]==A[rowIndex][columnIndex-1];columnIndex--;//向左
2.否则 rowIndex--;

代码如下:
/**
* @param {string[]} set1
* @param {string[]} set2
* @return {string[]}
*/
export default function longestCommonSubsequence(set1, set2) {
// Init LCS matrix.
const lcsMatrix = Array(set2.length + ).fill(null).map(() => Array(set1.length + ).fill(null)); // Fill first row with zeros.
for (let columnIndex = ; columnIndex <= set1.length; columnIndex += ) {
lcsMatrix[][columnIndex] = ;
} // Fill first column with zeros.
for (let rowIndex = ; rowIndex <= set2.length; rowIndex += ) {
lcsMatrix[rowIndex][] = ;
} // Fill rest of the column that correspond to each of two strings.
for (let rowIndex = ; rowIndex <= set2.length; rowIndex += ) {
for (let columnIndex = ; columnIndex <= set1.length; columnIndex += ) {
if (set1[columnIndex - ] === set2[rowIndex - ]) {
lcsMatrix[rowIndex][columnIndex] = lcsMatrix[rowIndex - ][columnIndex - ] + ;
} else {
lcsMatrix[rowIndex][columnIndex] = Math.max(
lcsMatrix[rowIndex - ][columnIndex],
lcsMatrix[rowIndex][columnIndex - ],
);
}
}
} // Calculate LCS based on LCS matrix.
if (!lcsMatrix[set2.length][set1.length]) {
// If the length of largest common string is zero then return empty string.
return [''];
} const longestSequence = [];
let columnIndex = set1.length;
let rowIndex = set2.length; while (columnIndex > || rowIndex > ) {
if (set1[columnIndex - ] === set2[rowIndex - ]) {
// Move by diagonal left-top.
longestSequence.unshift(set1[columnIndex - ]);
columnIndex -= ;
rowIndex -= ;
} else if (lcsMatrix[rowIndex][columnIndex] === lcsMatrix[rowIndex][columnIndex - ]) {
// Move left.
columnIndex -= ;
} else {
// Move up.
rowIndex -= ;
}
} return longestSequence;
}
求最长公共子序列-DP问题的更多相关文章
- HDU 4681 string 求最长公共子序列的简单DP+暴力枚举
先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...
- Java实现 LeetCode 583 两个字符串的删除操作(求最长公共子序列问题)
583. 两个字符串的删除操作 给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符. 示例: 输入: " ...
- [algorithm]求最长公共子序列问题
最直白方法:时间复杂度是O(n3), 空间复杂度是常数 reference:http://blog.csdn.net/monkeyandy/article/details/7957263 /** ** ...
- HDU 1243 反恐训练营 (动态规划求最长公共子序列)
反恐训练营 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- poj1458 求最长公共子序列 经典DP
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45763 Accepted: 18 ...
- 【dp】求最长公共子序列
[题目描述] 一个给定序列的子序列是在该序列中删去若干元素后得到的序列.确切地说,若给定序列X=<x1,x2,…,xm>X=<x1,x2,…,xm>,则另一序列Z=<z1 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LCS最长公共子序列~dp学习~4
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 Palindrome Time Limit: 4000/2000 MS (Java/Others ...
- POJ 1458 最长公共子序列(dp)
POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...
随机推荐
- Struts 2 的常规配置
Struts 2 的默认配置文件是struts.xml,该文件应该放在Web应用的类加载路径下,通常就是放在WEB-INF/classes路径下. struts.xml文件的最大作用是配置Action ...
- javaweb学习——会话技术(二)
文中部分借鉴了:https://www.cnblogs.com/xdp-gacl/p/3855702.html https://blog.csdn.net/p744174529/article/det ...
- 吴裕雄--天生自然ShellX学习笔记:Shell printf 命令
printf 命令模仿 C 程序库(library)里的 printf() 程序. printf 由 POSIX 标准所定义,因此使用 printf 的脚本比使用 echo 移植性好. printf ...
- 神奇的Python代码
一 def f(arg=i): print(arg) i = 6 f() i = 7 f(i) 输出结果是: 7 7
- 使用Anaconda安装TensorFlow
conda create -n tensorflow python=2.7 # or python=3.3, etc. pip install --ignore-installed --upgrade ...
- c++语法(3)
子类覆盖父类的成员函数: #include "stdafx.h" #include "iostream" class CAnimal { protected: ...
- Faraday Future,FF2019年一季度前完成第一阶段5亿美元左右的A+轮融资,2019年年底前完成7亿美元的Pre-IPO轮融资,2020IPO
FF2019年一季度前完成第一阶段5亿美元左右的A+轮融资,2019年年底前完成7亿美元的Pre-IPO轮融资,2020IPO 区块链公司先行宣布将对FF进行投资.EVAIO(中文名:伊娃)公司 跨链 ...
- 架构之道(3) - 令後端的吐血和喊FUCK的次数锐减
「那个产品经理不会技术,整天在需求,真操他妈的.」 这是很多产品经理遇到的一句话,如果你把顾客阶段完成了,回到自己的团队,遇到个技术大牛这麽说,那就表示,自己作为产品经理的功力还不够. 等我慢现解释, ...
- 使用迅为itop4418开发板创建Android模拟器
基于迅为iTOP-4418开发部在 Eclipse 中,单击“Windows”菜单,选择“Android Virtual Device Manager”启动 模拟器管理插件.然后如下图,单击“Crea ...
- bzoj1396识别子串(SAM+线段树)
复习SAM板子啦!考前刷水有益身心健康当然这不是板子题/水题…… 很容易发现只在i位置出现的串一定是个前缀串.那么对答案的贡献分成两部分:一部分是len[x]-fa~len[x]的这部分贡献会是r-l ...