-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
77 lines (71 loc) · 1.29 KB
/
trie.cpp
File metadata and controls
77 lines (71 loc) · 1.29 KB
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
68
69
70
71
72
73
74
75
76
77
/// https://infoarena.ro/problema/trie
#include<bits/stdc++.h>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
char s[22];
int k;
struct trie
{
int sum,fv;
int v[26];
};
vector<trie>a;
void add(int x,int i)
{
a[x].sum++;
if(s[i+1]=='\0')
a[x].fv++;
else
{
if(a[x].v[s[i+1]-'a']==0)
a[x].v[s[i+1]-'a']=++k,a.push_back({0,0,0});
add(a[x].v[s[i+1]-'a'],i+1);
}
}
void del(int x,int i)
{
a[x].sum--;
if(s[i+1]=='\0')
a[x].fv--;
else
del(a[x].v[s[i+1]-'a'],i+1);
}
int ap(int x,int i)
{
if(s[i+1]=='\0')
return a[x].fv;
if(a[x].v[s[i+1]-'a']==0)
return 0;
return ap(a[x].v[s[i+1]-'a'],i+1);
}
int pref(int x,int i)
{
if(a[x].sum==0)
return max(i-1,0);
if(s[i+1]=='\0')
return i;
if(a[x].v[s[i+1]-'a']==0)
return i;
return pref(a[x].v[s[i+1]-'a'],i+1);
}
int main()
{
int p;
a.push_back({0,0,0});
while(f>>p)
{
f>>(s+1);
if(p==0)
add(0,0);
else
if(p==1)
del(0,0);
else
if(p==2)
g<<ap(0,0)<<'\n';
else
g<<pref(0,0)<<'\n';
}
return 0;
}