Palindrome Partitioning (回文子串题)
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
[
["aa","b"],
["a","a","b"]
] 题意理解:就是把一个字符串进行切割,要求切割之后的子串是回文串。 思路步骤:1.回文字符串划分 2.动态规划生成回文字符串数组 3.根据dp数组用深度搜索生成回文字符串的划分 简单描述一下,首先用动态规划的方法记录出dp[i][j]是否为回文子串(是为1,否则为0)。dp[i][j]表示字符串s中的索引从i....j的子串是不是回文字符串。 构造dp数组,当i=j时,dp[i][j]=1。 当i不等于j时,要求dp[i][j]只需当s[i]==s[j]且dp[i+1][j-1]=1来判断其余的即可。(i+1和j-1表示子串s[i...j]变为子串s[i+1...j-1],即去掉左右两边) 因此我们得反着来求dp,因为需要用到i+1. 然后根据生成好的dp数组,用dfs对数组进行划分。![]()
代码:
class Solution {
private:
int dp[][];
vector<vector<string>> result;
void dfs(string s, int begin,vector<string> temp) {
if(begin==s.length()) {
result.push_back(temp);
return;
}
for(int i=begin;i<s.length();i++) {
if(dp[begin][i]==) {
temp.push_back(s.substr(begin,i-begin+));
dfs(s,i+,temp);
temp.pop_back();
}
}
}
void dp_resolve(string s){
int n=s.size();
memset(dp,,sizeof(dp));
for (int i = n-; i >=; --i)
{
for (int j = i; j < n; ++j)
{
if(j==i){
dp[i][j]=;
}else if(j==i+){
if(s[i]==s[j]) dp[i][j]=;
}
else{
if(s[i]==s[j]&&dp[i+][j-]) dp[i][j]=;
}
}
}
vector<string> temp;
dfs(s,,temp);
return;
}
public:
vector<vector<string>> partition(string s) {
if(s.empty()) return result;
dp_resolve(s);
return result;
}
};
参考博文:http://blog.csdn.net/worldwindjp/article/details/22042133
http://blog.csdn.net/u011095253/article/details/9177451
类似题目:最长回文子串(Longest Palindromic Substring)
最长回文子序列
Palindrome Partitioning (回文子串题)的更多相关文章
- 131. Palindrome Partitioning(回文子串划分 深度优先)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【HDU】4632 Palindrome subsequence(回文子串的个数)
思路:设dp[i][j] 为i到j内回文子串的个数.先枚举所有字符串区间.再依据容斥原理. 那么状态转移方程为 dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+ ...
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning 回文串划分
题目链接 题意 给定一个字符串(长度\(\leq 2e5\)),将其划分成尽量少的段,使得每段内重新排列后可以成为一个回文串. 题解 分析 每段内重新排列后是一个回文串\(\rightarrow\)该 ...
- 后缀数组 - 求最长回文子串 + 模板题 --- ural 1297
1297. Palindrome Time Limit: 1.0 secondMemory Limit: 16 MB The “U.S. Robots” HQ has just received a ...
- #leetcode刷题之路5-最长回文子串
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1:输入: "babad"输出: "bab"注意: " ...
- 【算法】最长回文子串 longest palindrome substring
对于字符串S, 要找到它最长的回文子串,能想到的最暴力方法,应该是对于每个元素i-th都向左向右对称搜索,最后用一个数组span 记录下相对应元素i-th为中心的回文子串长度. 那么问题来了: 1. ...
- 【LeetCode每日一题 Day 5】5. 最长回文子串
大家好,我是编程熊,今天是LeetCode每日一题的第五天,一起学习LeetCode第五题<最长回文子串>. 题意 给你一个字符串 s,找到 s 中最长的回文子串. 示例 输入:s = & ...
- LeetCode随缘刷题之最长回文子串
这一题我用的相对比较笨的方法. 相对于大佬们用的动态规划法,比较复杂.但却更容易理解,我主要是通过记录下标来确定最长回文串的. package leetcode.day_12_06; /** * 给你 ...
随机推荐
- TFS2010单独安装配置tfs build server
记录一下确实很磨人. 同样硬件和软件环境的两台服务器,其中一台服务器很久之前就配置好了tfs2010 build ,然后最近想再配置一台tfs build server,但是按照以前的配置流程始终提示 ...
- 【HEVC简介】Inter Prediction Tools
参考文献:见<High Efficiency Video Coding (HEVC)>Inter-Picture Prediction in HEVC章节 <HEVC标准介绍.HEV ...
- 在Apache服务器中禁用option
在apache禁止 http OPTIONS方法. apache disable http OPTIONS method 2013-04-17 09:27 4050人阅读 评论(1) 收藏 举报 分 ...
- 洛谷 P1832 A+B Problem(再升级)
题目背景 ·题目名称是吸引你点进来的 ·实际上该题还是很水的 题目描述 ·1+1=? 显然是2 ·a+b=? 1001回看不谢 ·哥德巴赫猜想 似乎已呈泛滥趋势 ·以上纯属个人吐槽 ·给定一个正整数n ...
- C#飞行棋总结
以下是掷色子的一个代码,比较有代表性,里面的逻辑和内容都已注释,可通过注释了解这一方法的运作模式. public static void RowTouZi(int playerPos) //掷色子 { ...
- react开启一个项目 webpack版本出错
npx create-react-app my-app cd my-app npm start 在命令行里执行以上语句就可(前两天刚刚发现,最新版的react对webpack的版本要了新要求,大概是他 ...
- regular expression matching DP
这个题目,我从前天晚上(8月6号晚上)调试到现在(8月8号16:21),太心酸了,不好好总结一下,就太对不起自己了! 这是题目: Implement regular expression matchi ...
- 浅谈nodejs与npm
(1)npm介绍 在正式介绍Node.js学习之前,我们先认识一下npm. npm是什么东西?npm其实是Node.js的包管理工具(package manager). 为啥需要一个包管理工具呢?因为 ...
- Eclipse的PyDev插件安装及解决安装后找不到的问题
一.环境 windows 7 64bit eclipse 4.5.2 pydev jdk7u55 二.安装步骤 1. 安装JDK eclipse依赖于Java环境,所以需要安装Java运行环境JRE. ...
- centos7 firewalld日常使用
若生产中使用有docker,建议不要使用firewalld,改用iptables,用firewalld坑很多,暂时还未找到解决办法,在此做下记录: 说明:若加参数permanent为永久添加即添加至z ...
代码: