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 ...
随机推荐
- HashMap图解
HashMap的数据结构和put.get.resize等操作的图解,看图轻松掌握HashMap (目前还不包括红黑树相关的部分) HashMap数据结构如下图 HashMap之put操作如下图 Has ...
- 获取class
使用原生JavaScript,获取类操作符时:即使使用getElementByClassName,在Firefox和IE9以下是不兼容的.Firefox下是可以用它获取的到元素而IE不行,一般框架都会 ...
- 大数据学习——scala入门程序
安装scala.msi https://blog.csdn.net/sinat_32867867/article/details/80305302 notepad++ object HelloScal ...
- Git x SVN rebase事故
Git x SVN rebase事故 @author ixenos 2019-01-09 14:21:21 前言: 昨天在Git x SVN 中进行git svn dcommit的时候,提示需要再进行 ...
- 贴一下我写过的c++程序代码
5258 #include <iostream>#include <iomanip>#include <cmath>using namespace std;clas ...
- 使用python实现简单的爬虫
python爬虫的简单实现 开发环境的配置 python环境的安装 编辑器的安装 爬虫的实现 包的安装 简单爬虫的初步实现 将数据写入到数据库-简单的数据清洗-数据库的连接-数据写入到数据库 开发环境 ...
- 周赛Problem 1108: 蛋糕(二分)
1108: 蛋糕 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 17 Solved: 4 Description 杨神打代码打得有点疲倦,于是他想要 ...
- BZOJ 1015: [JSOI2008]星球大战starwar【并查集】
题目可以表述成:给定一个无向图G,每次删除它的一个点和与点相关的边集,每次询问该操作后图G的连通度(连通分量的个数).和上一题一样都是考察逆向思维,虽然删除点的做法不会,但是每次加点后询问连通度却是并 ...
- 阿里 AndFix 热修复框架简介
阿里AndFix热修复框架简介 热修复原理: Android的类加载机制 Android的类加载器分为两种,PathClassLoader和DexClassLoader,两者都继承自BaseDexCl ...
- uva 10870 递推关系矩阵快速幂模
Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...