-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringTrie.cpp
59 lines (58 loc) · 1.98 KB
/
StringTrie.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
template <typename T> // 字符串trie,支持插入字符串,查询字符串个数,打印trie树,查询子树
class trie {
map<T, trie> tries;
map<T, int> cnt;
public:
template<typename It>
void insert(It it, It end_it) {
if(it == end_it) return;
if(next(it) == end_it) cnt[*it] ++;
tries[*it].insert(next(it), end_it);
}
template<typename C>
void insert(const C& container) { // 插入字符串tr.insert(begin(s), end(s));
insert(begin(container), end(container));
}
void insert(const initializer_list<T>& il) { // tr.insert({'a', 'b', 'c'});
insert(begin(il), end(il));
}
void print(vector<T> &v) const {
if(tries.empty()) {
copy(begin(v), end(v), ostream_iterator<T>(cout, " "));
cout << "\n";
}
for(auto [key, val] : tries) {
v.push_back(key);
val.print(v);
v.pop_back();
}
}
void print() const {
vector<T> v;
print(v);
}
template<typename It>
optional<reference_wrapper<const trie>> subtrie(It it, It end_it) const { // 查询trie树是否存在以某个字符串为前缀的字符串
if(it == end_it) return ref(*this);
auto found (tries.find(*it));
if(found == end(tries)) return nullopt;
return found->second.subtrie(next(it), end_it);
}
template<typename C>
auto subtrie(const C &c) {
return subtrie(begin(c), end(c));
}
int get_count(const T& key) const {
auto found (cnt.find(key));
if(found == end(cnt)) return 0;
return found->second;
}
template<typename It>
int get_count(It it, It end_it) const { // 查询某个字符串个数 tr.get_count(begin(s), end(s));
if(it == end_it) return 0;
if(next(it) == end_it) return get_count(*it);
auto found (tries.find(*it));
if(found == end(tries)) return 0;
return found->second.get_count(next(it), end_it);
}
};