-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtrie.cpp
67 lines (63 loc) · 1.39 KB
/
trie.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# include <bits/stdc++.h>
# define NR 25
# define sigma 26
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
struct trie {
int nrfii, cnt;
trie *fiu[sigma];
trie () {
nrfii=0; cnt=0;
memset (fiu, 0, sizeof(fiu));
}
};
trie *T=new trie;
int i,j,n,m;
char s[NR];
void ins (trie *nod, char *s) {
if (*s=='\0') {
nod->cnt ++;
return;
}
if (nod->fiu[*s - 'a']==NULL) {
nod->fiu[*s - 'a']=new trie;
nod->nrfii ++;
}
ins(nod->fiu[*s - 'a'], s+1);
}
int del (trie *nod, char *s) {
if (*s=='\0') nod->cnt --;
else {
if (del (nod->fiu[*s - 'a'], s+1)) {
nod->nrfii --;
nod->fiu[*s - 'a']=NULL;
}
}
if (nod->cnt==0 && nod->nrfii==0 && nod!=T) {
delete nod;
return 1;
}
return 0;
}
int query (trie *nod, char *s) {
if (*s=='\0') return nod->cnt;
if (nod->fiu[*s - 'a']!=NULL) return query(nod->fiu[*s - 'a'], s+1);
return 0;
}
int prefix (trie *nod, char *s, int nivel) {
if (*s=='\0' || nod->fiu[*s-'a']==NULL) {
return nivel;
}
else return prefix (nod->fiu[*s-'a'], s+1, nivel+1);
}
int main ()
{
while (f.getline(s, NR)) {
if (s[0]=='0') ins(T, s+2);
else if (s[0]=='1') del(T, s+2);
else if (s[0]=='2') g<<query(T, s+2)<<"\n";
else g<<prefix(T, s+2, 0)<<"\n";
}
return 0;
}