LeetCode132:Palindrome Partitioning 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.
解题思路:
这题是参考网上大牛的,一开始我采用上一题Palindrome Partitioning解题思路,结果运行超时。
实现代码:
#include <iostream>
#include <vector>
#include <iterator>
#include <unordered_map>
#include <string>
#include <algorithm> using namespace std;
/*
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.
*/
class Solution {
public:
//DP
/*
如果if s[j...k] 为回文串,则 dp[k] = min{ dp[k], dp[j - 1] + 1} 0 <=j <= k - 1;
否则 dp[k] = dp[k - 1] + 1
*/
int minCut(string s){
if(s.size() == 0)
return -1;
//dp保存前i个字符串最小切割数
vector<int> dp(s.size() + 1, s.size()-1);//初始化最小切分数为s.size-1 //保存i—j是否为回文串
vector<vector<bool> > status(s.size(), vector<bool>(s.size(), false));
dp[0] = -1;
for(int i = 0; i < s.size(); ++i)
{ dp[i + 1] = dp[i] + 1;//假定,s[i]不能和其前面的字符串构成回文串,则s[i]为单独回文串即要切分
/*
检查s[i]能否和其前面的字符串构成回文串,如果可以,则更新dp[i+1]
*/
for(int cur = i - 1; cur >= 0; --cur)
if(s[i] == s[cur] && (i - cur <= 2 || status[cur + 1][i - 1])){
dp[i + 1] = min(dp[i + 1], dp[cur] + 1);
status[cur][i] = true;
} }
return dp[s.size()];
} }; int main(void)
{
string s("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
//string s("aabbed");
Solution solution;
int minCut = solution.minCut(s);
cout<<minCut<<endl;
return 0;
}
LeetCode132:Palindrome Partitioning II的更多相关文章
- leetcode132. Palindrome Partitioning II
leetcode132. Palindrome Partitioning II 题意: 给定一个字符串s,分区使分区的每个子字符串都是回文. 返回对于s的回文分割所需的最小削减. 例如,给定s =&q ...
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- 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 ...
- 动态规划——Palindrome Partitioning II
Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "a ...
- LeetCode: Palindrome Partitioning II 解题报告
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
随机推荐
- 使用Eclipse进行远程调试【转】
今天决定做件有意义的事,写篇图文并茂的blog,为什么要图文并茂?因为很多事可能用语言也说不明白,从以前我发表的一篇文章可以看得出来,http://blog.csdn.net/sunyujia/arc ...
- .Net基础
标题 状态 内容 NET应用程序是如何执行的? http://www.cnblogs.com/kingmoon/archive/2012/07/16/2594459.html ...
- c++中两个类互相引用的问题
最近在改一个C++程序的时候碰到一条警告信息,警告信息为:“ 删除指向不完整“Q2DTorusNode”类型的指针:没有调用析构函数 1> c:\users\lxw ...
- Selenium WebDriver屏幕截图(C#版)
Selenium WebDriver屏幕截图(C#版)http://www.automationqa.com/forum.php?mod=viewthread&tid=3595&fro ...
- 使用Flask-Migrate进行管理数据库升级
我们在升级系统的时候,经常碰到需要更新服务器端数据结构等操作,之前的方式是通过手工编写alter sql脚本处理,经常会发现遗漏,导致程序发布到服务器上后无法正常使用. 现在我们可以使用Flask-M ...
- java之final关键字
final关键字(可以读不可以写.只读) 1.final的变量的值不能够被改变 ①.final的成员变量 ②.final的局部变量(形参) //意思是“实参”一旦传进我的方法里面,就不允许改变 2.f ...
- java之多态(Polymorphic)、动态绑定(Dynamic Binding)、迟绑定(Late Binding)
今天,我们来说说java面向对象最核心的东西,多态.通过多态可以使我们的程序可复用性达到极致,这就是我们为什么要学多态的原因. “多态”(Polymorphic)也叫“动态绑定”(Dynamic Bi ...
- java之Object类介绍
1.Object类是所有java类的基类 如果在类的声明中未使用extends关键字指明其基类,则默认基类为Object类,ex: public class Person{ ~~~~~ } 等价于 p ...
- centos下安装ZooKeeper
1.需求 安装ZooKeeper,metaQ 2.下载 http://zookeeper.apache.org/releases.html 当前stable版是zookeeper-3.4.6 3.解压 ...
- java攻城师之路--复习java web之jsp入门_El表达式_JSTL标签库
JSP 技术掌握:JSP语法 + EL + JSTL 为什么sun推出 JSP技术 ? Servlet 生成网页比较复杂,本身不支持HTML语法,html代码需要通过response输出流输出,JSP ...