forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Palindrome_Partitioning.cc
52 lines (48 loc) · 1.34 KB
/
Palindrome_Partitioning.cc
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
class Solution {
public:
vector<vector<string>> ans;
vector<string> vt;
vector<vector<int>> g;
int len;
bool judge(string &s, int i, int j) {
if (i > j)
return false;
if (g[i][j] != -1)
return g[i][j];
if (i == j)
return g[i][j] = 1;
if (s[i] != s[j])
return g[i][j] = 0;
else {
if (i + 1 == j)
return g[i][j] = 1;
return g[i][j] = judge(s, i + 1, j - 1);
}
}
void dfs(string &s, int cnt) {
if (cnt == len) {
ans.push_back(vt);
return;
}
for (int i = len - 1; i >= cnt; i--) {
if (judge(s, cnt, i)) {
vt.push_back(s.substr(cnt, i - cnt + 1));
dfs(s, i + 1);
vt.pop_back();
}
}
}
vector<vector<string>> partition(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ans.clear(), vt.clear(), g.clear();
len = s.length();
vector<int> tmp;
for (int i = 0; i < len; i++)
tmp.push_back(-1);
for (int i = 0; i < len; i++)
g.push_back(tmp);
dfs(s, 0);
return ans;
}
};