classTrie{public:Trie(){
root =newTrieNode();}classTrieNode// 内部类{public:bool isWord =false;
TrieNode *children[26];// 数组TrieNode(){for(int i =0; i <26; i++){
children[i]=nullptr;}}~TrieNode(){for(int i =0; i <26; i++){delete children[i];
children[i]=nullptr;}}};/** Inserts a word into the trie. */voidinsert(string word){
TrieNode *node = root;for(int i =0; i < word.length(); i++){int index = word[i]-'a';if(node->children[index]==nullptr){
node->children[index]=newTrieNode();}
node = node->children[index];}
node->isWord =true;}/** Returns if the word is in the trie. */boolsearch(string word){
TrieNode *node = root;for(int i =0; i < word.length(); i++){int index = word[i]-'a';if(node->children[index]==nullptr){returnfalse;}
node = node->children[index];}return node->isWord;}/** Returns if there is any word in the trie that starts with the given prefix. */boolstartsWith(string prefix){
TrieNode *node = root;for(int i =0; i < prefix.length(); i++){int index = prefix[i]-'a';if(node->children[index]==nullptr){returnfalse;}
node = node->children[index];}returntrue;}private:
TrieNode *root;};