palindrome-partitioning-ii leetcode C++

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", Return1since the palindrome partitioning["aa","b"]could be produced using 1 cut.

C++

class Solution {
public:
    int minCut(string s) {
        int n = s.size();
        if(n < 2) return 0;
        vector<int> dp(n, INT_MAX);
        vector<bool> tmp(n, false);
        vector<vector<bool> > p(n, tmp);
        for(int i=0; i<n; i++){
            for(int j=i; j>-1; j--){
                if(s[i] == s[j] && (i-j < 2 || p[j+1][i-1])){
                    p[j][i] = true;
                    dp[i] = min(dp[i], j == 0? 0 : dp[j-1] + 1);
                }
            }
        }
        return dp.back();
    }
};

 

posted @ 2018-09-22 10:58  vercont  阅读(85)  评论(0编辑  收藏  举报