zoukankan      html  css  js  c++  java
  • [LeetCode] Palindrome Partitioning II

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

    Return the minimum cuts needed for a palindrome partitioning of s.

    For example, given s = "aab",
    Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.

      给定一个字符串,返回最小回文分割次数。同上一题,此处也需要判定字符串是否是回文串,需要使用到DP算法来加速。

      考虑问题的主体:最小次数。给定一个字符串s,设最小分割函数为min_cut(),字符串s的最小分割串集合为{s}。{s}会对应有几个分割点,例如示例中的1cut,对应会有一个分割点。在这些分割点中任取一个点k,它会把s分割为s1,s2两个子串,同时也会把最小分割集合分成两部分{s1},{s2},则集合{s1}和{s2}必然也会是子串s1和s2的最小分割集合,且

      min_cut(s) = min_cut(s1) + min_cut(s2) + 1   (1)

    证明可以使用反证法,如果{s1}不是s1的最小分割集合,则必然存在一个最小分割集合{s1}',用它替换{s1}在{s}中的位置,可以得到更优的最小分割集合{s}’。这与{s}是最小分割集合相矛盾。可以看出这是一个最有子结构问题,可以考虑使用DP来求解。

      综上所述,这里两次使用了DP算法,一次是在判断回文串,另一次是在找最小分割次数。

    代码可以参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html

  • 相关阅读:
    cmd常用指令
    python笔记01-05
    python安装过程中的一些问题
    初始化spring容器的一种方式
    切入点范式
    spring的list注入多个值
    Statement和PreparedStatement有什么区别?哪个效率高?
    sql 的四种隔离级别
    简单的spring核心配置文件编写
    spring
  • 原文地址:https://www.cnblogs.com/CGwolke/p/3258487.html
Copyright © 2011-2022 走看看