当前位置:网站首页>Kickback record: dynamic programming 5 subsequence problem (3) palindrome - 647 palindrome subsequence, 516 longest palindrome subsequence
Kickback record: dynamic programming 5 subsequence problem (3) palindrome - 647 palindrome subsequence, 516 longest palindrome subsequence
2022-07-21 22:48:00 【Kiwi_ fruit】
This topic
- 647 Palindrome string
- 516 The longest palindrome subsequence
647 Palindrome string
- DP:
- Definition dp Array dp[i][j] Scope of representation i To j( Left and right closed ) Whether the substring in the palindrome substring , If so, it is true, Otherwise false;
- Recurrence formula if s[i] != s[j], Then for false, If s[i] == s[j], Discussion by situation , When i and j The difference is less than or equal to 1 when , by true, Otherwise, it is equal to the range i+1 To j-1( Left and right closed ) Result ;
- initialization dp[i][j] For all false;
- The traversal order is from bottom to top , From left to right , Prevent taking range i+1 To j-1 The result of is the initialization value ;
- give an example .
class Solution {
public int countSubstrings(String s) {
//DP
int leng = s.length();
// Definition dp Array dp[i][j] Scope of representation i To j( Left and right closed ) Whether the substring in the palindrome substring , If so, it is true, Otherwise false
// initialization dp[i][j] For all false
boolean[][] dp = new boolean[leng][leng];
// Define results
int result = 0;
// The traversal order is from bottom to top , From left to right , Prevent taking range i+1 To j-1 The result of is the initialization value
for(int i = leng - 1; i >= 0; i--){
for(int j = i; j < leng; j++){
if(s.charAt(i) == s.charAt(j)){
if(j - i <= 1){
dp[i][j] = true;
}else{
dp[i][j] = dp[i + 1][j - 1];
}
}
if(dp[i][j] == true) result++;
}
}
// Return results
return result;
}
}
- Double finger needling ( Less space complexity ):
class Solution {
public int countSubstrings(String s) {
// Double finger needling
int leng = s.length();
// Define results
int result = 0;
// Spread to both sides with one or two elements as the center
for(int i = 0; i < leng; i++){
// An element
result += judge(s, i, i, leng);
// Two elements
result += judge(s, i, i + 1, leng);
}
// Return results
return result;
}
// The double pointer spreads to both sides
private int judge(String s, int i, int j, int leng){
// Define results
int res = 0;
while(i >= 0 && j < leng && s.charAt(i) == s.charAt(j)){
res++;
i--;
j++;
}
return res;
}
}
516 The longest palindrome subsequence
- Compare with the previous question 647 Palindrome string , The palindrome subsequence of this question does not require continuity , But the idea is the same .
- DP:
- Definition dp Array dp[i][j] Representation string s In scope i To j( Left and right closed ) The length of the longest palindrome subsequence in is dp[i][j];
- Recurrence formula if s[i] == s[j], be dp[i][j] = dp[i + 1][j - 1] + 2, otherwise dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
- initialization dp[i][i] = 1;
- The traversal order is from bottom to top, left to right , Prevent taking range i+1 and j-1 Not assigned when ;
- give an example .
class Solution {
public int longestPalindromeSubseq(String s) {
//DP
int leng = s.length();
// Definition dp Array dp[i][j] Representation string s In scope i To j( Left and right closed ) The length of the longest palindrome subsequence in is dp[i][j]
int[][] dp = new int[leng][leng];
// initialization dp[i][i] = 1
for(int i = 0; i < leng; i++) dp[i][i] = 1;
// The traversal order is from bottom to top, left to right , Prevent taking range i+1 and j-1 Not assigned when
for(int i = leng - 1; i >= 0; i--){
for(int j = i + 1; j < leng; j++){
//j from i+1 Start , because j=i Has been initialized to 1 了
if(s.charAt(i) == s.charAt(j)){
dp[i][j] = dp[i + 1][j - 1] + 2; // When i and j The difference is 1 when i+1 and j-1 The exchange of upper and lower limits does not affect
}else{
dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
}
}
}
// Return results
return dp[0][leng - 1];
}
}
边栏推荐
- Probability theory - maximum likelihood estimation
- Cocos Creator 3.x 物理引擎使用笔记
- Fourth week ACM training report
- Verify Goldbach conjecture
- Seventh week ACM training report
- 226. Flip binary tree
- 108. Convert an ordered array into a binary search tree
- Redis主从复制的配置原理和过程
- 438. Find all letter ectopic words in the string
- 代码修改模型的渐隐和渐现
猜你喜欢
567. Arrangement of strings
Property analysis of matter protocol (II) multiple fabric and permission control
数据库初学笔记
How to complete the design of RGB lamp Bluetooth mesh module from 0 to 1
205. Isomorphic string
Playable Director (TimeLine) 3D游戏的开场动画制作
Merge sort
2021 popularization group summary
Expression evaluation
CocosCreator3.x 接入腾讯游戏联机对战引擎笔记
随机推荐
支持 CocosCreator 3.x 第三方库
unity 自定义工具之“执行bat文件”
Force deduction record: dynamic programming 1 basic topic - 509 Fibonacci number, 70 climbing stairs, 746 climbing stairs with minimum cost, 62 different paths, 63 different paths II, 343 integer spli
438. Find all letter ectopic words in the string
How to complete the design of RGB lamp Bluetooth mesh module from 0 to 1
Probability theory - maximum likelihood estimation
MySQL安装
P/np/np complete /np hard
Quick sort
数据库初学笔记
XML explanation
Chat about matter protocol (original chip protocol)
14. Longest common prefix
VisualStudio2019 配置点云库 PCL1.11.1+斯坦福兔子测试
Merge sort
每日升个级
567. Arrangement of strings
2022acm summer training weekly report (II)
2022ACM夏季集训周报(三)
029:陶陶摘苹果