【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的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...
随机推荐
- 分布式定时任务的redis锁实现
一个web项目如果部署为分布式时,平时常见的定时服务在一定的间隔时间内,可能出现多次重复调用的问题.而此时由于是不同容器之间的竞争,因此需要容器级别的锁 Redis为单进程单线程模式,采用队列模式将并 ...
- PostgreSQL 的日期函数用法举例
最近偶有开发同事咨询 PostgreSQL 日期函数,对日期处理不太熟悉,今天详细看了下手册的日期函数,整理如下,供参考. 一 取当前日期的函数 --取当前时间skytf=> select no ...
- SAP成都研究院郑晓霞:Shift Left Testing和软件质量保证的一些思考
今天的文章来自Jerry的同事,曾经的搭档郑晓霞(Zheng Kate).郑晓霞是在Jerry心中是一位很有实力的程序媛,2011年从西安某软件公司跳槽到SAP成都研究院.当时,成都研究院的CRM团队 ...
- iOS打包上传app store各种问题解决总结
问题1 this action could not be completed. try again 问题2 there was an error sending data to the iTunes ...
- WPF知识点全攻略05- XAML内容控件
此处简单列举出布局控件外,其他常用的控件: Window:WPF窗口 UserControl:用户控件 Page:页 Frame:用来浏览Page页 Border:嵌套控件,提供边框和背景. Butt ...
- Noip2016 提高组 蚯蚓
刚看到这道题:这题直接用堆+模拟不就可以了(并没有认真算时间复杂度) 于是用priority_queue水到了85分-- (STL大法好) 天真的我还以为是常数问题,于是疯狂卡常--(我是ZZ) 直到 ...
- 八 个优秀的 jQuery Mobile 教程
jQuery Mobile 是 jQuery 在手机上和平板设备上的版本.jQuery Mobile不仅会给主流移动平台带来jQuery核心库,而且会发布一个完整统一的jQuery移动UI框架.虽然j ...
- shell脚本,怎么实现每次新开一个shell都输出一个提示语?
[root@localhost wyb]# cat test.sh echo -e "\033[32mhello,This is wangyuebo's shell\033[0m" ...
- substring substr slice 区别
1. substring(start,end) 返回指定索引区间的字串,不改变原字符串 start 必需,开始位置的索引,一个非负的整数 end 可选,结束位置的索引(不包括其本身),如果未设置, ...
- 【树形背包】bzoj4033: [HAOI2015]树上染色
仔细思考后会发现和51nod1677 treecnt有异曲同工之妙 Description 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并 ...