码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Word Break

时间:2014-10-05 01:54:47      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   div   

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

txt.in

4
1
a a
2
what is whats
3
hello nice to helloto
5
Kobe Carter LeBron Jordan Iverson Yao

txt.out

Case 1: true
Case 2: false
Case 3: true
Case 4: false

[LeetCode] Word Break

标签:style   blog   color   io   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/zhouyoulie/p/4006506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!