Palindrome Partitioning II
这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数。
Example:
Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
状态:这个题的状态也非常好理解,dp[i]表示将s[0..i]分割成回文子串的最小分割次数。然后不急于寻找状态转移方程,我们先要明确如何用代码判断一个字符串的某个部分是不是回文,其实也很好理解啊,咱们可以分块来理解,毕竟这是个算法题,不可能用常规的那种遍历一半的方式来判断。首先对于这个字符串的某个子串s[j..i](j<=i),满足它是回文的条件两条(1)s[j]==s[i]  (2)  只确定两端的两个字符还不够,当这个子串的长度小于4或者去掉两端的相同字符后还是回文即可。现在我们除了递推数组dp,再定义一个记录数组pa[j][i],如果s[j..i]是回文,则pa[j][i] = 1,否则为0。有了这两个条件,我们就可以总结状态转移方程了:
dp[i] = min(dp[j-1]+1),当0<j<=i时,并且s[j..i]是回文时
dp[i] = 0 ,当j=0,并且s[j..i]是回文时
在具体的dp数组递推运算过程中,需要这两个分支,同时会更新pa[j][i]的值便于后面的过程中回文条件的判断。
 1 public int minCut(String s) {
 2         int slen = s.length();
 3         int[][]pa = new int[slen][slen];
 4         int[]dp = new int[slen];
 5         for(int i = 0;i<slen;i++)
 6             dp[i] = i;
 7         for(int i = 0;i<slen;i++)
 8             Arrays.fill(pa[i],0);
 9         for(int i = 1;i<slen;i++) {
10             for(int j = 0;j<=i;j++) {
11                 if(s.charAt(j)==s.charAt(i)&&((i-j<2)||pa[j+1][i-1]==1)) {
12                     pa[j][i] = 1;
13                     if(j!=0)dp[i] = Math.min(dp[i],dp[j-1]+1);
14                     else dp[i] = 0;
15                 }
16             }
17         }
18         return dp[slen-1];
19     }

转载于:https://www.cnblogs.com/messi2017/p/9904220.html

动态规划——Palindrome Partitioning II相关推荐

  1. 【回文串5 重点+动态规划】LeetCode 132. Palindrome Partitioning II

    LeetCode 132. Palindrome Partitioning II Solution1:我的答案1 直接模仿131那道题的DFS解法,找其中size最小的.果不其然,因为超时只能部分AC ...

  2. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  3. LeetCode Palindrome Partitioning II

    原题链接在这里:https://leetcode.com/problems/palindrome-partitioning-ii/ 题目: Given a string s, partition s  ...

  4. LeetCode | Palindrome Partitioning I,II

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

  5. Lintcode108 Palindrome Partitioning || solution 题解

    [题目描述] Given a strings, cutsinto some substrings such that every substring is a palindrome. Return t ...

  6. 【LeetCode】Palindrome Partitioning 解题报告

    [题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  7. Palindrome Partitioning

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

  8. C++palindrome partitioning回文分割算法的实现(附完整源码)

    C++palindrome partitioning回文分割算法的实现 C++palindrome partitioning回文分割算法的实现的完整源码(定义,实现,main函数测试) C++pali ...

  9. [Lintcode]136. Palindrome Partitioning /[Leetcode]131. Palindrome Partitioning

    136. Palindrome Partitioning / 131. Palindrome Partitioning 本题难度: Medium Topic: Search DFS Descripti ...

  10. [Leetcode Week13]Palindrome Partitioning

    Palindrome Partitioning 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/palindrome-partitioning/desc ...

最新文章

  1. 数据恢复工具PhotoRec
  2. 安装汇编环境,写一个最简单的窗口程序
  3. 《研磨设计模式》chap18 状态模式state(4)例子
  4. 初始Spring boot和一个入门SpringBoot工程
  5. 为SSIS编写自定义数据流组件(DataFlow Component)之进阶篇:自定义编辑器
  6. aptana studio 汉化与安装 zencoding、spket 配置
  7. spring 启动完成后事件监听器处理
  8. (84)JTAG接口与格雷码特点-面试必问(八)(第17天)
  9. gpg 中标麒麟获取 密钥失败_PHP实现码云Gitee的WebHook密钥验证算法
  10. Spark DF:关于Row中的数值获取问题
  11. Linux - wxWidgets安装和编译HelloWorld
  12. Cesium教程系列汇总
  13. GJB 软件质量保证计划(模板)
  14. 质点碰撞和卢瑟福公式
  15. 信息安全竞赛优秀作品介绍1
  16. 树莓派4B WIFI 物理网口设置固定IP方法
  17. TrafficStats 网络实时测速
  18. 最新H5网页分享到Twitter、Facebook带缩略图
  19. 一手源刊|10月SCI/SSCI/EI刊源已更新, 新增多本TOP/CCF-B优质刊~
  20. 小程序原生swiper中bindtransition监听滑动效果

热门文章

  1. 【优化预测】基于matlab飞蛾扑火算法优化LSSVM预测【含Matlab源码 110期】
  2. python在工作中怎么用_在Python中调用是如何工作的?
  3. keepalived 邮件通知
  4. MyEclipse出现红色感叹号解决办法
  5. prototype和__proto__的概念
  6. 微信红包惊人秘密:谁最容易抢到大红包?
  7. js或css文件后面跟参数的原因说明
  8. anaconda安装-清华镜像库
  9. Unity 后处理 性能优化
  10. C#删掉了发给lua的对象造成lua实际的Obj无效,解决办法