【leetcode dp】132. Palindrome Partitioning II
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的更多相关文章
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 132. Palindrome Partitioning II (String; DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 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 ...
- 132. Palindrome Partitioning II
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- Leetcode 132. Palindrome Partitioning II
求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...
- 【leetcode dp】629. K Inverse Pairs Array
https://leetcode.com/problems/k-inverse-pairs-array/description/ [题意] 给定n和k,求正好有k个逆序对的长度为n的序列有多少个,0& ...
- 【leetcode dp】Dungeon Game
https://leetcode.com/problems/dungeon-game/description/ [题意] 给定m*n的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...
随机推荐
- 51nod 1526 分配笔名
题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 320 难度:7级算法题. 班里有n个同学.老师为他们选了n个笔名.现在要把这些笔名分配给每一个同学,每一 ...
- PHP 哈希表碰撞攻击
理想情况下哈希表插入和查找操作的时间复杂度均为O(1),任何一个数据项可以在一个与哈希表长度无关的时间内计算出一个哈希值(key),然后在常量时间内定位到一个桶(术语bucket,表示哈希表中的一个位 ...
- Robotium实践之路基于APK创建测试项目
1.重新对包进行签名操作 .启动re-sign.jar文件 .找到相应的APK,拖拽置resigner中 2.创建基于APK测试的测试工程 .新建一个安卓测试项目 .选择this project
- OAuth授权之回调accessToken
具体说明见新浪官方文档 http://open.weibo.com/wiki/Oauth2/access_token 具体实现 第一步 打开回调页面 // 宏定义client_id #define ...
- 17条 Swift 最佳实践规范
本文由CocoaChina译者小袋子(博客)翻译自schwa的github主页原文作者:schwa 这是一篇 Swift 软件开发的最佳实践教程. 前言 这篇文章是我根据在 SwiftGraphics ...
- NOIP 成绩
这道题中点是在小数上,因为成绩可能是:“95.5 87.7……”所以我们就要用:printf和scanf这样就可以控制小数了!!! code: #include<bits/stdc++.h> ...
- hihoCoder第一周---最长回文子串(1032)
其实这就是mancher算法的板子题,贴个代码好了. 思想请见我的另一篇博客: https://blog.csdn.net/qq_41090676/article/details/86768361 # ...
- [LUOGU] P1024 选课
题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...
- Python爬虫环境常用库安装
1:urllib urllib.request这两个库是python自带的库,不需要重新安装,在python中输入如下代码: import urllibimport urllib.requestres ...
- Web框架之Django_06 模型层了解(F查询、Q查询、事务、update和save、only和defer、choice属性、bulk_create)
摘要: F查询 Q查询 事务 一.F查询 在上面所有的例子中,我们构造的过滤器都只是将字段值与某个我们自己设定的常量做比较.如果我们要对两个字段的值做比较,那该怎么做呢?Django 提供 F() 来 ...