解法一:pbds
pbds实现字典树非常方便。但是速度比自己实现的要慢一些。接口基本也是一致的。
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/trie_policy.hpp>
using namespace __gnu_pbds;
class Trie {
private:
trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update> t;
public:
/** Initialize your data structure here. */
Trie() {
}
/** Inserts a word into the trie. */
void insert(string word) {
t.insert(word);
}
/** Returns if the word is in the trie. */
bool search(string word) {
auto x = t.prefix_range(word);
return *x.first