https://leetcode.com/problems/palindrome-partitioning-ii/description/

【题意】

给定一个字符串,求最少切割多少下,使得切割后的每个子串都是回文串

【思路】

求一个字符串的所有子串是否是回文串,O(n^2) dp从后往前推

  1. vector<vector<bool> > p(len,vector<bool>(len));
  2. for(int i=;i<len-;i++){
  3. for(int j=;j<len-;j++){
  4. if(j!=i) p[i][j]=false;
  5. else p[i][j]=true;
  6. }
  7. }
  8. for(int i=len-;i--;i>=){
  9. for(int j=i+;j<len;j++){
  10. if(s[i]==s[j]&&((j==i+)||p[i+][j-])){
  11. p[i][j]=true;
  12. }else{
  13. p[i][j]=false;
  14. }
  15. }
  16. }

然后再dp求最小切割

【AC】

  1. class Solution {
  2. public:
  3. const int inf=0x3f3f3f3f;
  4.  
  5. int minCut(string s){
  6. int len=s.length();
  7. if(len== || len==) return ;
  8. vector<vector<bool> > p(len,vector<bool>(len));
  9. for(int i=;i<len;i++){
  10. for(int j=;j<len;j++){
  11. if(j!=i) p[i][j]=false;
  12. else p[i][j]=true;
  13. }
  14. }
  15. for(int i=len-;i--;i>=){
  16. for(int j=i+;j<len;j++){
  17. if(s[i]==s[j]&&((j==i+)||p[i+][j-])){
  18. p[i][j]=true;
  19. }else{
  20. p[i][j]=false;
  21. }
  22. }
  23. }
  24. vector<int> dp(len);
  25. for(int i=;i<len;i++) dp[i]=inf;
  26. for(int i=;i<len;i++){
  27. if(p[][i]) dp[i]=;
  28. }
  29. for(int i=;i<len;i++){
  30. for(int j=i+;j<len;j++){
  31. if(p[i+][j]){
  32. dp[j]=min(dp[j],dp[i]+);
  33. }
  34. }
  35. }
  36. return dp[len-];
  37. }
  38. };

【leetcode dp】132. Palindrome Partitioning II的更多相关文章

  1. 【LeetCode】132. Palindrome Partitioning II

    Palindrome Partitioning II  Given a string s, partition s such that every substring of the partition ...

  2. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  3. leetcode 132. Palindrome Partitioning II ----- java

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. 132. Palindrome Partitioning II (String; DP)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. 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 ...

  6. 132. Palindrome Partitioning II

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  7. Leetcode 132. Palindrome Partitioning II

    求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...

  8. 【leetcode dp】629. K Inverse Pairs Array

    https://leetcode.com/problems/k-inverse-pairs-array/description/ [题意] 给定n和k,求正好有k个逆序对的长度为n的序列有多少个,0& ...

  9. 【leetcode dp】Dungeon Game

    https://leetcode.com/problems/dungeon-game/description/ [题意] 给定m*n的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...

随机推荐

  1. sqlite总结1

    I Shell下命令行程序CLP I .help II 命令的简写 .e = .quit .s .h = .help II 数据库管理 A 创建数据库 1 CREATE TABLE id_name(i ...

  2. LR中webservice服务测试的脚本

    Action(){ /* 测试QQ是否在线的功能接口 输入参数:QQ号码 String,默认QQ号码:8698053. 返回数据:String,Y = 在线:N = 离线:E = QQ号码错误:A = ...

  3. openstack v3 rest 访问

    1. openstack主要面向得是python为主得开发.目前java中嵌入openstack主要是通过rest接口访问 2. 下载一个postman的接口测试工具 3. openstack 中的服 ...

  4. Python之Mac Scrapy爬虫小记

    最近在尝试用Python爬虫,在装Scrapy的过程中遇到了一些麻烦. 上网搜索资料也未能解决command not found scrapy的报错. 最后我删除scrapy,用pip3.6 inst ...

  5. 【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 ...

  6. Linux命令权限 用户权限 组权限 文件、目录权限

    Linux命令的格式是: 命令+选项+参数 命令是必须存在的,选项和参数可以不必存在,不写的情况是有默认的参数 Linux 一切皆文件 对于文件而言,只需要对文件进行读写就可以实现对文件内容内容的增删 ...

  7. QT +自定义控件-spin+slider

    动手实现自定义控件: 1.首先在ui界面中添加一个(Widget)容器类.如图中的1所示 2.在项目中添加一个SmallWidget类,如下: 3.接着在程序编辑界面进行程序编辑如下: #includ ...

  8. HTML5<picture>元素

    HTML5<picture>元素可以设置多张图片 <!DOCTYPE html><html><head><meta http-equiv=&quo ...

  9. [LUOGU] P1551 亲戚

    题目背景 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系. 题目描述 规定:x和y是亲戚,y和z是亲戚,那么x和z也是亲戚.如 ...

  10. free指令的说明

    CentOS 6.x系统中的freefree [-b|-k|-m|-g|-h] [-l] [-o] [-t] [-s delay] [-c count] [-V] -b #-k,-m,-g 以单位by ...