LeetCode ||& Word Break && Word Break II(轉)——動態規划


一、

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".

 

 1 class Solution {
 2 public:
 3     bool wordBreak(string s, unordered_set<string> &dict) {
 4         int len=s.length();
 5         vector<bool> v(len+1,false);
 6         v[0]=true;
 7         for(int pos=0;pos<len;pos++){
 8             for(int i=pos;v[pos]&&i<len;i++){
 9                 if(dict.find(s.substr(pos,i-pos+1))!=dict.end())
10                     v[i+1]=true;
11             }
12         }
13         return v[len];
14     }
15 };

二、

 

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"].

即不僅要確定字符串是否能被字典分割,還要找出所有可能的組合。參考word break那題的DP思路,首先,從尾部開始逆向看字符串 s ,循環截取一個存在的詞(milestone 1),然后在截取的位置遞歸,繼續向前看,繼續截取。。。知道到達頭部,此時組合出一種答案;然后進入milestone 1 處的下一次循環,如下圖的milestone 1,截取另外一個詞,找另外一個答案。。。

 

 

代碼如下:

 

 

 
  1.  1 class Solution {  
     2     vector<string> midres;  
     3     vector<string> res;  
     4     vector<bool> *dp;  
     5 public:  
     6     vector<string> wordBreak(string s, unordered_set<string> &dict) {  
     7         int len = s.length();  
     8           
     9         dp = new vector<bool>[len];  
    10         for(int i=0; i<len; ++i){  
    11             for(int j=i; j<len; ++j){  
    12                 if(dict.find(s.substr(i, j-i+1))!=dict.end()){  
    13                     dp[i].push_back(true);      //第二維的下標實際是:單詞長度-1  
    14                 }else{  
    15                     dp[i].push_back(false);     //數組第二維用vector,size不一定是n,這樣比n*n節省空間  
    16                 }  
    17             }  
    18         }  
    19         func(s, len-1);  
    20         return res;  
    21     }  
    22       
    23     void func(const string &s, int i){  
    24         if(i>=0){  
    25             for(int j=0; j<=i; ++j){  
    26                   
    27                 if(dp[j][i-j]){ //注意此處的第二個下標是 i-j,不是i,因為數組的第二維長度是不固定的,第二維的下標實際是單詞長度-1  
    28                   
    29                     midres.push_back(s.substr(j, i-j+1));  
    30                     func(s, j-1);  
    31                     midres.pop_back();  //繼續考慮for循環的下一個分段處  
    32                 }  
    33             }  
    34             return;  
    35         }  
    36         else{  
    37             string str;  
    38             for(int k=midres.size()-1; k>=0; --k){  //注意遍歷的順序是倒序的  
    39                 str += midres[k];   //注意此處是k,不是i  
    40                 if(k>0)  
    41                     str += " ";  
    42             }  
    43             res.push_back(str);  
    44             return;  
    45         }  
    46     }  
    47 };  

     

 
        


注意遞歸函數的技巧,用全局變量res來保存答案,每次遞歸成功到達頭部時將此中間結果保存到res。

轉自:http://blog.csdn.net/jiadebin890724/article/details/34829865


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM