【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的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...
随机推荐
- 爬虫基本原理及requests,response详解
一.爬虫基本原理 1.爬虫是什么 #1.什么是互联网? 互联网是由网络设备(网线,路由器,交换机,防火墙等等)和一台台计算机连接而成,像一张网一样. #2.互联网建立的目的? 互联网的核心价值在于数据 ...
- VirtualKD + Windbg 调试Win10虚拟机
安装完vminstall后,先在运行中输入"msconfig"命令,显示如下窗口. 首先点击“引导”选项卡,然后选择最后一个引导项(Disable Signature Enforc ...
- Jsoup获取全国地区数据(省市县镇村)(续) 纯干货分享
前几天给大家分享了一下,怎么样通过jsoup来从国家统计局官网获取全国省市县镇村的数据.错过的朋友请点击这里.上文说到抓取到数据以后,我们怎么转换成我们想要格式呢?哈哈,解析方式可能很简单,但是有一点 ...
- NBUT 1117 Kotiya's Incantation(字符输入处理)
题意: 比较两个串,有三种情况:完全相同,可见字符相同,不同.每个字符串以'-'结尾.难点在输入. 思路: 字符逐个读入,直到'-'为止,读出两串就可以直接进行判断.如果不足两串则结束.输入时需要注意 ...
- strongSwan大坑一直重启(ubuntu)
报错 Starting strongSwan 5.3.2 IPsec [starter]... charon (20533) started after 40 ms charon stopped af ...
- Java学习之初识线程
“身之主宰便是心,心之所发便是意,意之本体便是知,意之所在便是物 --摘自阳明先生语录” 1.概念 在说线程之前我们先了解关于进程的一些知识,什么是进程? 程序一旦运行就是一个独立的进程,以windo ...
- Fiddler模拟POST请求
在进行接口测试时,会模拟post请求,发送不同的请求参数,返回不同的结果,今天我们就来分享一下,怎么用Fiddler工具模拟post请求: 打开Fiddler工具,在右侧点击“composer”的选项 ...
- shell脚本自动部署及监控
一.shell脚本部署nginx反向代理和三个web服务 1 对反向代理服务器进行配置 #!/bin/bash #修改用户交互页面 用户输入参数执行相应的参数 #安装epel扩展包和nginx fun ...
- Dubbo服务的搭建
dubbo框架主要作用是基于RPC的远程调用服务管理,但是注册中心是用的zookeeper,搭建dubbo,首先要安装zookeeper,配置zookeeper... 实现功能如图所示:(存在2个系统 ...
- Spring框架 全注解annotation不使用配置文件(SpringConfiguration.java类代替) 补充 xml配置文件没有提示解决
全注解不使用配置文件 首先还是倒包 在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.b ...