-
Notifications
You must be signed in to change notification settings - Fork 0
/
131-PalindromePartitioning.cpp
61 lines (58 loc) · 2.02 KB
/
131-PalindromePartitioning.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
/*=============================================================================
# FileName: 131-PalindromePartitioning.cpp
# Desc: AC, 16ms
# Author: Jian Huang
# Email: [email protected]
# HomePage: https://cn.linkedin.com/in/huangjian1993
# Version: 0.0.1
# LastChange: 2015-08-29 16:15:39
# History:
=============================================================================*/
#include <leetcode.h>
class Solution {
public:
void dfs(vector<string> &ret, int index, const vector<vector<bool> > &dp, const string &s, vector<vector<string> > &result) {
int len = s.length();
if (index == len) {
result.push_back(ret);
return ;
}
for (int i = index; i < len; i ++) {
if (dp[index][i]) {
ret.push_back(s.substr(index, i - index + 1));
dfs(ret, i + 1, dp, s, result);
ret.pop_back();
}
}
}
vector<vector<string> > partition(string s) {
vector<vector<string> > result;
vector<string> ret;
if (s == "") {
return result;
}
int len = s.length();
vector<vector<bool> > dp(len, vector<bool>(len, false));
for (int i = len - 1; i >= 0; i --) {
for (int j = len - 1; j >= i; j --) {
if (s[i] == s[j] && (j - i <= 1 || dp[i + 1][j - 1])) {
dp[i][j] = true;
} else {
dp[i][j] = false;
}
}
}
dfs(ret, 0, dp, s, result);
return result;
}
};
int main() {
Solution solution;
vector<vector<string> > result = solution.partition("aabbbb");
for (int i = 0; i < (int) result.size(); i ++) {
for (int j = 0; j < (int) result[i].size(); j ++) {
cout << result[i][j] << " ";
}
cout << endl;
}
}