回溯+动态规划
class Solution {
public:
vector<vector<string>> res;
vector<vector<int>> f;
vector<string> temp;
int n = 0;
void dfs(string& s, int k){
if(k == n) {
res.push_back(temp);
return;
}
for(int i = k; i < n; ++i){
if(f[k][i]){
temp.push_back(s.substr(k, i - k + 1));
dfs(s, i + 1);
temp.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
n = s.size();
f.assign(n, vector<int>(n, true));
for(int i = n - 1; i >= 0; --i){
for(int j = i + 1; j < n; ++j){
f[i][j] = (s[i] == s[j]) && f[i + 1][j - 1];
}
}
dfs(s, 0);
return res;
}
};