暴力枚举
const int MAXN = 1000 + 10;
string s;
int is[MAXN][MAXN];
bool isPl(int left, int right){
if(left >= right)
return true;
int& ret = is[left][right];
if(ret != -1)
return ret;
return ret = (s[left] == s[right] && isPl(left + 1, right - 1));
}
vector<vector<string> > dfs(int left, int right){
vector<vector<string> > ret;
if(left > right){
ret.resize(1);
return ret;
}
for(int i = left; i <= right; ++ i)
if(isPl(left, i)){
vector<vector<string>> sub = dfs(i + 1, right);
for(int j = 0; j < sub.size(); ++ j){
sub[j].push_back(s.substr(left, i - left + 1));
ret.push_back(sub[j]);
}
}
return ret;
}
class Solution {
public:
vector<vector<string>> partition(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
::s = s;
memset(is, 0xff, sizeof(is));
vector<vector<string>> ret = dfs(0, s.size() - 1);
for(int i = 0; i < ret.size(); ++ i)
reverse(ret[i].begin(), ret[i].end());
return ret;
}
};