[LeetCode] Word Break II

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

PS:use DFS to solve the follow up question

  1 #include <iostream>
  2 #include <string>
  3 #include <set>
  4 #include <vector>
  5 #include <cstdio>
  6 
  7 using namespace std;
  8 
  9 set<string> myset;
 10 vector<string> vstring;
 11 bool ** pmatrix = NULL;
 12 string strtomatch;
 13 
 14 template<typename T>
 15 void BuildMatrix(T *** pmaze,unsigned row_num,unsigned column_num)
 16 {
 17     *pmaze = new T*[row_num];
 18     for(unsigned i=0;i<row_num;++i){
 19         (*pmaze)[i] = new T[column_num];
 20     }
 21 }
 22 
 23 template<typename T>
 24 void ReleaseMatrix(T ***pmaze,unsigned row_num)
 25 {
 26     if(!pmaze) return;
 27 
 28     for(unsigned i=0;i<row_num;++i){
 29         delete [](*pmaze)[i];
 30     }
 31 
 32     delete [](*pmaze);
 33 }
 34 
 35 void ListSentences(int i,int j,int length,string &strdata){
 36     if(i>=length) return;
 37     unsigned originallength = 0;
 38     for(i=++j;j<length;++j){
 39         if(pmatrix[i][j]){
 40             originallength = strdata.length();
 41             strdata.append(strtomatch,i,j-i+1);strdata.append(" ");
 42 
 43             if(j==length-1){
 44                 vstring.push_back(strdata);
 45             }else{
 46                 strdata.append(" ");
 47                 ListSentences(i,j,length,strdata);
 48                 strdata.resize(originallength);
 49             }
 50         }
 51     }
 52 }
 53 
 54 void Solve(){
 55     myset.clear();
 56 
 57     int dicwordnum = 0;
 58     cin>>dicwordnum;
 59 
 60     for(int i=0;i<dicwordnum;++i){
 61         cin>>strtomatch;
 62         myset.insert(strtomatch);
 63     }
 64 
 65     cin>>strtomatch;
 66     unsigned strlength = strtomatch.length();
 67 
 68     BuildMatrix(&pmatrix,strlength,strlength);
 69 
 70     for(int i=0;i<strlength;++i){
 71         for(int j=i;j<strlength;++j){
 72             pmatrix[i][j] = false;
 73         }
 74     }
 75 
 76     for(unsigned i=0;i<strlength;++i){
 77         for(unsigned j=i;j<strlength;++j){
 78             string strdata;
 79             strdata.assign(strtomatch,i,j-i+1);
 80             if(myset.find(strdata)!=myset.end()){
 81                 pmatrix[i][j] = true;
 82             }
 83         }
 84     }
 85 
 86     for(int i=strlength-1;i>=0;--i){
 87         bool isallfalse = true;
 88         for(int j = i;j<strlength;++j){
 89             if(pmatrix[i][j]){
 90                 isallfalse = false;
 91                 break;
 92             }
 93         }
 94 
 95         if(isallfalse&&(i-1>=0)){
 96             for(int j = 0;j<strlength;++j){
 97                 pmatrix[j][i-1] = false;
 98             }
 99         }
100     }
101 
102     string strintov;
103     vstring.clear();
104     ListSentences(0,-1,strlength,strintov);
105 
106     for(vector<string>::iterator iter = vstring.begin();iter!=vstring.end();++iter){
107         cout<<*iter<<endl;
108     }
109 
110     ReleaseMatrix(&pmatrix,strtomatch.length());
111 }
112 
113 int main()
114 {
115     freopen("txtII.in","r",stdin);
116     freopen("txtII.out","w",stdout);
117 
118     int case_num = 0;
119     cin>>case_num;
120 
121     for(int i=0;i<case_num;++i){
122         cout<<"Case "<<i+1<<": "<<endl;
123         Solve();
124     }
125 
126     return 0;
127 }

txtII.in

3
5
cat cats and sand dog catsanddog
7
abc ab de cde abcd e abcde abcde
2
a b ab

txtII.out

Case 1: 
cat  sand  dog 
cats  and  dog 
Case 2: 
ab  cde 
abc  de 
abcd  e 
abcde 
Case 3: 
a  b 

//深度遍历矩阵,用递归解决问题

posted @ 2014-10-05 16:29  zhouyoulie  阅读(131)  评论(0编辑  收藏  举报