【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的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...
随机推荐
- sqlite总结1
I Shell下命令行程序CLP I .help II 命令的简写 .e = .quit .s .h = .help II 数据库管理 A 创建数据库 1 CREATE TABLE id_name(i ...
- LR中webservice服务测试的脚本
Action(){ /* 测试QQ是否在线的功能接口 输入参数:QQ号码 String,默认QQ号码:8698053. 返回数据:String,Y = 在线:N = 离线:E = QQ号码错误:A = ...
- openstack v3 rest 访问
1. openstack主要面向得是python为主得开发.目前java中嵌入openstack主要是通过rest接口访问 2. 下载一个postman的接口测试工具 3. openstack 中的服 ...
- Python之Mac Scrapy爬虫小记
最近在尝试用Python爬虫,在装Scrapy的过程中遇到了一些麻烦. 上网搜索资料也未能解决command not found scrapy的报错. 最后我删除scrapy,用pip3.6 inst ...
- 【iview input 回车刷页面bug】input 就一个的时候 有form的时候 回车会刷页面,如果就一个input,可以不要form,或者form里面两个input 将一个input v-show false 就可以了
[iview input 回车刷页面bug]input 就一个的时候 有form的时候 回车会刷页面,如果就一个input,可以不要form,或者form里面两个input 将一个input v-sh ...
- Linux命令权限 用户权限 组权限 文件、目录权限
Linux命令的格式是: 命令+选项+参数 命令是必须存在的,选项和参数可以不必存在,不写的情况是有默认的参数 Linux 一切皆文件 对于文件而言,只需要对文件进行读写就可以实现对文件内容内容的增删 ...
- QT +自定义控件-spin+slider
动手实现自定义控件: 1.首先在ui界面中添加一个(Widget)容器类.如图中的1所示 2.在项目中添加一个SmallWidget类,如下: 3.接着在程序编辑界面进行程序编辑如下: #includ ...
- HTML5<picture>元素
HTML5<picture>元素可以设置多张图片 <!DOCTYPE html><html><head><meta http-equiv=&quo ...
- [LUOGU] P1551 亲戚
题目背景 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系. 题目描述 规定:x和y是亲戚,y和z是亲戚,那么x和z也是亲戚.如 ...
- free指令的说明
CentOS 6.x系统中的freefree [-b|-k|-m|-g|-h] [-l] [-o] [-t] [-s delay] [-c count] [-V] -b #-k,-m,-g 以单位by ...