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.

 
采用动态规划的方法
首先可以先得到isP[i][j],即字符串中,从i到j是否是回文
isP[i][j]=((s[i]==s[j])&&(j-i<=1||isP[i+1][j-1]))
然后我们再确定分割次数
 
dp[i]代表了从i到n的最小分割次数,只需要从中间找到一点,使得分割次数最少即可
dp[i]=min(dp[k]+1)   k=i+1……n
 

 class Solution {
public:
int minCut(string s) { int n=s.length();
vector<vector<bool> > isP(n,vector<bool>(n,false)); for(int i=n-;i>=;i--)
{
for(int j=i;j<n;j++)
{
if(s[i]==s[j]&&(j-i<=||isP[i+][j-])) isP[i][j]=true;
}
} vector<int> dp(n+);
dp[n]=-; //注意边界条件,表示了dp[i]无需分割时,dp[i]=dp[n]+1=0;
for(int i=n-;i>=;i--)
{
int minCut=INT_MAX;
for(int k=i+;k<n+;k++)
{
if(isP[i][k-]&&minCut>dp[k]+)
{
dp[i]=dp[k]+;
minCut=dp[i];
}
}
} return dp[];
}
};
 
 

【leetcode】Palindrome Partitioning II的更多相关文章

  1. 【leetcode】Palindrome Partitioning II(hard) ☆

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  2. 【LeetCode】Palindrome Partitioning 解题报告

    [题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  3. 【leetcode】Palindrome Partitioning

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

  4. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  5. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  6. 【leetcode刷题笔记】Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. 【Leetcode】【Medium】Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  8. 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. [Leetcode][JAVA] Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

随机推荐

  1. iOS边练边学--父子控件之作为导航控制器的子类产生的问题以及网易新闻练习

    一.导航控制器的子类 作为导航控制器的子类,并且是导航控制器子类中的第一个,系统会默认给子控件添加EdgeInsert属性,把导航栏的宽度挤出来.但是系统只会默认修改第一个. 解决办法1:系统帮忙给第 ...

  2. POJ2186 Popular Cows

    Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...

  3. AppStore占坑注意事项

    AppStore占坑注意事项 我们会提前在AppStore(iTunesConnect)里注册一些应用名称,以满足未来业务需要和防止恶意注册,其中有一些需要注意的事情,整理如下: 倒计时180天 为了 ...

  4. GNUPLOT画图工具

    http://blog.csdn.net/codingkid/article/details/7211492 不得不说这个工具实在是太强大了. 1.首先命令简单,不会有那么多的语法问题. 2.其次画图 ...

  5. visual studio 2012如何彻底删除TFS上的团队项目

    http://www.cnblogs.com/zfanlong1314/p/3378441.html 本人的TFS地址:https://zfanlong1314.visualstudio.com/ 最 ...

  6. android经典实战项目视频教程下载

    注:这是一篇转载的文章,原文具体链接地址找不到了,将原文分享如下,希望能对看到的朋友有所帮助! 最近在学习android应用方面的技术,自己在网上搜集了一些实战项目的资料,感觉挺好的,发布出来跟大伙分 ...

  7. jquery发送ajax请求返回数据格式

    jquery向服务器发送一个ajax请求后,可以返回多种类型的数据格式,包括:html,xml,json,text等. 1.html格式的数据 "<div class='comment ...

  8. RSA算法小记

    学习来源:http://www.cnblogs.com/vamei/p/3480994.html 小记: 一.数学基础: 欧拉Phi函数:Φ(n)=总数(从1到n-1中与n互质的整数) (1)欧拉定理 ...

  9. Writing the first draft of your science paper — some dos and don’ts

    Writing the first draft of your science paper — some dos and don’ts 如何起草一篇科学论文?经验丰富的Angel Borja教授告诉你 ...

  10. WinForm TextBox 焦点停留到文本最后

    最近写个 WinForm 项目,TextBox 控件有内容的时候,获取焦点,光标总是在最前面,很不便于输入.那怎么样让光标停留到最后呢?如下代码可以实现:            this.txtBox ...