設計一個支持如下兩種操做的數據結構: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; }