不戚戚于贫贱,不汲汲于富贵 ---五柳先生
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode"
,
dict = ["leet", "code"]
.
Return true because "leetcode"
can be segmented as "leet code"
.
搜索--》自顶向下的动态规划(备忘录法)》自底向下动态规划
这些思想一直在使用中,今天是7.8号,看了搜索的书籍,期中讲的是利用字典分词,所以觉得这个题目值得一做。今天用了3个多小时ac了,其实我把动态规划方程搞错了。
dp[i][j] 表示 以 j开始长度为 i的字符串是否符合条件,这个事最核心的,
举个例子: “abc”
第一层循环判断 a b c 是否则集合中,
第二层 ab bc 是否在集合中
第三层 abc 是否符合条件
每一层只要利用上面一层的信息,比如 判断 abc,只要判断 (abc) , (a&&bc) ,(ab&&c) 三个是否满足条件
1.递归(搜索)
学习递归时候,老师说他的好处 是简单,其实递归的思路是暴力搜索的方法,所以写的人需要考虑的问题很少,但是栈的调用很耗时,同时可能会出现栈溢出。但是他只做自己需要干的事情,,同时他需要干的事情他会重复做。这为后面的自顶向下的优化做了铺垫。
public class Solution { public boolean wordBreak(String s, Set<String> dict) { if(s=="") return true; for(int i=0;i<s.length();i++) { String s1=s.substring(0,i+1); //枚举所有以0为开头的的字符串 if(dict.contains(s1)&&wordBreak(s.substring(i+1),dict)) { return true; } } return false; } }
结果是:
Last executed input:"acaaaaabbbdbcccdcdaadcdccacbcccabbbbcdaaaaaadb", ["abbcbda","cbdaaa","b","dadaaad","dccbbbc","dccadd","ccbdbc","bbca","bacbcdd","a","bacb","cbc","adc","c","cbdbcad","cdbab","db","abbcdbd","bcb","bbdab","aa","bcadb","bacbcb","ca","dbdabdb","ccd","acbb","bdc","acbccd","d","cccdcda","dcbd","cbccacd","ac","cca","aaddc","dccac","ccdc","bbbbcda","ba","adbcadb","dca","abd","bdbb","ddadbad","badb","ab","aaaaa","acba","abbb"]
2.超时了,如何优化呢,博主在一本书上看过,是自顶向下动态规划,同学们可以查查,其实就是记住了他重复做的事情。
dp[i][j]表示两者之间是否在词典中。
dp[i][j]=dp[i][k] && dp[k+1][j] (i+1=<k<j-1)
public class Solution { // int dp(int i,int j,String s,Set<String> dict,int d[][]) // get the i to j is exit in the dict { if(d[i][j]==1) return 1; if(d[i][j]==-1) return -1; if(dict.contains(s.substring(i,j+1))) { d[i][j]=1; return 1; } else { for(int k=i;k<j;k++) { if(dp(i,k,s,dict,d)==1&&dp(k+1,j,s,dict,d)==1) { d[i][j]=1; return 1; } } return -1; } } public boolean wordBreak(String s, Set<String> dict) { int len=s.length(); int d[][]=new int[len][len]; int ans=dp(0,len-1,s,dict,d); if(ans==1) return true; return false; } }
错了,还是超时,
3.最后的自定向下方法了,填表,那些专业人士都叫打表。(AC)
1 public class Solution { 2 public boolean wordBreak(String s, Set<String> dict) { 3 if(s=="") return true; 4 int len=s.length(); 5 boolean dp[][]=new boolean[len][len]; 6 7 // 8 for(int i=0;i<len;i++) 9 { 10 for(int j=0;j<len-i;j++) 11 { 12 13 14 String s2=s.substring(j,j+i+1); 15 if(dict.contains(s2)) 16 { 17 dp[i][j]=true; 18 continue; 19 20 } 21 22 for(int k=j+1;k<=j+i+1;k++) 23 { 24 25 if(dp[k-j-1][j]&&dp[j+i-k][k]) 26 { 27 dp[i][j]=true; 28 break; 29 } 30 } 31 32 33 34 35 36 37 38 } 39 } 40 41 42 43 return dp[len-1][0]; 44 45 46 47 } 48 }
下面解决把分词结果输出来