-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrietree3.cpp
More file actions
97 lines (95 loc) · 2.13 KB
/
Copy pathtrietree3.cpp
File metadata and controls
97 lines (95 loc) · 2.13 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
const int MAX = 150001;
int path[MAX];
int endcnt[MAX];
int tree[MAX][26];
int cnt = 1;
void insert(string& word) {
int cur = 1;
for (int i = 0; i < word.length(); i++) {
int temp = word[i] - 'a';
if (tree[cur][temp] == 0)
tree[cur][temp] = ++cnt;
cur = tree[cur][temp];
path[cur]++;
}
endcnt[cur]++;
}
int countstring(string& word) {
int cur = 1;
for (int i = 0; i < word.length(); i++) {
int temp = word[i] - 'a';
if (tree[cur][temp] == 0)
{
return 0;
}
cur = tree[cur][temp];
}
return endcnt[cur];
}
int countstringwith(string& word) {
int cur = 1;
for (int i = 0; i < word.length(); i++) {
int temp = word[i] - 'a';
if (tree[cur][temp] == 0)
{
return 0;
}
cur = tree[cur][temp];
}
return path[cur];
}
void deletestring(string& word) {
if (countstring(word) > 0) {
int cur = 1;
path[cur]--;
for (int i = 0; i < word.length(); i++) {
int temp = word[i] - 'a';
if (--path[tree[cur][temp]] == 0)
{
tree[cur][temp] = 0;
return;
}
cur = tree[cur][temp];
}
endcnt[cur]--;
}
}
void clear() {
for (int i = 1; i <= cnt; i++) {
memset(tree[i],0,sizeof(tree[i])); //memset只能作用与数组 not vector
path[i] = 0;
endcnt[i] = 0;
}
}
signed main()
{
int m;
cin >> m;
int mod;
while(cin >> mod)
{
string s;
cin >> s;
if(mod == 1)
insert(s);
else if(mod == 2)
deletestring(s);
else if(mod == 3)
{
if(countstring(s) == 0)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
else
{
cout << countstringwith(s) << endl;
}
}
clear();
}