代码拉取完成,页面将自动刷新
#include <unordered_map>
using namespace std;
struct TrieNode {
bool is_word_;
unordered_map<char, TrieNode*> children;
TrieNode(): is_word_(false) {}
};
class WordDictionary {
public:
TrieNode* root;
WordDictionary() {
root = new TrieNode();
}
// Adds a word into the data structure.
void addWord(string word) {
if (word.size() <= 0) return;
TrieNode* node = root;
for (int i = 0; i < word.size(); ++i){
if (node->children.find(word[i]) == node->children.end()){
node->children[word[i]] = new TrieNode();
}
node = node->children[word[i]];
}
node->is_word_ = 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 retrieve(word, root, 0, word.size());
}
bool retrieve(string& word, TrieNode* node, int idx, int length) {
if (idx == length) return node->is_word_;
if (word[idx] == '.') {
if (node->children.size() == 0) {
return false;
}
for (auto& kv: node->children) {
if (retrieve(word, kv.second, idx+1, length)) {
return true;
}
}
return false;
}else if (node->children.find(word[idx]) != node->children.end()) {
return retrieve(word, node->children[word[idx]], idx+1, length);
}
return false;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。