求最长公共子序列-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
ABCDGH
andAEDFHR
isADH
of length 3. - LCS for input Sequences
AGGTAB
andGXTXAYB
isGTAB
of 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 ...
随机推荐
- 静态页面缓存(thymeleaf模板writer)
//前端html <!DOCTYPE html><html lang="en"> <head> <meta charset="U ...
- 17.3.10--C语言运行的步骤
编译-->生成-->调试-->链接-->运行 编译就是:将你编写的C语言程序翻译成机器能识别运行的指令集 生成就是:根据编译完成的指令集制造出机器可以具体执行的指令序列 调试就 ...
- python学习笔记--数据类型和变量总结
1.数据类型 字符串 数字 列表 元祖 字典 2.可变不可变划分 可变:列表,字典 不可变:字符串,数字,元祖 举例:字符串,通过id查看字符串变量在内存中的地址.两次存的值不一样,这就说明了内存重新 ...
- 长沙中考2019数学T25讲解
好久没更Blog了... 为了应付完成寒假作业,还是更一下(再不更都庚子年了) Upd:2020.1.22 题目 第一问 还是比较水友好的 给顶点就相当于多给了对称轴-\(\frac{b}{2a}\) ...
- Python笔记_第三篇_面向对象_1.面向对象的基本概念和作用域
1. 软件编程的实质: 软件编程就是将我们的思维转变成计算机能够识别语言的一个过程.重要的是思想,代码技术反而次要.因此思想也是最难的,突破固定的思想是关键 2. 什么是面向过程: * 自上而下顺序执 ...
- ReportingService语法
="Dear All:"& vbcrlf & vbcrlf & IIF(First(Fields!ProductFamily.Value, "bc ...
- springboot学习笔记:9.springboot+mybatis+通用mapper+多数据源
本文承接上一篇文章:springboot学习笔记:8. springboot+druid+mysql+mybatis+通用mapper+pagehelper+mybatis-generator+fre ...
- vue路由的跳转-路由传参-cookies插件-axios插件-跨域问题-element-ui插件
---恢复内容开始--- 项目初始化 创建一个纯净的vue环境项目,手动书写全局的样式配置,全局的main,js配置 (1)如果vue项目在重构或者出错的时候,手动安装node_modules. 如果 ...
- 实现TabControl 选项卡首个标签缩进的方法
借用一张网图说明需求 在网上找了一圈,没有找到直接通过API或者重绘TabControl 的解决方法,最后灵机一动想到了一个折(tou)中(lan)的解决办法 Tab1.TabPages.Clear( ...
- JavaWeb过滤器(Filter)
参考:https://blog.csdn.net/yuzhiqiang_1993/article/details/81288912 原理: 一般实现流程: 1.新建一个类,实现Filter接口2.实现 ...