palindrome-partitioning I&II——回文切割、深度遍历
I:
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"]
] 先切割,而后判断是否为回文。若是,则将剩余部分继续进行深度遍历。
class Solution {
public:
vector<vector<string>> partition(string s) {
if(s.length()<) return res;
dfs(s,);
return res;
}
void dfs(string s,int n){
int len=s.length();
if(n==len){
res.push_back(v);
return ;
}
for(int i=;i<len-n+;i++){
string tmp=s.substr(n,i);
if(isPalindrome(tmp)){
v.push_back(tmp);
dfs(s,n+i);
v.pop_back();
}
}
}
bool isPalindrome(string s){
int n=s.length();
int i=,j=n-;
while(i<j){
if(s[i]!=s[j])
return false;
i++;
j--;
}
return true;
}
vector<string> v;
vector<vector<string>> res;
};
II:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.
从后往前构造二维数组isPalin,用于存储已经确定的回文子串。isPalin[i][j]==true代表s[i,...,j]是回文串。
在构造isPalin的同时使用动态规划计算从后往前的最小切分数,记录在min数组中。min[i]代表s[i,...,n-1]的最小切分数。
(上述两步分开做会使得代价翻倍,容易TLE)
关键步骤:
1、min[i]初始化为min[i+1]+1,即初始化s[i]与s[i+1]之间需要切一刀。这里考虑边界问题,因此min数组设为n+1长度。
2、从i到n-1中间如果存在位置j,同时满足:(1)s[i,...,j]为回文串;(2)1+min[j+1] < min[i]。
那么min[i]=1+min[j+1],也就是说一刀切在j的后面比切在i的后面要好。
class Solution {
public:
int minCut(string s) {
int n=s.length();
if(n==) return ;
vector<vector<bool>> isPali(n,vector<bool>(n,false));
vector<int> min(n+,-);
for(int i=;i<n;i++){
isPali[i][i]=true;
}
for(int i=n-;i>=;i--){
min[i]=min[i+]+;
for(int j=i+;j<n;j++){
if(s[i]==s[j]){
if(j==i+||isPali[i+][j-]==true){
isPali[i][j]=true;
if(j==n-)
min[i]=;
else if(min[i]>min[j+]+)
min[i]=min[j+]+;
}
}
}
}
return min[];
}
};
palindrome-partitioning I&II——回文切割、深度遍历的更多相关文章
- 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串
131. Palindrome Partitioning Given a string s, partition s such that every substring of the partitio ...
- [Leetcode] palindrome partition ii 回文分区
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- leetcode之 Palindrome Partitioning I&II
1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...
- 【算法】leetcode之 Palindrome Partitioning I&II(转载)
1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- Palindrome(最长回文串manacher算法)O(n)
Palindrome Time Limit:15000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- [LeetCode] Find the Closest Palindrome 寻找最近的回文串
Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...
- Palindrome Partitioning I & II
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- Android开发工具——Gradle知识汇总
1.什么是构建工具 Eclipse大家都知道是一种IDE(集成开发环境),最初是用来做Java开发的,而Android是基于Java语言的,所以最初Google还是希望Android能在Eclipse ...
- python模块--random
random主要用于生成随机字符串等,例如登录页面上随机字符串验证. random常用方法: import random print(random.randrange(1, 10)) # 返回1-10 ...
- django基础(web框架,http协议,django安装)
学习Django之前我们先来看什么是OSI七层模型: 应用层 表示层 应用层(五层模型中把这三层合成一个应用层) http协议 会话层 传输层 提供端口对 ...
- luogu1129 [ZJOI2007]矩阵游戏
其实,只用考虑某一行能否放到某一行就行了 #include <iostream> #include <cstring> #include <cstdio> usin ...
- SPOJ - ADALIST,双端队列入门模板!
ADALIST - Ada and List 这道题时限6.5s,激动人心啊,好多人STL一顿乱写AC,哈哈,如果熟悉双端队列的话这道题其实是很水的. 题意:n个数的数列,然后接下来Q次操作,每次可以 ...
- 九度oj 题目1109:连通图
题目描述: 给定一个无向图和其中的所有边,判断这个图是否所有顶点都是连通的. 输入: 每组数据的第一行是两个整数 n 和 m(0<=n<=1000).n 表示图的顶点数目,m 表示图中边的 ...
- 九度oj 题目1256:找出两个只出现了一次的数字
题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 输入: 输入的第一行包括一个整数N(1<=N<=1000). 接下来的一行包括N个 ...
- RAISERROR 的用法(转)
raiserror 的作用: raiserror 是用于抛出一个错误.[ 以下资料来源于sql server 2005的帮助 ] 其语法如下: RAISERROR ( { msg_id | msg ...
- HDU 4609 3-idiots ——FFT
[题目分析] 一堆小木棍,问取出三根能组成三角形的概率是多少. Kuangbin的博客中讲的很详细. 构造一个多项式 ai=i的个数. 然后卷积之后去重. 统计也需要去重. 挺麻烦的一道题. #inc ...
- P1651 塔 (动态规划)
题目描述 小明很喜欢摆积木,现在他正在玩的积木是由N个木块组成的,他想用这些木块搭出两座高度相同的塔,一座塔的高度是搭建它的所有木块的高度和,并且一座塔至少要用一个木块.每个木块只能用一次,也可以不用 ...