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.
    

      分析: D[i] = 区间[i,n)之间最小的cut数,n为字符串长度, 则,D[i] = min(D[i],1+D[j+1]) i<=j <n

                    P[i][j] = str[i] == str[j] && P[i+1][j-1];

    class Solution {
    public:
        int minCut(string s) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            int len = s.size();
            if(len < 2) return 0 ;
            vector<vector<bool>> flag(len, vector<bool>(len, false)) ;
            vector<int> dp(len+1);
            
            for(int i = 0; i<= len ; i++)
                dp[i] = len - i;
                
            for(int i = len -1 ; i >= 0 ; i--)
                 for(int j = i; j < len ; j++)
                     if(s[i] == s[j] &&(j-i< 2 || flag[i+1][j-1] ) )
                     {
                            flag[i][j] = true;
                            dp[i] = dp[i] < dp[j+1] +1 ? dp[i]  : dp[j+1] +1 ;
                     }
            
                
            return dp[0]-1;            
        }
    };
  • 相关阅读:
    模拟乒乓球双打和单打比赛
    关于zip内置函数的应用及在 Python 2 和 3 的不同之处
    计算文本平均列数
    四则运算
    Python跳一跳小游戏
    数据库
    类和正则表达
    带进度条的圆周率计算
    球队预测
    自己的第一个网页
  • 原文地址:https://www.cnblogs.com/graph/p/3223942.html
Copyright © 2011-2022 走看看