Trie Tree Template

Zhiyuan Xu Lv1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
namespace Trie{
int n,cnt,sz, q,val[MAXN],ch[MAXN][MAXA];
char tmp[MAXL];
inline int read(){
register int a=0,f=1;register char c;
while((c=getchar())<'0')if(c=='-')f=-1;;
while(c>='0')a=a*10+(c^48),c=getchar();
return a*f;
}
inline void trie(){
sz = 1;
memset(ch[0],0,sizeof(ch[0]));
memset(val,0,sizeof(val));
}
inline int idx(char a){return a-'a';}
inline void insert(char* s, int v){
int u = 0, len = strlen(s);
for (int i=0;i<len;i++){
int c = idx(s[i]);
if (!ch[u][c]){
memset(ch[sz],0,sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
}
inline int query(char* s){
int u = 0, len = strlen(s);
for (int i=0;i<len;i++){
int c = idx(s[i]);
if (!ch[u][c]) return -1;
u = ch[u][c];
}
if (!val[u]) {val[u] = true; return 1;}
return 0;
}
inline void solve();
inline void init();
}
  • Title: Trie Tree Template
  • Author: Zhiyuan Xu
  • Created at : 2024-07-06 00:05:00
  • Updated at : 2024-07-06 00:27:20
  • Link: https://redefine.ohevan.com/Template/Trie/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
Trie Tree Template