forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
30 lines (30 loc) · 837 Bytes
/
Solution.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
class Solution {
public:
vector<vector<string>> partition(string s) {
int n = s.size();
bool f[n][n];
memset(f, true, sizeof(f));
for (int i = n - 1; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
f[i][j] = s[i] == s[j] && f[i + 1][j - 1];
}
}
vector<vector<string>> ans;
vector<string> t;
auto dfs = [&](this auto&& dfs, int i) -> void {
if (i == n) {
ans.emplace_back(t);
return;
}
for (int j = i; j < n; ++j) {
if (f[i][j]) {
t.emplace_back(s.substr(i, j - i + 1));
dfs(j + 1);
t.pop_back();
}
}
};
dfs(0);
return ans;
}
};