1957

无聊蛋疼的1957写的低端博客
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[leetcode]Palindrome Partitioning

Posted on 2013-12-30 20:27  1957  阅读(139)  评论(0编辑  收藏  举报

暴力= =

枚举断开位置...

据说可以dp,等下试试

 

class Solution {
public:
    bool isPal(const string& s , int start , int end){
        end --;
        while(start < end){
            if(s[start] != s[end]) return false;
            start ++ ; end --;
        }
        return true;
    }
    void search(const string& s , vector<vector<string> > &ans , vector<string>& path , int prev , int end){
        if(end >= s.size()){
            if(isPal(s , prev , end)){
                path.push_back(s.substr(prev , end-prev));
                ans.push_back(path);
                path.pop_back();
            }
            return;
        }
        
        //not break
        search(s , ans , path , prev , end + 1);
        //break
        if(isPal(s , prev , end)){
            path.push_back(s.substr(prev,end-prev));
            search(s , ans , path , end  , end + 1);
            path.pop_back();
        }
    }
    
    vector<vector<string>> partition(string s) {
        
        vector<vector<string> >ans;
        if(s.size() == 0) return ans;
        vector<string> path;
        search(s , ans , path , 0 , 1);
        return ans;
    }
};