-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path分割回文串.cpp
86 lines (74 loc) · 1.7 KB
/
分割回文串.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <queue>
#include <functional>
using namespace std;
/*
一开始没什么思路,参考了https://blog.csdn.net/u012501459/article/details/46792453
例如分割字符串"abcbad"。
1.分成"a"和"bcbad",判断"a"是否回文
2.将"bcbad"分成"b"和"cbad",判断"b"是否回文3
重复,将原字符串切割成一个一个字母,如a/b/c/b/a/d/。当切割到最后,压入result中
然后回溯a/b/c/b/ad,判断"ad"是否是回文,再回溯成a/b/c/bad,判断"bad"是否是回文。
其中在判断"bad"是否是回文,会切割成ba/d来进行再次判断。
其中,这里需要引入一个pos来判断是否切割到最后,而不是根据path.size()来判断
*/
class Solution
{
public:
vector<vector<string>> partition(string s)
{
vector<vector<string>> result;
vector<string> path;
partition(s, 0, result, path);
return result;
}
private:
void partition(string s, int pos, vector<vector<string>> &result, vector<string> &path)
{
if (pos == s.size())
{
result.push_back(path);
return;
}
for (int i = pos; i < s.size(); ++i)
{
string tmp = s.substr(pos, i - pos + 1);
if (check(tmp))
{
path.push_back(tmp);
partition(s, i + 1, result, path);
path.pop_back();
}
}
}
bool check(string s)
{
int beg = 0, end = s.size() - 1;
while (beg < end)
{
if (s[beg++] != s[end--])
return false;
}
return true;
}
};
int main()
{
string s = "abcbcad";
Solution a;
auto b = a.partition(s);
for (auto m : b)
{
for (auto n : m)
cout << n << " ";
cout << endl;
}
system("pause");
return 0;
}