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. Composer使用体验

    什么是Composer 简单来说,Composer是PHP的包依赖管理器.但是Composer并不是类似于Yum.Apt的包管理器.Composer可以用于包或者第三方库的安装,但是可以选择在Proj ...

  2. 中国计算机学会CCF推荐国际学术会议和期刊目录(PDF版,2015年)

    total: CCF推荐国际学术会议和期刊目录(下载) parts: 点击下载:  计算机体系结构并行与分布计算存储系统.pdf 点击下载:  计算机网络.pdf 点击下载:  网络与信息安全.pdf ...

  3. C语言条件编译

    使用与平台有关的C语言函数,可能会使得程序不具有可移植性.比如Socket编程.多线程编程等是与平台有关的. 若想将程序做成平台无关的就需要用到与平台相关的条件编译. 下面转自:http://blog ...

  4. go语言基础之随机数的使用

    1.随机数的使用 示例1:   如果种子参数一样,每次运行程序产生的随机数都一样 package main //必须有个main包 import "fmt" import &quo ...

  5. 第2章 排序 | 第10节 计数排序练习题 && 基数排序

    对于一个int数组,请编写一个计数排序算法,对数组元素排序. 给定一个int数组A及数组的大小n,请返回排序后的数组. 测试样例: [1,2,3,5,2,3],6 [1,2,2,3,3,5] 计数排序 ...

  6. iOS开发-简单的图片查看器

    现在你只要拿着手机,不管你Android还是iOS,新闻类的App不可避免都有一个功能就是图片查看,做个专题,查看一下内容,App Store中也有专门针对图片浏览的App,鉴于目前所知有限,无法做到 ...

  7. OpenGL使用libPng读取png图片

    #include<stdarg.h> #include<png.h> #include<glut.h> #include<math.h> #includ ...

  8. maven nexus linux私服搭建

    搭建maven 下载jar包,将apache-maven-3.2.2-bin.tar.gz上传到server 1.解压 tar -zvxf apache-maven-3.2.2-bin.tar.gz ...

  9. 【转】Understanding the Angular Boot Process

    原文: https://medium.com/@coderonfleek/understanding-the-angular-boot-process-9a338b06248c ----------- ...

  10. sass与less

    刚刚发现sass这个东西,前端真热闹,下面比较一下这两者的共同点与区别. 开头总结一下,方便记忆:sass依赖后端计算能力,less依赖客户端的计算能力. 很多开发者不选择LESS是因为LESS输出修 ...