@@ -27,32 +27,32 @@ public static void main(String[] strs) {
27
27
28
28
System .out .println ("Test" );
29
29
30
- Algorithms .permutation .Stopwatch timer1 =new Algorithms .permutation .Stopwatch ();
31
-
32
- // 递归模板,加剪枝
33
- List <String >list =wordBreak (s ,dict );
34
-
35
- System .out
36
- .println ("Computing time with dfs and cut branch used as Queue/Deque: "
37
- +timer1 .elapsedTime () +" millisec." );
38
-
39
30
Algorithms .permutation .Stopwatch timer2 =new Algorithms .permutation .Stopwatch ();
40
31
41
32
// HASH保存记忆
42
33
wordBreak1 (s ,dict );
43
34
44
35
System .out
45
- .println ("Computing time withArrayDeque used as Queue/Deque : "
36
+ .println ("Computing time withDFS1 : "
46
37
+timer2 .elapsedTime () +" millisec." );
47
38
39
+ Algorithms .permutation .Stopwatch timer1 =new Algorithms .permutation .Stopwatch ();
40
+
41
+ // 递归模板,加剪枝
42
+ wordBreak (s ,dict );
43
+
44
+ System .out
45
+ .println ("Computing time with DFS2: "
46
+ +timer1 .elapsedTime () +" millisec." );
47
+
48
48
Algorithms .permutation .Stopwatch timer3 =new Algorithms .permutation .Stopwatch ();
49
49
50
50
// DFS+ 剪枝 3: 设置Flag 变量
51
51
//http://fisherlei.blogspot.com/2013/11/leetcode-wordbreak-ii-solution.html
52
52
wordBreak3 (s ,dict );
53
53
54
54
System .out
55
- .println ("Computing time withArrayDeque used as Queue/Deque : "
55
+ .println ("Computing time withDFS3 : "
56
56
+timer3 .elapsedTime () +" millisec." );
57
57
58
58
//System.out.println(list.toString());
@@ -207,32 +207,6 @@ public static boolean iswordBreak(String s, Set<String> dict) {
207
207
return D [len ];
208
208
}
209
209
210
- /*
211
- // 解法3:重新剪枝。
212
- */
213
-
214
- // 我们用DFS来解决这个问题吧
215
- public static List <String >wordBreak3 (String s ,Set <String >dict ) {
216
- if (s ==null ||s .length () ==0 ||dict ==null ) {
217
- return null ;
218
- }
219
-
220
- List <String >ret =new ArrayList <String >();
221
-
222
- // 记录切割过程中生成的字母
223
- List <String >path =new ArrayList <String >();
224
-
225
- int len =s .length ();
226
- boolean canBreak [] =new boolean [len ];
227
- for (int i =0 ;i <len ;i ++) {
228
- canBreak [i ] =true ;
229
- }
230
-
231
- dfs3 (s ,dict ,path ,ret ,0 ,canBreak );
232
-
233
- return ret ;
234
- }
235
-
236
210
// 我们用DFS模板来解决这个问题吧
237
211
public static void dfs3 (String s ,Set <String >dict ,
238
212
List <String >path ,List <String >ret ,int index ,
@@ -256,23 +230,25 @@ public static void dfs3(String s, Set<String> dict,
256
230
return ;
257
231
}
258
232
259
- boolean flag =false ;
260
233
for (int i =index ;i <len ;i ++) {
261
234
// 注意这些索引的取值。左字符串的长度从0到len
262
235
String left =s .substring (index ,i +1 );
263
- if (!dict .contains (left )) {
236
+ if (!dict .contains (left ) || ! canBreak [ i + 1 ] ) {
264
237
// 如果左字符串不在字典中,不需要继续递归
265
238
continue ;
266
239
}
267
240
268
241
// if can't find any solution, return false, other set it
269
242
// to be true;
270
- flag =true ;
271
243
path .add (left );
244
+
245
+ int beforeChange =ret .size ();
272
246
dfs3 (s ,dict ,path ,ret ,i +1 ,canBreak );
247
+ // 注意这些剪枝的代码. 关键在于此以减少复杂度
248
+ if (ret .size () ==beforeChange ) {
249
+ canBreak [i +1 ] =false ;
250
+ }
273
251
path .remove (path .size () -1 );
274
252
}
275
-
276
- canBreak [index ] =flag ;
277
253
}
278
254
}