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 ...
随机推荐
- 简单把webdriver的find_element方法写成函数
__author__ = 'jyd' from selenium.webdriver.common.by import By #driver webdriver实例化对象 #element 查询元素的 ...
- Linux(CentOs)下安装Phantomjs + Casperjs
Linux(CentOs)下安装Phantomjs + Casperjs 是参照cnMiss's Blog http://ju.outofmemory.cn/entry/70691的博客进行安装的 1 ...
- TestNG官方文档中文版(2)-annotation(转)
1. 介绍 TestNG是一个设计用来简化广泛的测试需求的测试框架,从单元测试(隔离测试一个类)到集成测试(测试由有多个类多个包甚至多个外部框架组成的整个系统,例如运用服务器). 编写一个测试的 ...
- hive UDAF
java 程序 package com.ibeifeng.udaf; import org.apache.hadoop.hive.ql.exec.UDAF; import org.apache.had ...
- 对Spring的理解
1.Spring实现了工厂模式的工厂类,这个类名为BeanFactory实际上是一个接口,在程序中通常BeanFactory的子类ApplicationContext.Spring相当于一个大的工厂类 ...
- 如何在安裝SELinux的环境执行Quartus II
(原創) 如何在安裝SELinux的環境執行Quartus II? (SOC) (Quartus II) (Linux) (RedHat) Abstract一般人安裝Linux時,也會同時安裝SELi ...
- 百度,你家云管家能靠谱点不?替你脸红!Shame on you!
此文已提交百度云问题反馈, 坐等答复. 笔者最近下载某24+G分卷压缩文件, 24+G啊, 足足要下将近7个小时.满心欢喜的下载完却尼玛发现解压出错, 有6个文件无法解压?wrong password ...
- 三、python高级特性(切片、迭代、列表生成器、生成器)
1.python高级特性 1.1切片 list列表 L=['Mli','add','sal','saoo','Lkkl'] L[0:3] #即为['Mli','add','sal'] 从索引0开始 ...
- 定位程序问题的方法 -- clwu
原本的标题的<定位程序代码的方法>,但问题有时候超出了自己代码的范围,而是别人的程序,所以今天想分享的是一个通用的分析问题(程序)的思路. 先来说一下在使用别人的程序(Vim)过程中遇到问 ...
- 转:高性能Mysql主从架构的复制原理及配置详解
温习<高性能MySQL>的复制篇. 1 复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台 ...