当前位置:网站首页>以动态规划的方式求解最长回文子串
以动态规划的方式求解最长回文子串
2022-06-28 07:10:00 【小的时候可菜了】
Dynamic Programming (DP) is an algorithmic technique for simplifying a complicated problem by breaking it down into simpler sub-problems in a recursive manner and utilizing the fact that the optimal solution to the overall problem depends upon the optimal solution to its subproblems.
以递归的方式将复杂问题分解为“更简单的子问题”:整体问题的最优解取决于其子问题的最优解
leetcode - 5. 最长回文子串
官方动态规划的题解写很抽象,没一个图片,看的差点怀疑智商,后看到如下视频,清楚多了,遂记录下来
使用【动态规划】求解最长回文子串
判断方式:首尾字符比较,之后去掉首尾字符,再比较现有首尾字符。单个字符一定是一个字串
暴力解法状态无法保留,比如[a,b,c,a]中,首尾字符相等,再比较[b,c],但[b,c]可能之间已经比较过了,现在又需要重新比较下
使用动态规划如下图,注意,这里的二维数组要理解为区间,如 [3,5] 为区间 [b,a,b]

var longestPalindrome = function (s) {
let n = s.length
let dp = new Array(n).fill(0).map(() => new Array(n).fill(true))
for (let i = n - 2; i >= 0; i--) {
// 第一个[b]一定为true,所以从[a,b]开始
for (let j = i + 1; j < n; j++) {
// 这里的二维数组理解为区间,如[3,5]为区间[b,a,b]
dp[i][j] = (s[i] == s[j]) && dp[i + 1][j - 1] // 起点和端点相同,并且内部字串起点和端点也相同,代表是回文子串 [i+1,j-1]代表内部
}
}
let maxstr = ""
for (let i = 0; i < n; i++) {
var maxIndex = dp[i].lastIndexOf(true)
if (maxIndex - i + 1 > maxstr.length) {
maxstr = s.slice(i, maxIndex + 1)
}
}
return maxstr
};
console.log(longestPalindrome("cabbab"))
当然,这里的动态规划时间空间复杂度都是 n 2 n^2 n2,并不是最优解,下面的文章中心扩散解释的还是不错的
中心扩散,暴力,动态规划,马拉车4种方式解决
最长公共子序列
状态转移方程如下
d p [ i ] [ j ] = { d p [ i − 1 ] [ j − 1 ] + 1 , a [ i ] = b [ j ] M a x ( d p [ i ] [ j − 1 ] , d p [ i − 1 ] [ j ] ) , a [ i ] ≠ b [ j ] dp[i][j]= \left\{ \begin{aligned} dp[i-1][j-1]+1,a[i]=b[j] & \\ Max(dp[i][j-1], dp[i-1][j]),a[i]\not=b[j] \end{aligned} \right. dp[i][j]={ dp[i−1][j−1]+1,a[i]=b[j]Max(dp[i][j−1],dp[i−1][j]),a[i]=b[j]
a [ i ] = b [ j ] a[i]=b[j] a[i]=b[j] 相等,添加字串长度,否则,取之前计算过的最大字串,如 [a,b,c,d],[b,a],尾字符不相等,则取[a,b,c,d],[b] 和 [a,b,c],[b,a]中最大字串

var longestCommonSubsequence = function (text1, text2) {
let dp = new Array(text1.length + 1).fill(0).map(() =>
new Array(text2.length + 1).fill(0)
)
for (let i = 1; i <= text1.length; i++) {
dp[i][0] = 0
}
for (let i = 1; i <= text2.length; i++) {
dp[0][i] = 0
}
for (let i = 1; i <= text1.length; i++) {
for (let j = 1; j <= text2.length; j++) {
if (text1[i - 1] == text2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1
} else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j])
}
}
}
return dp[text1.length][text2.length]
};
边栏推荐
- Puge -- three basic sorting, bubbling, selection and quickness
- 微信小程序编译页面空白bug的原因
- Compilation principles final review
- Trie string statistics
- 【网络教程】IPtables官方教程--学习笔记1
- MySQL installation steps - Linux configuration file JDK installation (II)
- 全方位透析真实企业软件测试流程
- Niubi 666, this project makes web page making as simple as building blocks
- Servlet value passing JSP
- Comprehensive analysis of real enterprise software testing process
猜你喜欢

Top 25 most popular articles on vivo Internet technology in 2021

Rn7302 three-phase electric quantity detection (based on STM32 single chip microcomputer)

Vivo browser rapid development platform practice - Overview

LeetCode+ 66 - 70 高精度、二分专题

Speech enhancement - spectrum mapping

【网络教程】IPtables官方教程--学习笔记1

Application and Optimization Practice of redis in vivo push platform

SQL statement optimization steps (1)

Practice of traffic recording and playback in vivo

编译原理期末复习
随机推荐
OPC 协议认识
[rust daily] published on rust 1.43.0 on April 23, 2020
什么是一致性哈希?可以应用在哪些场景?
What if the applet page is set to 100% height or left blank?
The practice of event driven architecture in vivo content platform
【Rust翻譯】從頭實現Rust异步執行器
CMAKE小知识
Detailed explanation of collection class methods____ (4) Judgment and assignment, etc
FPM tool installation
LLVM 与 Clang
Mysql8.0和Mysql5.0访问jdbc连接
Using interceptor and cache to complete interface anti brushing operation
小程序页面设置100%高度还是留白怎么办?
[rust translation] implement rust asynchronous actuator from scratch
Batch import of pictures into WPS table by date
7-1 懂的都懂
Extern "C" overview
东方财富上开户是安全的吗
7-2 芬兰木棋 结构体排序
Will Internet talents be scarce in the future? Which technology directions are popular?