classTrieNode{public:char ch;TrieNode *next[26];bool isEnd;TrieNode(char c ='/'):ch(c),isEnd(false){memset(next,0,sizeof(TrieNode*)*26);}};classTrie{public:TrieNode *root;Trie(){root =newTrieNode();}~Trie(){destroy(root);}voiddestroy(TrieNode *root){if(root ==NULL)return;for(int i =0; i <26; i++)destroy(root->next[i]);delete root;}voidinsert(string str){TrieNode *cur = root;for(char s:str){if(cur->next[s-'a']==NULL)cur->next[s-'a']=newTrieNode(s);cur = cur->next[s-'a'];}cur->isEnd =true;}};classWordDictionary{Trie tree;public:/** Initialize your data structure here. */WordDictionary(){}/** Adds a word into the data structure. */voidaddWord(string word){tree.insert(word);}/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */boolsearch(string word){TrieNode *cur = tree.root;bool found =false;for(int i =0; i <26;++i){find(word,cur->next[i],0,found);}return found;}voidfind(string &word, TrieNode *root,int idx,bool&found){if(found ||!root)return;if(idx == word.size()-1){if(root->isEnd)if(word[idx]=='.'|| word[idx]== root->ch)found =true;return;}if((word[idx]!='.'&&root->ch == word[idx])|| word[idx]=='.'){for(int i =0; i <26;++i){find(word,root->next[i],idx+1,found);}}}};