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.

从后往前构造二维数组isPalin,用于存储已经确定的回文子串。isPalin[i][j]==true代表s[i,...,j]是回文串。

在构造isPalin的同时使用动态规划计算从后往前的最小切分数,记录在min数组中。min[i]代表s[i,...,n-1]的最小切分数。

(上述两步分开做会使得代价翻倍,容易TLE)

关键步骤:

1、min[i]初始化为min[i+1]+1,即初始化s[i]与s[i+1]之间需要切一刀。这里考虑边界问题,因此min数组设为n+1长度。

2、从i到n-1中间如果存在位置j,同时满足:(1)s[i,...,j]为回文串;(2)1+min[j+1] < min[i]。

那么min[i]=1+min[j+1],也就是说一刀切在j的后面比切在i的后面要好。

class Solution {
public:
int minCut(string s) {
int n = s.size();
vector<vector<bool> > isPalin(n, vector<bool>(n, false));
vector<int> min(n+, -); //min cut from end for(int i = ; i < n; i ++)
{
isPalin[i][i] = true;
} for(int i = n-; i >= ; i --)
{
min[i] = min[i+] + ;
for(int j = i+; j < n; j ++)
{
if(s[i] == s[j])
{
if(j == i+ || isPalin[i+][j-] == true)
{
isPalin[i][j] = true;
if(j == n-)
min[i] = ;
else if(min[i] > min[j+]+)
min[i] = min[j+] + ;
}
}
}
} return min[];
}
};

【LeetCode】132. Palindrome Partitioning II的更多相关文章

  1. 【leetcode dp】132. Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...

  2. 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)

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

  3. 【LeetCode】131. Palindrome Partitioning

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

  4. 【leetcode】1278. Palindrome Partitioning III

    题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...

  5. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

  6. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  7. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  8. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  9. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

随机推荐

  1. otl翻译(11) -- OTL的迭代器

    OTL stream read iterator 这个类是一个像传统的JDBC中的getter()操作一样扩展了OTL流的模板类.它现在还不支持UNICODE字符集.它对otl_refcur_stre ...

  2. 容器list使用之erase(其他容器也通用)

    今天无论如何要写点东西,算是搞清楚了一点东西吧.有点小小的成就感. 之前在程序里面使用了list容器,其中用到了erase()函数,之前一直没出现问题,这两天突然莫名奇妙.花了点时间,搞清楚了eras ...

  3. 矩阵求和及Kadane算法

    今天的一道题目: https://leetcode.com/problems/max-sum-of-sub-matrix-no-larger-than-k/ 有难度.这一类题目很有代表性. 搜到这个网 ...

  4. [leetcode]Sort List @ Python

    原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度 ...

  5. 告诉我, 究竟我的SQL Server慢在哪里?

    你可以使用下面的语句来使用sys.dm_os_wait_stats这个DMV得到线程的等待信息(线程在等什么, 等了多久)的统计数值. WITH [Waits] AS (SELECT [wait_ty ...

  6. 6个原则、50条秘技提高HTML5应用及网站性能

    Jatinder Mann是微软Internet Explorer产品的一名项目经理,在BUILD 2012大会上,他做了题为“提高HTML5应用和网站性能的50条秘技(50 performance ...

  7. android实现gif图与文字混排

    我们在进行qq聊天的时候发送表情,但这些表情都是并不是静态的,更多的是动态图,gif图,那么如何在android客户端显示动态gif图呢. 在github上找到了这样一种方法,Github地址http ...

  8. IOS中的动画——Core Animation

    一.基础动画 CABasicAnimation //初始化方式 CABasicAnimation * cabase=[CABasicAnimation animation]; //通过keyPath设 ...

  9. Android NFC近场通信03----读写MifareClassic卡

                                           Android NFC近场通信02----读写MifareClassic卡 一.MifareClassic卡 相关 一般来 ...

  10. git简单运用

    创建新仓库 创建新文件夹,打开,然后执行 git init 以创建新的 git 仓库. 检出仓库 执行如下命令以创建一个本地仓库的克隆版本: git clone /path/to/repository ...