-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathsolution.cpp
66 lines (60 loc) · 1.61 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Solution
{
public:
vector<string> result;
void printStack(stack<string> stk)
{
string output = "";
while(!stk.empty())
{
if(output == "")
output += stk.top();
else
output = output + " " + stk.top();
stk.pop();
}
result.push_back(output);
}
void check(vector<vector<int> > &v, int t, stack<string> stk, string s)
{
if(t == -1)
{
printStack(stk);
return ;
}
else
{
for(vector<string>::size_type st = 0; st < v[t].size(); st ++)
{
stk.push(s.substr(v[t][st]+1, t-v[t][st]));
check(v, v[t][st], stk, s);
stk.pop();
}
}
}
vector<vector<int> > buildv(string s, unordered_set<string> &dict)
{
vector<vector<int> > v(s.length());
for(string::size_type st1 = 0; st1 < s.length(); st1 ++)
{
for(string::size_type st2 = 0; st2 <= st1; st2 ++)
{
if(dict.find(s.substr(st2, st1-st2+1)) != dict.end())
{
if(st2 == 0)
v[st1].push_back(-1);
else if(!v[st2-1].empty())
v[st1].push_back(st2-1);
}
}
}
return v;
}
vector<string> wordBreak(string s, unordered_set<string> &dict)
{
vector<vector<int> > v = buildv(s, dict);
stack<string> stk;
check(v, v.size()-1, stk, s);
return result;
}
};