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.

 

 

原题链接: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ 
这道题跟Palindrome Partitioning非常类似,区别就是不需要返回所有满足条件的结果,而只是返回最小的切割数量就可以。做过Word Break的朋友可能马上就会想到,其实两个问题非常类似,当我们要返回所有结果(Palindrome PartitioningWord Break II)的时候,使用动态规划会耗费大量的空间来存储中间结果,所以没有明显的优势。而当题目要求是返回某个简单量(比如Word Break是返回能否切割,而这道题是返回最小切割数)时,那么动态规划比起brute force就会有明显的优势。这道题先用Palindrome Partitioning中的方法建立字典,接下来动态规划的方式和Word Break是完全一样的,我们就不再细说了,不熟悉的朋友可以看看Word Break的分析哈。因为保存历史信息只需要常量时间就能完成,进行两层循环,时间复杂度是O(n^2)。空间上需要一个线性数组来保存信息,所以是O(n)。代码如下:

  1. public int minCut(String s) {  
  2.     if(s == null || s.length()==0)  
  3.         return 0;  
  4.     boolean[][] dict = getDict(s);  
  5.     int[] res = new int[s.length()+1];  
  6.     res[0] = 0;  
  7.     for(int i=0;i<s.length();i++)  
  8.     {  
  9.         res[i+1] = i+1;  
  10.         for(int j=0;j<=i;j++)  
  11.         {  
  12.             if(dict[j][i])  
  13.             {  
  14.                 res[i+1] = Math.min(res[i+1],res[j]+1);  
  15.             }  
  16.         }  
  17.     }  
  18.     return res[s.length()]-1;  
  19. }  
  20. private boolean[][] getDict(String s)  
  21. {  
  22.     boolean[][] dict = new boolean[s.length()][s.length()];  
  23.     for(int i=s.length()-1;i>=0;i--)  
  24.     {  
  25.         for(int j=i;j<s.length();j++)  
  26.         {  
  27.             if(s.charAt(i)==s.charAt(j) && (j-i<2 || dict[i+1][j-1]))  
  28.                 dict[i][j] = true;  
  29.         }  
  30.     }  
  31.     return dict;  
  32. }  

这个问题和Word Break可以说是一个题目,这里多了一步求解字典。如果求解所有结果时,他们没有多项式时间的解法,复杂度取决于结果数量,而当求解某一种统计的特殊量时,用动态规划就会很大的优势,可以降低时间复杂度。

 

 

class Solution {
public:
    int minCut(string s) {
       const int n = s.size();
        if (n <= 1) return 0;
        
        // determine witch substrs are palindrome
        vector<vector<bool> > P(n, vector<bool>(n));
        for (int i = 0; i < n; i++) P[i][i] = true;
        for (int i = 0; i < n - 1; i++) P[i][i+1] = s[i] == s[i+1];
        for (int len = 3; len <= n; len++) {
            for (int i = 0; i + len <= n; i++) {
                int j = i + len - 1;
                P[i][j] = P[i+1][j-1] && s[i] == s[j];
            }
        }
        
        // find out the min cut
        vector<int> cut(n, 0);
        for (int i = 1; i < n; i++) {
            if (P[0][i]) {
                cut[i] = 0;
            }
            else {
                cut[i] = n;
                for (int j = i; j > 0; j--) {
                    if (P[j][i] && cut[j-1] + 1 < cut[i]) {
                        cut[i] = cut[j-1] + 1;
                    }
                }
            }
        }
        
        return cut[n-1];        
    }
};

 

posted on 2015-01-08 20:50  风云逸  阅读(61)  评论(0)    收藏  举报