- Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathpalindrome_partitioning.cpp
50 lines (47 loc) · 961 Bytes
/
palindrome_partitioning.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
#include<vector>
usingnamespacestd;
classSolution {
public:
enum
{
INIT = 0,
YES = 1,
NO = 2,
};
vector<vector<int> > matrix;
public:
vector<vector<string>> partition(string s) {
matrix = vector<vector<int> >(s.size(), vector<int>(s.size(), INIT));
vector<vector<string>> res;
vector<string> path;
DFS(s, 0, res, path);
return res;
}
voidDFS(string s, int start, vector<vector<string>>& res, vector<string>& path){
if (start==s.size()){
res.push_back(path);
return;
}
for (int i = start; i < s.size(); ++i){
if (matrix[start][i]==INIT){
if (is_partition(s, start, i))
matrix[start][i] = YES;
else
matrix[start][i] = NO;
}
if (matrix[start][i]==YES){
path.push_back(s.substr(start, i-start+1));
DFS(s, i+1, res, path);
path.pop_back();
}
}
}
boolis_partition(string s, int i, int j){
while (i<j){
if (s[i]!=s[j]) returnfalse;
++i;
--j;
}
returntrue;
}
};