【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的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...
随机推荐
- iOS Category 添加属性实现原理 - 关联对象
iOS Category 添加属性实现原理 - 关联对象 RunTime为Category动态关联对象 使用RunTime给系统的类添加属性,首先需要了解对象与属性的关系.对象一开始初始化的时候其属性 ...
- 洛谷 P1276 校门外的树(增强版)
题目描述 校门外马路上本来从编号0到L,每一编号的位置都有1棵树.有砍树者每次从编号A到B处连续砍掉每1棵树,就连树苗也不放过(记 0 A B ,含A和B):幸运的是还有植树者每次从编号C到D 中凡是 ...
- UVA1665 Islands (并查集)
补题,逆序考虑每个询问的时间,这样每次就变成出现新岛屿,然后用并查集合并统计.fa = -1表示没出现. 以前写过,但是几乎忘了,而且以前写得好丑的,虽然常数比较小,现在重新写练练手.每个单词后面都要 ...
- leetcode_1052. Grumpy Bookstore Owner
1052. Grumpy Bookstore Owner https://leetcode.com/problems/grumpy-bookstore-owner/ 题意:每个时刻i会有custome ...
- js 获取当前URL信息
document.location 这个对象包含了当前URL的信息 location.host 获取port号 location.hostname 设置或获取主机名称 location.href 设置 ...
- ftp - Internet 文件传输程序 (file transfer program)
概述 (SYNOPSIS) ftp [-pinegvd ] [host ] pftp [-inegvd ] [host ] 说明 (DESCRIPTION) 用户通过 Ftp 这个程序来使用 Inte ...
- python之数据类型补充
1. capitalize (首字母大写) 例题: s = "alex wusir" s1 = s.capitalize() # 格式 print(s1) ''' 输出结果 Ale ...
- UIScrollView和MultiThreading、UITextField、Keyboard
- Linux安全调优1:CentOS防火墙的设置与优化
CentOS防火墙的设置与优化 时间:2014-09-11 02:11来源:blog.csdn.net 作者:成长的小虫 的BLOG 举报 点击:4908次 一.设置主机防火墙. 开放: 服务器的:w ...
- 【动态规划】luoguP1941 飞扬的小鸟
细节总是打挂选手:) 题目描述 Flappy Bird是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管 ...