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 ...
随机推荐
- windows下升级pip失败,重新安装pip最新版本
环境: python3.6.5 32bit,后改为python3.4.3 32bit pycharm2018旗舰版 问题: pycharm里的pip一直无法升级到10.0.1版本,在cmd中使用升级命 ...
- (转)ios应用导航模型
Eko - MoboCentre 本文将介绍iPhone的导航风格,同时,也一并了解能够组织好应用内容和工具的导航方式.对于一个应用来说,最基础的操作就是基于页面间简单的移动,每张页面都完成一个任务或 ...
- WIN10配置instantclient
在PLSQL Developer目录下建立如下bat文件,替换其快捷方式,启动PLSQL Developer: @echo off set path=C:\instantclient-basic-nt ...
- 【04】在webstorm里Export declarations are not supported by current JavaScript version
[04]在webstorm里Export declarations are not supported by current JavaScript version Export declara ...
- luogu2568 GCD
先筛法求出 \([1,n]\) 间的素数,然后枚举每个素数.可以发现,对于每个素数 \(x\),它的贡献是 \([1,\lfloor n/x \rfloor]\) 间的有序互质对数. 我们钦定 \(( ...
- Etree方式解析xml知识积累
movies.xml: <collection shelf="New Arrivals"> <movie title="Enemy Behind&quo ...
- 面试准备——rpc面试题
https://www.jianshu.com/p/28e48e5f9c73 1 什么是 RPC ? RPC (Remote Procedure Call)即远程过程调用,是分布式系统常见的一种通信方 ...
- pytorch使用过程中遇到的一些问题
问题一 ImportError: No module named torchvision torchvison:图片.视频数据和深度学习模型 解决方案 安装torchvision,参照官网 问题二 安 ...
- conda管理包
清理 conda clean --lock --tarball --package (安装包) 安装或更新包 conda install numpy=1.9.3 conda update numpy= ...
- Linux 下运行 C++ 程序出现 “段错误(核心已转储)”
Linux下写C++程序出现“段错误(核心已转储)”的问题: 段错误一般就是指访问的内存超出了系统所给这个程序的内存空间,通常这个值是由gdtr来保存的,他是一个48位的寄存器,其中的32位是保存由它 ...