当前位置:网站首页>分割回文串[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 分割回文串
边栏推荐
猜你喜欢

Mysql中锁的使用场景是什么

【现代信号处理第六次作业】

力扣每日一题 06.29 两数相加

Viewing splitchunks code segmentation from MPX resource construction optimization

DevCloud加持下的青软,让教育“智”上云端

0 basic self-study STM32 (wildfire) -- use register to light LED -- Explanation of GPIO function block diagram

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

Subgraphs in slam

Tencent cloud released orbit, an automated delivery and operation and maintenance product, to promote enterprise applications to be fully cloud native
![[R language data science]: Text Mining (taking Trump's tweet data as an example)](/img/4f/09b9885915bee50fb40976a5002730.png)
[R language data science]: Text Mining (taking Trump's tweet data as an example)
随机推荐
基于STM32F103ZET6库函数串口实验
mysql数据库扫盲,你真的知道什么是数据库嘛
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses exp function and coef function to ob
mysql游标的作用是什么
一次采集JSON解析错误的修复
2022 software evaluator examination outline
力扣每日一题 06.29 两数相加
卷妹带你学jdbc—2天冲刺Day1
Custom handlerinterceptor interceptor for user authentication
R语言epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息、aggregate.data.frame函数包含缺失值的情况下分组统计结果为NA
SRM供应商协同管理系统功能介绍
Sectigo ov pan domain name certificate is 1590 yuan a year easy to use
2020版KALI安装教程
Error:Connection refused: connect
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
腾讯云发布自动化交付和运维产品Orbit,推动企业应用全面云原生化
如何使用B/S开发工具DevExtreme的图表控件 - 自定义轴位置?
Issue 42: is it necessary for MySQL to have multiple column partitions
Epoll analysis
关于日期相加减问题