vector<string> tmp_result;
vector<vector<string> > result;
vector<vector<bool> > is_division;
int str_length;
void dpDivision(const string &str)
{
for(int i = str_length - 1; i >= 0; --i)
{
is_division[i][i] = true;
for(int j = i + 1; j < str_length; ++j)
{
/* 二个字符相等 && 二个挨着 或者 左右各减去一个是回文 */
if(str[i] == str[j] && (j == i+1 || is_division[i+1][j-1]))
is_division[i][j] = true;
}
}
}
void backTracking(string &str, int current_index)
{
if(current_index == str_length)
{
result.push_back(tmp_result);
return;
}
for(int index = current_index; index < str_length; ++index)
{
if(is_division[current_index][index])
{
tmp_result.push_back(str.substr(current_index, index - current_index + 1));
backTracking(str, index + 1);
tmp_result.pop_back();
}
}
}
vector<vector<string> > partition(string s)
{
if(s.empty())
return result;
str_length = s.size();
is_division.assign(str_length, vector<bool>(str_length, false));
dpDivision(s);
backTracking(s, 0);
return result;
}
vector<vector<string> > result;
vector<vector<bool> > is_division;
int str_length;
void dpDivision(const string &str)
{
for(int i = str_length - 1; i >= 0; --i)
{
is_division[i][i] = true;
for(int j = i + 1; j < str_length; ++j)
{
/* 二个字符相等 && 二个挨着 或者 左右各减去一个是回文 */
if(str[i] == str[j] && (j == i+1 || is_division[i+1][j-1]))
is_division[i][j] = true;
}
}
}
void backTracking(string &str, int current_index)
{
if(current_index == str_length)
{
result.push_back(tmp_result);
return;
}
for(int index = current_index; index < str_length; ++index)
{
if(is_division[current_index][index])
{
tmp_result.push_back(str.substr(current_index, index - current_index + 1));
backTracking(str, index + 1);
tmp_result.pop_back();
}
}
}
vector<vector<string> > partition(string s)
{
if(s.empty())
return result;
str_length = s.size();
is_division.assign(str_length, vector<bool>(str_length, false));
dpDivision(s);
backTracking(s, 0);
return result;
}