-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
35 lines (32 loc) · 700 Bytes
/
trie.cpp
File metadata and controls
35 lines (32 loc) · 700 Bytes
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
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e6 + 5, A = 27;
const int MOD = 1e9 + 7;
#define ll long long
struct Node{
int terminal = 0;
int next[A];
Node(){
memset(next, -1, sizeof next);
}
};
vector<Node> trie(1);
void add(string s){
int v = 0;
for(char ch : s){
if(trie[v].next[ch - 'a'] == -1){
trie[v].next[ch - 'a'] = trie.size();
trie.emplace_back();
}
v = trie[v].next[ch - 'a'];
}
trie[v].terminal++;
}
ll Count(string s){
int v = 0;
for(char ch:s){
if(trie[v].next[ch - 'a'] == -1)return 0;
v = trie[v].next[ch-'a'];
}
return trie[v].terminal;
}