hihocoder题目():#1014 trie树
1 #include2 using namespace std; 3 class trieTree 4 { 5 public: 6 trieTree() 7 { 8 isword=false; 9 for (int i=0;i<26;i++) 10 { 11 next[i]=NULL; 12 } 13 } 14 ~trieTree() 15 { 16 for (int i=0;i<26;i++) 17 { 18 if (next[i]!=NULL) 19 { 20 destory(next[i]); 21 } 22 } 23 } 24 void destory(trieTree* index); 25 int insertWord(char*); 26 int searchWord(char*); 27 int findWord(trieTree* index); 28 private: 29 trieTree* next[26]; 30 bool isword; 31 }; 32 int main() 33 { 34 int dictionarySize; 35 trieTree root; 36 char word[10]; 37 cin>>dictionarySize; 38 int i=0; 39 for(i=0;i >word; 42 root.insertWord(word); 43 } 44 cin>>dictionarySize; 45 for(i=0;i >word; 48 cout< < next[*word-'a']) 59 { 60 index->next[*word-'a']=new trieTree; 61 } 62 if (!index->next[*word-'a']) 63 { 64 return -1; 65 } 66 else 67 { 68 index=index->next[*word-'a']; 69 } 70 word++; 71 } 72 index->isword=true; 73 return 0; 74 } 75 int trieTree::searchWord(char* word) 76 { 77 trieTree* index=this; 78 int count=0; 79 while(*word) 80 { 81 if(index->next[*word-'a']==NULL) 82 { 83 return 0; 84 } 85 index=index->next[*word-'a']; 86 word++; 87 } 88 count=findWord(index); 89 return count; 90 } 91 int trieTree::findWord(trieTree* index) 92 { 93 int count=0; 94 if(index->isword) 95 count++; 96 for(int i=0;i<26;i++) 97 { 98 if(index->next[i]!=NULL) 99 {100 count+=findWord(index->next[i]);101 }102 }103 return count;104 }105 void trieTree::destory(trieTree* index)106 {107 for (int i=0;i<26;i++)108 {109 if (index->next[i]!=NULL)110 {111 destory(index->next[i]);112 index->next[i]=NULL;113 }114 }115 delete index;116 }
通过编译之后,发现提交上去还是错了,Wrong Answer!