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 ...
随机推荐
- 记我的小网站发现的Bug之一 —— 某用户签到了两次
1.故事背景 今天上午我忙完手中的事情之后突然想起来我还没签到,于是赶紧打开签到页面,刚点击了签到按钮,提示"签到成功,获得25阅读额度!",正准备退出浏览器,忽然发现签到列表有异 ...
- R语言基础-data.frame
data.frame比较像表格,每一列是一个向量,即每列中的元素是同一类型:所有列具有相同的长度. x = 10:1 y = -4:5 q = c("Ha","oh&qu ...
- HDU 4283 区间DP You Are the One
题解 我使用记忆化搜索写的.
- linux 环境下bash脚本中找不到命令
mr.sh: line 1: HADOOP_CMD: command not found mr.sh: line 4: INPUT_FILE_PATH: command not found mr.sh ...
- Spring 结构
Spring框架主要由7大模块组成,它们提供了企业级开发需要的所有功能,而且每个模块都可以单独使用,也可以和其它模块组合使用,灵活且方便的部署可以使开发的程序更加简单灵活. 核心模块 Spring C ...
- HDU 3947 River Problem
River Problem Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ...
- Codeforces Round #204 (Div. 2)
D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- hdu3038 How Many Answers Are Wrong
TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always wo ...
- jvm gc回收机制
jdk8取消永久代,增加metaspace元空间.使用本地内存,不使用堆内存. jstat -gcutil jstat -gccause pid 1 每格1毫秒输出结果jstat -gccause p ...
- 【Hihocoder1034】毁灭者问题(splay,树状数组)
题意: 假设你拥有 n 个魔法单位,他们从左到有站在一行,编号从 1 到 n. 每个单位拥有三项属性: si: 初始法力. mi: 最大法力上限. ri: 每秒中法力回复速度. 现在你操纵一个毁灭者, ...