#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
//判断某字符是否为回文字符串
bool isPartition(string str) {
int leftboundary = str.size() / 2;
for (int i = 0; i < leftboundary; i++) {
if (str[i] != str[str.size() - i-1])
return false;
}
return true;
}
void DFS(string s, vector<vector<string>> &res, vector<string> &tem, int index,int size) {
if (s.size()==0) {//一开始用index==s.size用作终止判断条件,不对,因为每次递归传过来的s的size变了
res.push_back(tem);
return;//这个return 可以不要,因为当这个if满足时,s,size()=0,for循环不能执行。
}
for (int i = 1; i <= s.size(); i++) {//尝试分割方法
string str = s.substr(0, i);
//检验截取的str是否为回文数字
if (isPartition(str)) {
tem.push_back(str);
DFS(s.substr(i,s.size()-i), res, tem, index + 1,size);
tem.pop_back();//一开始这个写道了if外边,不太对。
}
}
return;
}
vector<vector<string>> partition(string s) {
vector<vector<string>> res;//用于最终保存结果
vector<string>tem;//用于暂时保存某一个结果
int index = 0;//递归深度
int size = s.size();
DFS(s, res, tem, index,size);
return res;
}
};
int main() {
Solution solution;
string s = "aab";
string s1 = "aa";
bool ce = solution.isPartition(s1);
vector<vector<string>> res = solution.partition(s);
return 0;
}