Kyopro Library
読み取り中…
検索中…
一致する文字列を見つけられません
trie.hpp
[詳解]
1
struct
Trie
{
2
struct
Node
{
3
array
<
int
,26>
to
;
4
int
count_of_end
=0,
count_of_node
=0;
5
Node
() { fill(ALL(to),-1); }
6
};
7
8
Trie
(
int
len) {
9
nodes.reserve(len);
10
Node
root;
11
nodes.push_back(root);
12
}
13
14
void
insert
(
const
string& s) {
15
int
cur=0;
16
for
(
const
char
& c:s) {
17
int
nxt=nodes[cur].to[c-
'a'
];
18
if
(nxt==-1) {
19
nodes[cur].to[c-
'a'
]=nodes.size();
20
nxt=nodes.size();
21
nodes.push_back(Node());
22
}
23
cur=nxt;
24
nodes[cur].count_of_node++;
25
}
26
nodes[cur].count_of_end++;
27
}
28
29
int
count
(
const
string& s) {
30
int
cur=0,ret=0;
31
for
(
const
char
& c:s) {
32
int
nxt=nodes[cur].to[c-
'a'
];
33
if
(nxt==-1)
return
0;
34
cur=nxt;
35
ret=nodes[cur].count_of_end;
36
}
37
}
38
39
void
erase
(
const
string& s) {
40
int
cur=0;
41
for
(
const
char
& c:s) {
42
int
nxt=nodes[cur].to[c-
'a'
];
43
if
(nxt==-1)
return
;
44
cur=nxt;
45
nodes[cur].count_of_node--;
46
}
47
nodes[cur].count_of_end--;
48
}
49
50
private
:
51
vector<Node> nodes;
52
};
Trie::Node
Definition
trie.hpp:2
Trie::Node::to
array< int, 26 > to
Definition
trie.hpp:3
Trie::Node::count_of_end
int count_of_end
Definition
trie.hpp:4
Trie::Node::Node
Node()
Definition
trie.hpp:5
Trie::Node::count_of_node
int count_of_node
Definition
trie.hpp:4
Trie
Definition
trie.hpp:1
Trie::Trie
Trie(int len)
Definition
trie.hpp:8
Trie::count
int count(const string &s)
Definition
trie.hpp:29
Trie::erase
void erase(const string &s)
Definition
trie.hpp:39
Trie::insert
void insert(const string &s)
Definition
trie.hpp:14
string
trie.hpp
構築:
1.13.2