当前位置:网站首页>Split palindrome string [dp + DFS combination]
Split palindrome string [dp + DFS combination]
2022-06-29 17:39:00 【REN_ Linsen】
dp + dfs Combine
Preface
Dynamic programming , Space for time , For example, whether a string is a palindrome , See whether the middle part is palindrome and whether the two ends are equal .
DFS Find combination , Classic backtracking scenes .
One 、 Split palindrome string

Two 、 Preprocessing DP + DFS Combine
package everyday.dp;
import java.util.ArrayList;
import java.util.List;
// Split palindrome string
public class Partition {
/* target: The outer goals : Get palindrome combination , can DFS, But the inner layer needs to know that the interval is palindrome , Ability palindrome combination , therefore Whether the preprocessing is a palindrome + DFS Are combined . */
public List<List<String>> partition(String s) {
// Preprocess whether various strings are palindromes .
boolean[][] f = preProcess(s);
// dfs Combine
List<List<String>> ans = new ArrayList<>();
List<String> el = new ArrayList<>();
dfs(s, 0, el, ans, f);
// Return the processed combination .
return ans;
}
// DFS Combine all possible palindrome strings .
private void dfs(String s, int cur, List<String> el, List<List<String>> ans, boolean[][] f) {
if (cur == s.length()) {
// The legal end , Not only does recursion end , And put the legal combination into ans
ans.add(new ArrayList<>(el));
return;
}
// Standard backtracking
for (int i = cur; i < s.length(); i++) {
if (f[cur][i]) {
el.add(s.substring(cur, i + 1));
dfs(s, i + 1, el, ans, f);
el.remove(el.size() - 1);
}
}
// There is no legal combination to reach the end . no need ans.add, And end recursion .
}
// Decision interval [i,j] Is it palindrome .
private boolean[][] preProcess(String s) {
int n = s.length();
boolean[][] f = new boolean[n][n];
for (int j = 0; j < n; j++) {
// Every single letter is a palindrome .
f[j][j] = true;
for (int i = j - 1; i >= 0; i--) {
// f[i][j] Is it a palindrome , Want to see s[i] == s[j] And f[i - 1][j - 1] It's palindrome. .
if (s.charAt(i) != s.charAt(j)) continue;
if (i + 1 == j || f[i + 1][j - 1]) f[i][j] = true;
}
}
return f;
}
public static void main(String[] args) {
new Partition().partition("aab");
}
}
summary
1) Clear understanding , What does the outer layer need , Want to put up an outer layer , What inner layer is needed . From the outside to the inside , Solve the subproblem from the inside out .
2) Dynamic programming /DFS Combine .
reference
边栏推荐
- [the sixth operation of modern signal processing]
- 固定资产管理系统让企业动态掌握资产情况
- L'intercepteur handlerinterceptor personnalisé permet l'authentification de l'utilisateur
- What is the SRM system? How do I apply the SRM system?
- 剖析下零拷贝机制的实现原理,适用场景和代码实现
- Pancakeswap Technology: development principle of gripper robot system
- What are the usage scenarios for locks in MySQL
- Mysql中锁的使用场景是什么
- 开源仓库贡献 —— 提交 PR
- Basic operations such as MySQL startup under Windows platform
猜你喜欢

Openfeign use step polling strategy and weight log4j configuration of openfeign interceptor

【WebDriver】使用AutoIt上传文件

selenium 文件上传方法

What are the usage scenarios for locks in MySQL

The soft youth under the blessing of devcloud makes education "smart" in the cloud

Basic operations such as MySQL startup under Windows platform

mysql在linux中2003错误如何解决

与爱同行,育润走进贫困家庭,助推公益事业

Redis principle - sorted set (Zset)

PCB frame drawing - ad19
随机推荐
How to solve the 2003 error of MySQL in Linux
Bags of Binary Words for Fast Place Recognition in Image Sequenc
mysql支持外键吗
linux中mysql 1045错误如何解决
Cross border independent station language Unicode to Hebrew
Master slave replication of MySQL
R language ggplot2 visualization: use the patchwork package (directly use the plus sign +) to horizontally combine the two ggplot2 visualization results, and then horizontally combine them with the th
KUKA robot external axis configuration what you must know
Use SSH to pull codes
OpenFeign使用步骤 轮询策略与权重 log4j使用 openFeign拦截器的配置
External automatic (PLC start robot)
剑桥大学教授:经常吃早餐害处多,很危险 - 知乎
The dplyr package filter function of R language filters the data in dataframe data through combinatorial logic (and logic). The content of one field is equal to one of the specified vectors, and the v
关于KALI使用xshell连接
Viewing splitchunks code segmentation from MPX resource construction optimization
填充每个节点的下一个右侧节点指针[利用好每个点->尽可能降低时空复杂度]
基于STM32F103ZET6库函数PWM输出实验
0 basic self-study STM32 (wildfire) - register lit LED
Walk with love, educate and run poor families, and promote public welfare undertakings
剖析下零拷贝机制的实现原理,适用场景和代码实现