当前位置:网站首页>分割回文串[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 分割回文串
边栏推荐
- SAAS 服务的优势都有哪些
- 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
- 在线SQL转CSV工具
- Browser large screen capture
- What is the MySQL query view command
- 0 basic self-study STM32 (wildfire) - register lit LED
- 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
- The fixed assets management system enables enterprises to dynamically master assets
- Mysql中锁的使用场景是什么
- 【Try to Hack】Cookie和Session
猜你喜欢

High landing pressure of "authorization and consent"? Privacy computing provides a possible compliance "technical solution"

手把手教你在windows上安装mysql8.0最新版本数据库,保姆级教学

Online text digit recognition list summation tool

mysql. What is the concept of sock

mysql支持外键吗

0基础自学STM32(野火)——寄存器点亮LED

如何使用B/S开发工具DevExtreme的图表控件 - 自定义轴位置?

PCB板框的绘制——AD19

基于gis三维可视化的智慧城市行业运用

SRM供应商协同管理系统功能介绍
随机推荐
Redis bloom filter and cuckoo filter
如何使用B/S开发工具DevExtreme的图表控件 - 自定义轴位置?
0基础自学STM32(野火)——使用寄存器点亮LED——GPIO功能框图讲解
序列检测器
R语言ggplot2可视化:使用patchwork包(直接使用加号+)将一个ggplot2可视化结果和一个plot函数可视化结果横向组合起来形成最终结果图
2022 software evaluator examination outline
How to solve the 2003 error of MySQL in Linux
Openfeign use step polling strategy and weight log4j configuration of openfeign interceptor
R language uses user-defined functions to write deep learning leaky relu activation functions and visualize leaky relu activation functions
机器人不需要保养和出界也能拿金牌是一样一样的
在供应链场景应用中,供应链管理系统扮演什么角色?
R语言使用自定义函数编写深度学习线性激活函数、并可视化线性激活函数
PCB frame drawing - ad19
Kali installation tutorial 2020
剖析下零拷贝机制的实现原理,适用场景和代码实现
基于gis三维可视化的智慧城市行业运用
开源仓库贡献 —— 提交 PR
windows平台下的mysql启动等基本操作
LeetCode 每日一题——535. TinyURL 的加密与解密
第42期:MySQL 是否有必要多列分区