设计一个支持如下两种操做的数据结构:python
void addWord(word) bool search(word)
search(word) 能够搜索文字或正则表达式字符串,字符串只包含字母 .
或 a-z
。 .
能够表示任何一个字母。ios
示例:c++
addWord("bad") addWord("dad") addWord("mad") search("pad") -> false search("bad") -> true search(".ad") -> true search("b..") -> true
说明:正则表达式
你能够假设全部单词都是由小写字母 a-z
组成的。数据结构
解题思路
直接用字典树(trie)便可,至于.
匹配符直接利用回溯便可。spa
#include<bits/stdc++.h> using namespace std; const int nch = 26; const int maxn = 200010; static auto x = []() { std::ios::sync_with_stdio(false); std::cin.tie(NULL); return 0; } (); struct Node { int ch[nch]; bool flag; void reset() { for(int i = 0; i < nch; ++i) { ch[i] = -1; } flag = false; } }; Node tree[maxn]; class WordDictionary { public: /** Initialize your data structure here. */ int tot; WordDictionary() { tree[tot = 0].reset(); } /** Adds a word into the data structure. */ void addWord(string word) { int root = 0; for(auto &chr:word) { if(tree[root].ch[chr - 'a'] == -1) { tree[root].ch[chr - 'a'] = ++tot; tree[tot].reset(); } root = tree[root].ch[chr - 'a']; } tree[root].flag = true; } /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */ bool search(string word) { return _search(word, 0, 0); } bool _search(string &word, int cur, int root) { if(cur == word.size() && tree[root].flag) return true; if(cur >= word.size() or root == -1) return false; if(word[cur] == '.') { for(int i = 0; i < nch; ++i) { if(tree[root].ch[i] != -1 && _search(word, cur + 1, tree[root].ch[i])) return true; } return false; } if(tree[root].ch[word[cur]-'a'] != -1) return _search(word, cur + 1, tree[root].ch[word[cur]-'a']); return false; } }; int main() { return 0; }