当前位置:网站首页>分割回文串[dp + dfs组合]
分割回文串[dp + dfs组合]
2022-06-29 17:34:00 【REN_林森】
dp + dfs组合
前言
动态规划,空间换时间,比如一个字符串是否为回文,看它中间部分是否为回文和两端是否相等即可。
DFS求组合,经典的回溯场景。
一、分割回文串

二、预处理DP + DFS组合
package everyday.dp;
import java.util.ArrayList;
import java.util.List;
// 分割回文串
public class Partition {
/* target:外层目标:得到回文组合,可DFS,但是内层需要知道那个区间是回文,才能回文组合,所以 预处理是否为回文 + DFS进行组合。 */
public List<List<String>> partition(String s) {
// 预处理各种字串是否为回文。
boolean[][] f = preProcess(s);
// dfs组合
List<List<String>> ans = new ArrayList<>();
List<String> el = new ArrayList<>();
dfs(s, 0, el, ans, f);
// 返回处理好的组合。
return ans;
}
// DFS 组合所有可能的回文串。
private void dfs(String s, int cur, List<String> el, List<List<String>> ans, boolean[][] f) {
if (cur == s.length()) {
// 合法走到终点,不仅递归结束,且将合法组合放入ans
ans.add(new ArrayList<>(el));
return;
}
// 标准回溯
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);
}
}
// 这里是没有合法的走到终点的组合。不用ans.add,并结束递归。
}
// 判定区间[i,j]是否为回文。
private boolean[][] preProcess(String s) {
int n = s.length();
boolean[][] f = new boolean[n][n];
for (int j = 0; j < n; j++) {
// 每个单独的字母都是回文。
f[j][j] = true;
for (int i = j - 1; i >= 0; i--) {
// f[i][j] 是不是回文,要看s[i] == s[j] 且f[i - 1][j - 1]是回文。
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");
}
}
总结
1)理清楚,外层需要什么,想搭起外层,又需要什么内层。自外而内的分析,自内而外的解掉子问题。
2)动态规划/DFS组合。
参考文献
[1] LeetCode 分割回文串
边栏推荐
- Multi mode concurrent implementation of tortoise and rabbit race in go language
- Inheritablethreadlocal resolves message loss during message transmission between parent and child threads in the thread pool
- C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
- Use SSH to pull codes
- How to solve the 2003 error of MySQL in Linux
- About Kali using xshell connection
- 外部自动(PLC启动机器人)
- R language uses user-defined functions to write deep learning linear activation functions and visualize linear activation functions
- PancakeSwap技术:夹子机器人系统开发原理
- Browser large screen capture
猜你喜欢
随机推荐
Redis 原理 - Sorted Set (ZSet)
Bags of Binary Words for Fast Place Recognition in Image Sequenc
一次采集JSON解析错误的修复
Viewing splitchunks code segmentation from MPX resource construction optimization
反射
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
力扣今日题-535. TinyURL 的加密与解密
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用exp函数和coef函数获取模型所有变量的事件密度比(Incidence Density Ratio,IDR)并解读
Online text digit recognition list summation tool
关于harbor私有仓库忘记登录密码
DevCloud加持下的青软,让教育“智”上云端
How to solve MySQL 1045 error in Linux
linux中mysql 1045错误如何解决
What are the usage scenarios for locks in MySQL
How to solve the 2003 error of MySQL in Linux
windows平台下的mysql启动等基本操作
OpenFeign使用步骤 轮询策略与权重 log4j使用 openFeign拦截器的配置
育润多维发力慈善领域,勇抗企业公益大旗
自学结构体(小甲鱼c语言)
What role does the supply chain management system play in the supply chain scenario?









