/*
DFS
palindrome partation
input ['"aab"]
output ["a","aa","b"]
*/
#include <iostream>
#include<string>
#include<vector>
using namespace std;
class Solution
{
public:
vector<vector<string>> Subset(string s)
{
vector<vector<string>>result;
vector<string>path;
DFS(s, path, 0, result);
return result;
}
void DFS(string &s, vector<string>&path, int start, vector<vector<string>>&result)
{
if (start == s.size())
{
result.push_back(path);
return;
}
for (int i = start; i < s.size(); i++)
{
if (IsPalindrome(s, start, i))//从i处砍一刀
{
path.push_back(s.substr(start, i - start + 1));
DFS(s, path, i + 1, result);//继续往下砍
path.pop_back();//返回上一层
}
}
}
bool IsPalindrome(string &s, int start, int end)
{
while (start < end && s[start]==s[end])
{
++start;
--end;
}
return start >= end;
}
};
int main()
{
Solution s1;
string s { "adsjhtfyyuui" };
vector<vector<string>>result;
result = s1.Subset(s);
for (int i = 0; i < result.size(); i++)
{
for (int j = 0; j < result[i].size(); j++)
{
cout << result[i][j]<<",";
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}
palindrome
最新推荐文章于 2021-01-16 16:28:37 发布