leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

https://leetcode.com/problems/palindrome-partitioning/

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

Return all possible palindrome partitioning of s.

For example, given s = "aab",
Return

  [
    ["aa","b"],
    ["a","a","b"]
  ]
class Solution {
public:
    void Parlindrome(vector<vector<bool> >& isPal, string& s) {
        for(int i=0;i<isPal.size();++i) {
            for(int j=0;j<=i;++j) {
                isPal[i][j] = true;
            }
        }
        
        for(int len=2;len<=s.length();++len) {
            for(int i=0;i+len-1<s.length();++i) {
                int j=i+len-1;
                if(i==j || (s[i]==s[j] && isPal[i+1][j-1])) isPal[i][j] = true;
            }
        }
        
    }
    void dfs(vector<vector<string> >& res, vector<string>& load, vector<vector<bool> >& isPal, string& s, int idx) {
        if(idx == s.length()) {
            res.push_back(load);
            return;
        }
        
        for(int nx=idx;nx<s.length();++nx) {
            if(isPal[idx][nx]) {
                load.push_back(s.substr(idx, nx-idx+1));
                dfs(res, load, isPal, s, nx+1);
                load.pop_back();
            }
        }
    }
    vector<vector<string>> partition(string s) {
        vector<vector<bool> > isPal(s.length(), vector<bool>(s.length(), false));
        vector<string> load; load.clear();
        vector<vector<string> > res;
        
        Parlindrome(isPal, s);
        dfs(res, load, isPal, s, 0);
        return res;
    }
};
leetcode 131: Palindrome Partitioning

 

https://leetcode.com/problems/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.

class Solution {
public:
    void Parlindrome(vector<vector<bool> >&isPal, string& s) {
        for(int i=1;i<s.length();++i) isPal[i][i-1] = true;
        for(int len=1;len<=s.length();++len) {
            for(int i=0;i+len-1<s.length();++i) {
                int j=i+len-1;
                if(i==j || (s[i]==s[j] && isPal[i+1][j-1])) isPal[i][j] = true;
            }
        }
    }
    void minCut(vector<vector<bool> >& isPal, vector<int>& cut, string& s) {
        for(int i=0;i<s.length();++i) {
            if(isPal[0][i] == true) {
                cut[i] = 0;
                continue;
            }
            
            for(int pre=0;pre<i;++pre) {
                if(isPal[pre+1][i]) {
                    cut[i] = min(cut[pre]+1, cut[i]);
                }
                else cut[i] = min(cut[pre]+i-pre, cut[i]);
            }
        }
    }
    int minCut(string s) {
        vector<int> cut(s.length(), INT_MAX);
        vector<vector<bool> > isPal(s.length(), vector<bool>(s.length(), false));
        
        Parlindrome(isPal, s);
        minCut(isPal, cut, s);
        return cut[s.length()-1];
    }
};
leetcode 132: Palindrome Partitioning II

 

posted @ 2015-11-28 17:47  流白  阅读(248)  评论(0编辑  收藏  举报