forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordBreak.II.cpp
236 lines (203 loc) · 6.25 KB
/
wordBreak.II.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Source : https://oj.leetcode.com/problems/word-break-ii/
// Author : Hao Chen
// Date : 2014-07-03
/**********************************************************************************
*
* Given a string s and a dictionary of words dict, add spaces in s to construct a sentence
* where each word is a valid dictionary word.
*
* Return all such possible sentences.
*
* For example, given
* s = "catsanddog",
* dict = ["cat", "cats", "and", "sand", "dog"].
*
* A solution is ["cats and dog", "cat sand dog"].
*
*
**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;
// ---------------
// Recursive Way
// ---------------
// The recursive method is quite straight forward.
//
// 1) if a substring from 0 to i is a word, then take the rest substring to evaluate.
// 2) during the recursion, keep tracking the result
//
// For example:
//
// s = "catsanddog",
// dict = ["cat", "cats", "and", "sand", "dog"].
//
//
// +---> sand / dog ---> dog
// |
// +-------> cat / sanddog
// |
// catsanddog
// |
// +------> cats / anddog
// |
// +----> and / dog ---> dog
//
//
// However, the recursive could produce a lot duplicated calculation, we need use a cache to avoid.
//
//To avoid time limit error, need to add cache
vector<string> wordBreak(string s, set<string> &dict, map<string, vector<string> >& cache) {
if (cache.find(s)!=cache.end()){
return cache[s];
}
vector<string> result;
for(int i=0; i<s.size(); i++){
string w = s.substr(0,i+1);
if (dict.find(w)!=dict.end()) {
if (i==s.size()-1){
result.push_back(w);
break;
}
vector<string> ret = wordBreak(s.substr(i+1, s.size()-i-1), dict, cache);
for(int j=0; j<ret.size(); j++){
result.push_back( w + " " + ret[j] );
}
}
}
cache[s] = result;
return result;
}
//Time limit error
void wordBreak(string s, set<string> &dict, string str, vector<string>& result) {
string org_str = str;
for(int i=0; i<s.size(); i++){
string w = s.substr(0,i+1);
// if the current substring is a word
if (dict.find(w)!=dict.end()) {
str = org_str;
if (str.size()>0){
str +=" ";
}
str = str + w;
// foud the solution, add it into the result
if (i==s.size()-1){
result.push_back(str);
return;
}
//recursively to solve the rest subarray
wordBreak(s.substr(i+1, s.size()-i-1), dict, str, result);
}
}
}
//---------------------
// Dynamic Programming
//---------------------
//
// Define substring[i, j] is the sub string from i to j.
//
// (substring[i,j] == word) : result[i] = substring[i,j] + {result[j]}
//
// So, it's better to evaluate it backword.
//
// For example:
//
// s = "catsanddog",
// dict = ["cat", "cats", "and", "sand", "dog"].
//
// 0 c "cat" -- word[0,2] + {result[3]} ==> "cat sand dog"
// "cats" -- word[0,3] + {result[4]} ==> "cats and dog"
// 1 a ""
// 2 t ""
// 3 s "sand" -- word[3,6] + {result[7]} ==> "sand dog"
// 4 a "and" -- word[4,6] + {result[7]} ==> "and dog"
// 5 n ""
// 6 d ""
// 7 d "dog"
// 8 o ""
// 9 g ""
vector<string> wordBreak_dp(string s, set<string> &dict) {
vector< vector<string> > result(s.size());
for(int i=s.size()-1; i>=0; i--) {
vector<string> v;
result[i] = v;
for(int j=i+1; j<=s.size(); j++) {
string word = s.substr(i, j-i);
if (dict.find(word) != dict.end()){
if (j==s.size()){
result[i].push_back(word);
}else{
for(int k=0; k<result[j].size(); k++){
result[i].push_back(word + " " + result[j][k]);
}
}
}
}
}
return result[0];
}
vector<string> wordBreak(string s, set<string> &dict) {
vector<string> result;
switch (random()%3)
{
case 0:
{
cout << "---------Recursive Solution--------" << endl;
string str;
wordBreak(s, dict, str, result);
}
break;
case 1:
{
cout << "----Memorized Recursive Solution----" << endl;
map<string, vector<string> > cache;
result = wordBreak(s, dict, cache);
}
break;
case 2:
cout << "----Dynamic Programming Solution----" << endl;
result = wordBreak_dp(s, dict);
break;
}
return result;
}
void printVector(vector<string>& v)
{
for(int i=0; i<v.size(); i++){
cout << v[i] <<endl;
}
}
int main()
{
srand(time(NULL));
string d[]={"cat", "cats", "and", "sand", "dog"};
set<string> dict(d, d+5);
string s = "catsanddog";
vector<string> v = wordBreak(s, dict);
printVector(v);
string d0[]={"apple","app","le","pie"};
dict.clear();
dict.insert(d0, d0+4);
s = "applepie";
v = wordBreak(s, dict);
printVector(v);
string d1[]={"aaaa","aa","a"};
dict.clear();
dict.insert(d1, d1+3);
s = "aaaaaaa";
v = wordBreak(s, dict);
printVector(v);
string d5[]={"a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"};
dict.clear();
dict.insert(d5, d5+10);
s="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
v = wordBreak(s, dict);
printVector(v);
return 0;
}