-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
52 lines (48 loc) · 1.22 KB
/
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
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:
string s;
vector<vector<bool> > DP;
vector<vector<string> > result;
void solve(int index, vector<string> tempResult)
{
if(index == s.size())
result.push_back(tempResult);
else
{
for(int i = index;i<DP[index].size();i++)
{
if(DP[index][i])
{
tempResult.push_back(s.substr(index,i-index+1));
solve(i+1,tempResult);
tempResult.pop_back();
}
}
}
}
vector<vector<string> > partition(string s)
{
DP.resize(s.size(),vector<bool>(s.size(),false));
for(int i=0;i<DP.size();i++)
{
DP[i][i]=true;
if( i<DP.size()-1 && s.at(i) == s.at(i+1) )
DP[i][i+1] = true;
}
for(int len=3;len<=s.size();len++)
{
for(int i=0;i+len<=s.size();i++)
{
int j=i+len-1;
if( DP[i+1][j-1] && s[i] == s[j] )
{
DP[i][j]=true;
}
}
}
this->s = s;
solve(0,vector<string>());
return result;
}
};