-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcode131.cpp
47 lines (46 loc) · 1.25 KB
/
lcode131.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
//DFS+DP
class Solution {
public:
vector<vector<int>> palind_dp(string s){
int n=s.size();
vector<vector<int>> res(n+1,vector<int>(n+1,0));
for(int i=1;i<n+1;i++)
res[i][i]=1;
for(int i=2;i<n+1;i++){
if(s[i-1]==s[i-2])
res[i][i-1]=1;
}
for(int l=1;l<n;l++){
for(int i=1;i<n-l+1;i++){
int j=i+l;
if(s[i-1]==s[j-1])
res[i][j]=res[i+1][j-1];
}
}
return res;
}
void dfs(string s,vector<vector<string>> &res,vector<string> tmp,int idx,vector<vector<int>>& dp){
if(idx==s.size()){
res.push_back(tmp);
return;
}
for(int i=idx;i<s.size();i++){
if(dp[idx+1][i+1]){
string t=s.substr(idx,i-idx+1);
tmp.push_back(t);
dfs(s,res,tmp,i+1,dp);
tmp.pop_back();
}
}
return;
}
vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<string> tmp;
int n=s.size();
if(n==0)return res;
vector<vector<int>> dp=palind_dp(s);
dfs(s,res,tmp,0,dp);
return res;
}
};