LeetCode: Palindrome Partition
LeetCode: Palindrome Partition
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"]
]
地址:https://oj.leetcode.com/problems/palindrome-partitioning/
算法:可以用动态规划来解决。用一个二维数组dp来存储所有子问题的解,一维数组dp[i]用来存储0~i的字串的所有解,其中每个解用最后一个回文开始位置来标记。比如对于上面的字符串“aab”,
dp[0]={0},dp[1]={0,1},dp[2]={2}。这样,在构造解的过程中就可以采用递归的方法,对dp[n-1]中的所有值dp[n-1][j]先递归构造出字串0~dp[n-1][j]-1,然后在加上dp[n-1][j]~n-1
字串。具体代码:
class Solution {
public:
vector<vector<string> > partition(string s) {
int n = s.size();
if (n < ) return vector<vector<string> >();
vector<vector<int> > dp(n);
dp[].push_back();
for (int i = ; i < n; ++i){
if(isPalindrome(s.substr(,i+))){
dp[i].push_back();
}
dp[i].push_back(i);
for (int j = i-; j >= ; --j){
if(isPalindrome(s.substr(j+,i-j))){
dp[i].push_back(j+);
}
}
}
return constructResult(s,dp,n);
}
bool isPalindrome(const string &s){
int len = s.size();
int n = len / ;
int i = ;
while(i < n && s[i] == s[len--i]) ++i;
return i == n;
}
vector<vector<string> > constructResult(string &s, vector<vector<int> > &dp,int n){
if (n < ){
return vector<vector<string> >();
}
vector<int>::iterator it = dp[n-].begin();
vector<vector<string> > result;
for (; it != dp[n-].end(); ++it){
if (*it == ){
vector<string> temp1;
temp1.push_back(s.substr(,n));
result.push_back(temp1);
continue;
}
vector<vector<string> >temp2 = constructResult(s,dp,*it);
vector<vector<string> >::iterator str_it = temp2.begin();
for(; str_it != temp2.end(); ++str_it){
str_it->push_back(s.substr(*it,n-(*it)));
result.push_back(*str_it);
}
}
return result;
}
};
第二题:
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.
地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/
算法:同样用动态规划来解决,但这次只要用一维数组dp来储存所有子问题。其中dp[i]表示字串0~i所用的最小cut,dp[i+1]=min{dp[j-1] | 0 =< j <= i+1 且 字串j~i+1是回文}。代码:
class Solution {
public:
int minCut(string s) {
int n = s.size();
if(n < ) return ;
vector<int> dp(n);
dp[] = ;
for(int i = ; i < n; ++i){
if(isPalindrome(s.substr(,i+))){
dp[i] = ;
continue;
}
int min_value = n;
for(int j = i-; j >= ; --j){
if(dp[j]+ < min_value){
if(isPalindrome(s.substr(j+,i-j))){
min_value = dp[j] + ;
}
}
}
dp[i] = min_value;
}
return dp[n-];
}
bool isPalindrome(const string &s){
int len = s.size();
int n = len / ;
int i = ;
while(i < n && s[i] == s[len--i]) ++i;
return i == n;
}
};
LeetCode: Palindrome Partition的更多相关文章
- Leetcode: Palindrome Partition I II
题目一, 题目二 思路 1. 第一遍做时就参考别人的, 现在又忘记了 做的时候使用的是二维动态规划, 超时加超内存 2. 只当 string 左部分是回文的时候才有可能减少 cut 3. 一维动规. ...
- [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,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【CF932G】Palindrome Partition(回文树,动态规划)
[CF932G]Palindrome Partition(回文树,动态规划) 题面 CF 翻译: 给定一个串,把串分为偶数段 假设分为了\(s1,s2,s3....sk\) 求,满足\(s_1=s_k ...
- LeetCode: Palindrome 回文相关题目
LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...
- 【CF932G】Palindrome Partition 回文自动机
[CF932G]Palindrome Partition 题意:给你一个字符串s,问你有多少种方式,可以将s分割成k个子串,设k个子串是$x_1x_2...x_k$,满足$x_1=x_k,x_2=x_ ...
- CF932G Palindrome Partition(回文自动机)
CF932G Palindrome Partition(回文自动机) Luogu 题解时间 首先将字符串 $ s[1...n] $ 变成 $ s[1]s[n]s[2]s[n-1]... $ 就变成了求 ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
随机推荐
- C++实现网格水印之调试笔记(六)—— 提取完成
昨天在修改了可以调试出来的错误之后,提取出的水印和嵌入的仍然相去甚远.这个时候我觉得有必要整理一下嵌入和提取的整个过程. 嵌入过程: Step1,嵌入的时候对网格的拉普拉斯矩阵L进行特征值分解,得到特 ...
- iOS开发常用输入校验
1.数字字符校验 #define NUMBERSPERIOD @"0123456789." - (BOOL)CheckInput:(NSString *)string { NSCh ...
- BufferedReader和BufferedWriter读写文件(转载)
http://375940084.blog.51cto.com/2581965/751040 1.创建Student类存储每个学生信息,属性(学号,姓名,出生日期,得分)2.从c:/test/stud ...
- HDU5727 Necklace
http://acm.hdu.edu.cn/showproblem.php?pid=5727 题意:n个珠子,每个珠子有阴阳两种属性,且阴的一定和阳的紧邻,排成一个环:m行,每行两个数,表示阳性x珠子 ...
- C#中的堆和栈
看到一篇讲堆和栈的文章,是我目前为止见到讲的最易懂,详细和深入的.我翻译成中文.以此总结. 原文=>C#Heap(ing) Vs Stack(ing) in .NET 在net framewor ...
- java StreamTokenizer使用
注意:用JAVA解题一般用Scanner类来进行输入,但对时间要求严格的题,用它可能会超时,我.解POJ1823的时候就遇到这样的问题,后改用StreamTokenizer类进行输入,就过了.看来后者 ...
- Apache Spark GraphX的使用简介
类似 Spark 在 RDD 上提供了一组基本操作符(如 map, f ilter, reduce), GraphX 同样也有针对 Graph 的基本操作符,用户可以在这些操作符传入自定义函数和通过修 ...
- HDU 5792 World is Exploding (树状数组)
World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- linux下面的查找命令
在linux下面经常用查找命令,我自己最常用的是find whereis locate 关于find 我常用find的基本功能,如 find / -name filename 在某个目录下寻找文件. ...
- ActiveX控件的Events事件
http://labview360.com/article/info.asp?TID=10152&FID=165 Active X函式库 对使用LabVIEW作为开发环境的开发人员来说,如果能 ...