Detailed trie Trie

This morning the provincial election strings ...... only KMP, even hash I will not be miserable Gangster abuse ...... So I worked hard to learn a string, the string learning course this lovely Trie tree data structure from the start it! ! !

First, what is the Trie? ? ?

Trie, by definition, is used to save some string.

Two, Trie tree advantages / purposes? ? ?

Then someone will ask: save some string, why not just use an array? ? ? The answer is obvious, you will reap MLE good results, so we consider space-saving to store their data structures, so the Trie came into being! ! !

Third, the core idea? ? ? preservation method? ? ?

Consider storing two words ABA and ABC, we can use them to share a common prefix "AB" (Trie therefore also known as "prefix tree"), so we can build a Trie tree shaped like this:

      A

      |

   B

   |

   ————

 |    |

  A        C

We found that we used the space of four characters, it is saved six characters, plus dynamic open points, so that the spatial complexity of Trie's very good.

Four, Trie common operations

Insert / query Trie is the most basic operations:

Insert code as follows (detailed notes, query Similarly)

. 1  void INSERT ( char * S)
 2  {
 . 3      int U = 0 , len = strlen (S + . 1 );
 . 4      // len is a string length, u point to the current node, starting with the root node, point 0 
. 5      for ( int I = . 1 ; I <= len; I ++ )
 . 6      {
 . 7          int C = IDX (s [i]); // query s [i] corresponding to the number 
. 8          IF (CH [U] [C]) CH [U! ] [C] = CNT ++; // If there is no point to open a new node 
. 9          U = CH [U] [C]; // along down a 
10      }
 11 }
Trie insert

Five examples:

Luo Gu P2580

Resolution:

Trie tree bare title, all the students' names into the Trie tree. For inquiries, if a person's name in some point is empty, then it is wrong, if it is the first query, then it is right, otherwise it is repeated.

Reference code is as follows:

 1 // luogu-judger-enable-o2
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #define N 500050
 6 using namespace std;
 7 struct Trie
 8 {
 9     int val[N],ch[N][26],cnt;
10     int idx(char c){return c-'a';}
11     void insert(char *s)
12     {
13         int u=0,len=strlen(s+1);
14         for(int i=1;i<=len;i++)
15         {
16             int c=idx(s[i]);
17             if(!ch[u][c])ch[u][c]=++cnt;
18             u=ch[u][c];
19         }
20     }
21     int search(char *s)
22     {
23         int u=0,len=strlen(s+1);
24         for(int i=1;i<=len;i++)
25         {
26             int c=idx(s[i]);
27             if(!ch[u][c])return 0;
28             u=ch[u][c];
29         }
30         if(!val[u]){val[u]=1;return 1;}
31         return 2;
32     }
33 }trie;
34 int n,m;
35 char s[25000];
36 int main()
37 {
38     ios::sync_with_stdio(0);
39     cin>>n;
40     for(int i=1;i<=n;i++)
41     {
42         cin>>(s+1);
43         trie.insert(s);
44     }
45     cin>>m;
46     for(int i=1;i<=m;i++)
47     {
48         cin>>(s+1); 
49         int res=trie.search(s);
50         if(res==0)cout<<"WRONG"<<endl;
51         else if(res==1)cout<<"OK"<<endl;
52         else cout<<"REPEAT"<<endl;
53     }
54     return 0;
55 }
Luo Gu P2580

 

Guess you like

Origin www.cnblogs.com/szmssf/p/11489090.html