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 ...
随机推荐
- 感知器Perceptron
Perceptron: 1.一种基于监督的线性分类器,其特点是:1)模型简单,具有很少的学习参数:2)具有可视性,一条直线即可划分:3)基于人工神经网络的原理. 其结构图为: 2.学习的关键技术: ...
- 安装nagios出现的两个错误记录
最近在安装nagios,出现几个错误记录: 一 检查nagios配置的时候出现错误如下: Warning: Duplicate definition found for host 'kelly' (c ...
- MVC中CheckBox
一.单个Checkbox 1.View文件 <%= Html.CheckBoxFor(model => model.IsNeverExpired)%> 2.生成的HTML为 < ...
- HDU-4738 Caocao's Bridges 边联通分量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:在有重边的无向图中,求权值最小的桥. 注意trick就好了,ans为0时输出1,总要有一个 ...
- (转)Http协议经典详解
转自:http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx Author :Jeffrey 引言 HTTP 是一个属于应用层的面向对象 ...
- UVALive 7456 Least Crucial Node (并查集)
Least Crucial Node 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/C Description http://7 ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- jquery easyui添加图标扩展
easyui中有很多通过iconCls="icon-reload"这样的属性引入小图标显示,当然我们也可以自己添加自己的小图标. 方式:1.我们可以在jquery easyui的文 ...
- Rop 文件上传解决思路
由于服务请求报文是一个文本,无法直接传送二进制的文件内容,因此必须采用某种转换机制将二进制的文件内容转换为字符串.Rop 采用如下的方式对上传文件进行编码:<fileType>@<B ...
- [置顶] 函数传递不定参数理解-c语言
感性认识 Typedef char *va_list;/*这个在<stdatg.h>中有定义*/ #define va_start(ap,p) (ap=(char*)(&(p)+1 ...