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. iOS Category 添加属性实现原理 - 关联对象

    iOS Category 添加属性实现原理 - 关联对象 RunTime为Category动态关联对象 使用RunTime给系统的类添加属性,首先需要了解对象与属性的关系.对象一开始初始化的时候其属性 ...

  2. 洛谷 P1276 校门外的树(增强版)

    题目描述 校门外马路上本来从编号0到L,每一编号的位置都有1棵树.有砍树者每次从编号A到B处连续砍掉每1棵树,就连树苗也不放过(记 0 A B ,含A和B):幸运的是还有植树者每次从编号C到D 中凡是 ...

  3. UVA1665 Islands (并查集)

    补题,逆序考虑每个询问的时间,这样每次就变成出现新岛屿,然后用并查集合并统计.fa = -1表示没出现. 以前写过,但是几乎忘了,而且以前写得好丑的,虽然常数比较小,现在重新写练练手.每个单词后面都要 ...

  4. leetcode_1052. Grumpy Bookstore Owner

    1052. Grumpy Bookstore Owner https://leetcode.com/problems/grumpy-bookstore-owner/ 题意:每个时刻i会有custome ...

  5. js 获取当前URL信息

    document.location 这个对象包含了当前URL的信息 location.host 获取port号 location.hostname 设置或获取主机名称 location.href 设置 ...

  6. ftp - Internet 文件传输程序 (file transfer program)

    概述 (SYNOPSIS) ftp [-pinegvd ] [host ] pftp [-inegvd ] [host ] 说明 (DESCRIPTION) 用户通过 Ftp 这个程序来使用 Inte ...

  7. python之数据类型补充

    1. capitalize (首字母大写) 例题: s = "alex wusir" s1 = s.capitalize() # 格式 print(s1) ''' 输出结果 Ale ...

  8. UIScrollView和MultiThreading、UITextField、Keyboard

  9. Linux安全调优1:CentOS防火墙的设置与优化

    CentOS防火墙的设置与优化 时间:2014-09-11 02:11来源:blog.csdn.net 作者:成长的小虫 的BLOG 举报 点击:4908次 一.设置主机防火墙. 开放: 服务器的:w ...

  10. 【动态规划】luoguP1941 飞扬的小鸟

    细节总是打挂选手:) 题目描述 Flappy Bird是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管 ...