求最长公共子序列-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 ...
随机推荐
- 2018.11.2JavaScript随笔
构造函数首字母大写 通过new创建对象 BOM:浏览器对象模型
- 吴裕雄--天生自然 JAVA开发学习:String 类
public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'r', 'u', 'n' ...
- 01 语言基础+高级:1-9 网络编程_day11【网络编程】
day11[网络编程] 主要内容 软件架构CS/BS 网络通信三要素 TCP通信 Socket套接字 ServerSocket 教学目标 能够辨别UDP和TCP协议特点 能够说出TCP协议下两个常用类 ...
- [Python] 关于__init__.py
当目录结构为下面这样└── utils/│ ├── __init__.py│ └── config.py├── test.py 每个文件夹下都有__init__.py,一个目录如果包含了__init_ ...
- CodeForces 992B Nastya Studies Informatics + Hankson的趣味题(gcd、lcm)
http://codeforces.com/problemset/problem/992/B 题意: 给你区间[l,r]和x,y 问你区间中有多少个数对 (a,b) 使得 gcd(a,b)=x lc ...
- LeetCode——15. 三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- 新服务器搭建-总结: 下载nginx,jdk8,docker-compose编排(安装mysql,redis) 附安装
三明SEO: 前言 如题, 公司新买了一条4核16G的服务器, 不得不重新搭建环境, 只能一一重来, 做个记录 1.nginx : 手动安装 2.jdk8: 手动安装 3. 安装docker 及doc ...
- TPO3-1Architecture
Much of the world's great architecture has been constructed of stone because of its beauty, permanen ...
- Review For Exam
Review For Exam [2019 福建省赛] 一个很简单的状态压缩DP,结果集体走偏 如何解决连续几日的限制问题?这种东西普通的DP很难写 #include <bits/stdc++. ...
- 安卓ButtomBar实现方法
这里ButtomBar有3个items,分别有icon和文字,在当前fragment时,所属的icon和文字会显示不同颜色. 1. 首先要准好ICON素材,命名规范要清楚. 2. 实现这个Buttom ...