File tree Expand file tree Collapse file tree 1 file changed +8
-12
lines changed Expand file tree Collapse file tree 1 file changed +8
-12
lines changed Original file line number Diff line number Diff line change 11#pragma once
22// ! https://cp-algorithms.com/string/aho_corasick.html#construction-of-the-trie
3- const int mn = ' A' ,
4- cnt_let = 26 ; // mn <= s[i] < mn + cnt_let
3+ const int mn = ' A' ;
54struct trie {
65 struct node {
7- array<int , cnt_let> next;
8- int cnt_words = 0 , par = -1 ;
9- char ch;
10- node (int par = -1 , char ch = ' #' ): par(par), ch(ch) {
11- fill (all (next), -1 );
12- }
6+ array<int , 26 > next;
7+ bool end_of_word = 0 ;
8+ node () { fill (all (next), -1 ); }
139 };
14- vector <node> t;
10+ deque <node> t;
1511 trie (): t(1 ) {}
1612 void insert (const string& s) {
1713 int v = 0 ;
1814 for (char ch : s) {
1915 int u = ch - mn;
2016 if (t[v].next [u] == -1 ) {
2117 t[v].next [u] = sz (t);
22- t.emplace_back (v, ch );
18+ t.emplace_back ();
2319 }
2420 v = t[v].next [u];
2521 }
26- t[v].cnt_words ++ ;
22+ t[v].end_of_word = 1 ;
2723 }
2824 int find (const string& s) {
2925 int v = 0 ;
@@ -32,6 +28,6 @@ struct trie {
3228 if (t[v].next [u] == -1 ) return 0 ;
3329 v = t[v].next [u];
3430 }
35- return t[v].cnt_words ;
31+ return t[v].end_of_word ;
3632 }
3733};
You can’t perform that action at this time.
0 commit comments