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. 郑捷2017年电子工业出版社出版的图书《NLP汉语自然语言处理原理与实践》

    郑捷2017年电子工业出版社出版的图书<NLP汉语自然语言处理原理与实践> 第1章 中文语言的机器处理 1 1.1 历史回顾 2 1.1.1 从科幻到现实 2 1.1.2 早期的探索 3 ...

  2. Spark从HDFS上读取JSON数据

    代码如下: import org.apache.spark.sql.Row; import org.apache.spark.SparkConf; import org.apache.spark.ap ...

  3. go语言基础之copy的使用

    1.copy的使用 示例: package main //必须有个main包 import "fmt" func main() { srcSlice := []int{1, 2} ...

  4. Python并发编程-redis-3.0.5 源码安装

    1.简介 Remote Dictionary Server(Redis)是一个基于 key-value 键值对的持久化数据库存储系统.redis 和 Memcached 缓存服务很像,但它支持存储的 ...

  5. Java基础(十四):泛型

    一.Java 泛型: Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质是参数化类型,也就 ...

  6. 数据库操作语句类型(DQL、DML、DDL、DCL)简介

    SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML,数据定义语言DDL,数据控制语言DCL. 1. 数据查询语言DQL数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHER ...

  7. SQL-查询排名

    select row_number() over(order by amount) as rank,* from dbo.t_group

  8. Real-time 3D Reconstruction using Kinect

    http://jiakaizhang.com/project/real-time-3d-reconstruction/ Real-time 3D Reconstruction using Kinect ...

  9. 从头说catalan数及笔试面试里那些相关的问题 (转)

    作者:寒小阳 时间:2013年9月. 出处:http://blog.csdn.net/han_xiaoyang/article/details/11938973. 声明:版权所有,转载请注明出处,谢谢 ...

  10. poj_1681_高斯消元

    这道题和之前的把那一道1222很类似.仅仅只是一定要注意一下对于无解的推断. /*########################################################### ...