zoukankan      html  css  js  c++  java
  • trie tree(字典树)

    hihocoder题目(http://hihocoder.com/problemset):#1014  trie树

      1 #include <iostream>
      2 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<dictionarySize;i++)
     40     {
     41         cin>>word;
     42         root.insertWord(word);
     43     }
     44     cin>>dictionarySize;
     45     for(i=0;i<dictionarySize;i++)
     46     {
     47         cin>>word;
     48         cout<<root.searchWord(word)<<endl;
     49     }
     50     
     51     return 0;
     52 }
     53 int trieTree::insertWord(char* word)
     54 {
     55     trieTree* index=this;
     56     while(*word)
     57     {
     58         if(!index->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!

  • 相关阅读:
    mysql:基础管理、体系结构、升级降级
    Linux网络基础
    mysql 忘记本地密码
    seq命令的用法
    mysql03-SQL应用
    SolidWorks 如何改变封闭草图的背景颜色
    新版 AD 无法选中某些部件(如 Via,Pad)的问题
    安装 dot net 时出现严重错误 0x80070643 安装时发生严重错误 1603 ndp48
    IO 口扩展
    自动波特率检测
  • 原文地址:https://www.cnblogs.com/howardhuo/p/4683349.html
Copyright © 2011-2022 走看看