-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path208(字典树).cpp
96 lines (86 loc) · 2.14 KB
/
208(字典树).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
90
91
92
93
94
95
96
#include<iostream>
#include<sstream>
#include<vector>
#include<set>
#include<string>
#include<string.h>
#include<unordered_set>
#include<unordered_map>
#include<stack>
#include<algorithm>
#include<stdarg.h>
#include<queue>
#include <stdint.h>
using namespace std;
// 实现字典树
// 字典树中的结点,数据成员包括本身的值,是否是单词的结尾标志,被多少单词共享(用于删除),以及其后继结点数组
class TrieNode {
public:
char ch;
bool isend;
int shared;
unordered_map<char, TrieNode*> children;
// Initialize your data structure here.
TrieNode():ch(' '), isend(false), shared(0) {}
TrieNode(char ch): ch(ch), isend(false), shared(0) {}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
~Trie() {
delete root;
}
// Inserts a word into the trie.
void insert(string word) {
if(search(word))
// 如果找到该单词,则不用插入
return;
TrieNode* cur = root;
for(auto c : word) {
if(cur->children.find(c) == cur->children.end()) {
TrieNode* newNode = new TrieNode(c);
cur->children[c] = newNode;
}
cur = cur->children[c];
++cur->shared;
}
cur->isend = true;
}
// Returns if the word is in the trie.
bool search(string word) {
TrieNode* cur = root;
for(auto c : word) {
if(cur->children.find(c) == cur->children.end())
return false;
cur = cur->children[c];
}
return cur->isend == true;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode* cur = root;
for(auto c : prefix) {
if(cur->children.find(c) == cur->children.end())
return false;
cur = cur->children[c];
}
return true;
}
private:
TrieNode* root;
};
// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
int main() {
Trie trie;
trie.insert("somestring");
cout << trie.search("somestring");
cout << trie.startsWith("some");
system("pause");
return 0;
}