字典树模板

Posted 2020pengxiyue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字典树模板相关的知识,希望对你有一定的参考价值。

 1 typedef struct letter{
 2     int finished = 0;
 3     char le;
 4     letter *son, *ne;
 5     letter():le(),son(NULL),ne(NULL){};
 6 }letter,*pointer;
 7  
 8 struct Trie
 9     {
10         pointer rt;
11         Trie() {rt = new letter();}
12         void insert(char *word, int len)
13             {
14                 pointer u = rt;
15                 for(int i = 0; i < len; i ++)
16                     {
17                         pointer v = u->son;
18                         if(v != NULL)   while(v->ne != NULL && v->le != word[i]) v = v->ne;
19                         if(v == NULL || v->ne == NULL && v->le != word[i])
20                             {
21                                 pointer &p = (v == NULL ? u->son :v->ne);
22                                 p = new letter();
23                                 p->le = word[i];
24                                 if(i == len - 1)    ++ p->finished;
25                                 u = p;
26                             }
27                         else u = v;
28                     }
29             }
30          
31         bool  find(char *word, int len)
32             {
33                 pointer u = rt;
34                 for(int i = 0; i < len; i ++)
35                     {
36                         pointer v = u->son;
37                         if(v == NULL)   return false;
38                         while(v->ne != NULL && v->le != word[i])  v = v->ne;
39                         if(v->ne == NULL && v->le != word[i]) return false;
40                         u = v;
41                     }
42                 return true;
43             }
44     };

 

以上是关于字典树模板的主要内容,如果未能解决你的问题,请参考以下文章

字典树数组模板

cf842D 01字典树|线段树 模板见hdu4825

Shortest Prefixes 字典树模板

字典树模板+HDU 1671 ( Phone List )(字典树)

字典树(模板)

字典树模板