https://leetcode.com/problems/palindrome-partitioning-ii/description/

【题意】

给定一个字符串,求最少切割多少下,使得切割后的每个子串都是回文串

【思路】

求一个字符串的所有子串是否是回文串,O(n^2) dp从后往前推

     vector<vector<bool> > p(len,vector<bool>(len));
for(int i=;i<len-;i++){
for(int j=;j<len-;j++){
if(j!=i) p[i][j]=false;
else p[i][j]=true;
}
}
for(int i=len-;i--;i>=){
for(int j=i+;j<len;j++){
if(s[i]==s[j]&&((j==i+)||p[i+][j-])){
p[i][j]=true;
}else{
p[i][j]=false;
}
}
}

然后再dp求最小切割

【AC】

class Solution {
public:
const int inf=0x3f3f3f3f; int minCut(string s){
int len=s.length();
if(len== || len==) return ;
vector<vector<bool> > p(len,vector<bool>(len));
for(int i=;i<len;i++){
for(int j=;j<len;j++){
if(j!=i) p[i][j]=false;
else p[i][j]=true;
}
}
for(int i=len-;i--;i>=){
for(int j=i+;j<len;j++){
if(s[i]==s[j]&&((j==i+)||p[i+][j-])){
p[i][j]=true;
}else{
p[i][j]=false;
}
}
}
vector<int> dp(len);
for(int i=;i<len;i++) dp[i]=inf;
for(int i=;i<len;i++){
if(p[][i]) dp[i]=;
}
for(int i=;i<len;i++){
for(int j=i+;j<len;j++){
if(p[i+][j]){
dp[j]=min(dp[j],dp[i]+);
}
}
}
return dp[len-];
}
};

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

  1. 【LeetCode】132. Palindrome Partitioning II

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

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

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

  3. leetcode 132. Palindrome Partitioning II ----- java

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

  4. 132. Palindrome Partitioning II (String; DP)

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

  5. Java for LeetCode 132 Palindrome Partitioning II

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

  6. 132. Palindrome Partitioning II

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

  7. Leetcode 132. Palindrome Partitioning II

    求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...

  8. 【leetcode dp】629. K Inverse Pairs Array

    https://leetcode.com/problems/k-inverse-pairs-array/description/ [题意] 给定n和k,求正好有k个逆序对的长度为n的序列有多少个,0& ...

  9. 【leetcode dp】Dungeon Game

    https://leetcode.com/problems/dungeon-game/description/ [题意] 给定m*n的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...

随机推荐

  1. 三行命令搞定查询Python安装目录

    想为Python添加一个库文件到默认目录,却忘记了Python安装目录. 其实,只要用下面三行命令,就可以轻松得到Python安装路径了. 进入Python >>>import sy ...

  2. HDU 5418 Victor and World (可重复走的TSP问题,状压dp)

    题意: 每个点都可以走多次的TSP问题:有n个点(n<=16),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短. 思路: 给了很多重边,选最小的留下即可.任意点可能无法直接到达, ...

  3. poj1595 水题

    题意:输入n, 和c  统计1 - n 有多少个素数为cnt 若 2*c > cnt 则将素数全部输出 否则分支判断:  若cnt 为偶数,则从中心开始输出2*c 个  若cnt 为奇数,则从中 ...

  4. dubbo 快速入门

    1. 步骤 实现消费者调用生产者服务接口. 2.安装zookeeper  3.创建maven 工程 4.安装dobboadmin 平台实现监控 5.dubbo 目录结构 ------dubbo-mem ...

  5. TCP的三次握手与四次挥手详解

    TCP的三次握手与四次挥手是TCP创建连接和关闭连接的核心流程,我们就从一个TCP结构图开始探究中的奥秘  序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序 ...

  6. Vue的elementUI实现自定义主题

    使用vue开发项目,用到elementUI,根据官网的写法,我们可以自定义主题来适应我们的项目要求,下面来介绍一下两种方法实现的具体步骤,(可以参考官方文档自定义主题官方文档),先说项目中没有使用sc ...

  7. bootstrap 两端对齐的导航

    您可以在屏幕宽度大于768px时,通过在分别使用.nav .nav-tabs或.nav .nav-pills的同时使用class.nav-justified,让标签式或胶囊式导航菜单与父元素等宽,在更 ...

  8. NOIP 2018数据点

    链接: https://pan.baidu.com/s/14jXQGPSac3b9_m5h5x2wGQ 提取码: 1cbk 好文别忘点赞!!!

  9. DNS服务-了解篇

    简介 DNS是用来名字解析的,名字解析成IP地址,IP地址解析成名字,正反操作,有服务器端和客户端即 S/C DNS是应用层协议,基于UDP/53.TCP/53端口,缺一不可 分为正向解析和反向解析/ ...

  10. Python模块概念

    补充:生成器表达式 将列表生成器的中括号改为小括号就是生成器表达式 res = [i for i in range(10) if i > 5]  #  列表生成式 res = (i for i ...