TrieNode *searchPattern(string word) { typedef std::pair<TrieNode *, int> Match; stack<Match>s; s.push(make_pair(root, 0)); int len = word.length(); while (!s.empty()) { Match m = s.top(); TrieNode *node = m.first; s.pop(); int id = m.second; if (id >= len) { if (node->isEnd() || s.empty()) { return node; } continue; } char ch = word[m.second]; if (ch == '.') { for (int i = 0; i < TrieNode::R; i++) { if (node->links[i]) { s.push(make_pair(node->links[i], id + 1)); } } } else if (node->containsKey(ch)) { s.push(make_pair(node->get(ch), id + 1)); } } return NULL; }
// Returns if the word is in the trie. bool search(string word) { //TrieNode *node = searchPrefix(word); TrieNode *node = searchPattern(word); return node != NULL && node->isEnd(); }