-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_trie_logn.cpp
90 lines (77 loc) · 1.85 KB
/
AC_trie_logn.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_trie_logn.cpp
* Create Date: 2015-08-24 18:44:56
* Descripton:
*/
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 26;
class TrieNode {
public:
TrieNode* p[SIZE];
int val;
// Initialize your data structure here.
TrieNode() {
val = 0;
memset(p, 0, sizeof(p));
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string word) {
TrieNode *cur = root;
for (auto ch : word) {
int i = num(ch);
if (cur->p[i] == NULL) {
cur->p[i] = new TrieNode();
}
cur = cur->p[i];
}
cur->val++;
}
// Returns if the word is in the trie.
bool search(string word) {
TrieNode *cur = root;
for (auto ch : word) {
int i = num(ch);
if (cur->p[i] == NULL) {
return false;
}
cur = cur->p[i];
}
return cur->val > 0;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode *cur = root;
for (auto ch : prefix) {
int i = num(ch);
if (cur->p[i] == NULL) {
return false;
}
cur = cur->p[i];
}
return true;
}
private:
TrieNode* root;
// the method to calculate the number of a char
int num(char x) { return x - 'a'; }
};
int main() {
Trie trie;
trie.insert("abc");
trie.insert("bc");
cout << trie.search("b") << endl;
cout << trie.search("bc") << endl;
cout << trie.search("a") << endl;
cout << trie.search("abc") << endl;
cout << trie.startsWith("ab") << endl;
return 0;
}