[Template] trie

. 1 #include <the iostream>
 2 #include <cstdio>
 . 3 #include <CString>
 . 4  the using  namespace STD;
 . 5  struct Node {
 . 6      int CNT;   // number of record appears 
. 7      int NEX [ 30 ]; // this node a son node 
. 8 } Trie [ 400500 ];
 . 9  char S1 [ 105 ], S2 [ 105 ];
 10  int TOT = . 1 ; // may be 0 
. 11  void Build ( char * s){
12     int len=strlen(s);
13     int root=0;
14     for(int i=0;i<len;++i){
15         int id=s[i]-'a';
16         if(trie[root].nex[id]==0) trie[root].nex[id]=++tot;
17         root=trie[root].nex[id];
18         ++trie[root].cnt;
19     }
20 }
21 int query(char* s){
22     int len=strlen(s);
23     int root=0;
24     for(int i=0;i<len;++i){
25         int id=s[i]-'a';
26         if(!trie[root].nex[id]) return 0;
27         root=trie[root].nex[id];
28     }
29     return trie[root].cnt;
30 }
31 int main(){
32     while(gets(s1)){
33         intlen = strlen (S1);
 34 is          IF (len!) BREAK ;
 35          Build (S1);
 36      }
 37 [      the while (the gets (S2)) {
 38 is          int U = Query (S2);
 39          the printf ( " % D \ n- " , U);
 40      }
 41 is      return  0 ;
 42 is  }
 43 is  // hdu1251
 44 is  // https://vjudge.net/contest/281068 # problem / C
 45  // do not use scanf gets because the read character strings, gets Enter will give up, but scanf does not.
46  //https://www.cnblogs.com/hlongch/p/5742477.html

 

Guess you like

Origin www.cnblogs.com/xiaobuxie/p/11391852.html