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. ios UnitTest 学习笔记1

    一.运行第一个单元测试: 1.在Xcode 5中新建一个工程默认自带一个单元测试的文件夹,IDE自动生成了一个实现XCTestCase的.m文件,里面有一个失败测试(早期版本中实现的是SenTestC ...

  2. 欧拉回路/通路 Codeforces Round #288 (Div. 2)

    http://codeforces.com/contest/508/problem/D 以上是题目链接 题目大意 给n个字符串看能不能链接在一起 因为 三个三个分割 所以字符串 如abc ab作为起点 ...

  3. (三)VMware harbor使用http访问

    参考:https://www.cnblogs.com/biglittleant/p/7283738.html harbor使用http访问 如果使用http启动harbor需要在docker中配置-- ...

  4. 如何移除 Navicat Premium for Mac 的所有文件

    作者:郭文峰链接:http://www.zhihu.com/question/24210959/answer/34579422来源:知乎著作权归作者所有,转载请联系作者获得授权. 数据库连接信息存放在 ...

  5. SC || 关于java迭代中修改迭代集合的操作

    在通过for循环遍历整个List/Map等的时候,如果想要进行remove的操作,这时就更改了迭代集合,会出现错误 一种方法是如果只会remove一个可以remove后直接break 另一种是把集合先 ...

  6. cmake 指定输出目录

    $ mkdir ~/cpp-netlib-build $ cd ~/cpp-netlib-build $ cmake -DCMAKE_BUILD_TYPE=Debug \ > -DCMAKE_C ...

  7. javaEE(14)_文件上传下载

    一.文件上传概述 1.实现web开发中的文件上传功能,需完成如下二步操作: •在web页面中添加上传输入项•在servlet中读取上传文件的数据,并保存到本地硬盘中. 2.如何在web页面中添加上传输 ...

  8. java在线聊天项目1.3版 ——设计好友列表框功能

    设计好友列表框功能,思路—— 1.当客户端成功登陆后,则客户端把成功登陆信息发送给服务端, 2.由服务端将接收到来自各个成功登陆的客户端的用户信息添加进好友列表, 3.每当有成功登陆的用户就向各个客户 ...

  9. substring substr slice 区别

    1. substring(start,end)  返回指定索引区间的字串,不改变原字符串 start 必需,开始位置的索引,一个非负的整数 end  可选,结束位置的索引(不包括其本身),如果未设置, ...

  10. static静态变量的用法

    一,static全局变量 当一个进程的全局变量被声明为static之后,它的中文名叫静态全局变量.静态全局变量和其他的全局变量的存储地点并没有区别,都是在.data段(已初始化)或者.bss段(未初始 ...