Leetcode-Trie
来源:互联网 发布:linux如何停掉mysql 编辑:程序博客网 时间:2024/05/14 08:49
Add and Search Word - Data structure design
Design a data structure that supports the following two operations:
void addWord(word)
bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.
For example:
addWord(“bad”)
addWord(“dad”)
addWord(“mad”)
search(“pad”) -> false
search(“bad”) -> true
search(“.ad”) -> true
search(“b..”) -> true
#include <string>#include <iomanip>#include <utility> #include <memory>#include <iostream>#include <vector>#include <unordered_set>#include <unordered_map>#include <algorithm>using namespace std;template<typename T>class nodeHash {public: nodeHash():len(0){} nodeHash(const T&t): val(t) {} T val; vector<shared_ptr<nodeHash<T>>> children; unordered_set<char> endNode; unordered_map<T, shared_ptr<nodeHash<T>>> mmps; size_t len;};class WordDictionary{public: WordDictionary():root(nullptr){} shared_ptr<nodeHash<char>> root; void addWord(const string& word) { if (!root) root = make_shared<nodeHash<char>>('\0'); auto it = root; for (size_t i = 0; i < word.size(); ++i) { if (i+1 == word.size()) { it->endNode.insert(word[i]); } auto resf = it->mmps.find(word[i]); if (resf != it->mmps.end()) { it = resf->second; } else { shared_ptr<nodeHash<char>> tmpptr = make_shared<nodeHash<char>>(nodeHash<char>(word[i])); it->children.push_back(tmpptr); pair<char, decltype(tmpptr)> tmppair; tmppair.first = word[i]; tmppair.second = tmpptr; it->mmps.insert(tmppair); it = tmpptr; } } return; } bool search(const string &word,size_t index ,decltype(root) it) { /*dsf*/ if (index+1 == word.size()) { if (!it)return false; char c = word.back(); if (c != '.') { auto resf = it->endNode.find(word.back()); if (resf != it->endNode.end()) return true; else return false; } else { if (!(it->endNode).empty())return true; else return false; } } else if ( ( (it->mmps).empty() && index < word.size()) || ( (!(it->mmps).empty()) && index >= word.size()) ) { return false; } if (index >= word.size()) return true; if (!it) return false; char c = word[index]; if (c != '.') { auto resf = it->mmps.find(c); if (resf == it->mmps.end()) { return false; } else { it = resf->second; return search(word, index+1, it); } } else { for (auto &c1 : it->mmps) { if (search(word, index+1, c1.second)) return true; } return false; } } bool search(string word) { if ((!root) || (root->mmps).empty()) return false; auto it = root; return search(word, 0, it); }};/** * Your WordDictionary object will be instantiated and called as such: * WordDictionary obj = new WordDictionary(); * obj.addWord(word); * bool param_2 = obj.search(word); */
0 0
- Leetcode-Trie
- leetcode ----Trie/stack专题
- leetcode 212 【trie+dfs】
- leetcode 208 【trie】
- leetcode 211 【Trie】
- Leetcode-DSF-Trie-Backtrack
- LeetCode基础-字符串-Trie
- [leetcode][trie] Implement Trie (Prefix Tree)
- leetcode:Trie:Implement Trie (Prefix Tree)(208)
- leetcode---Implement Trie (Prefix Tree)---Trie树
- [leetcode] Implement Trie (Prefix Tree)
- LeetCode Implement Trie (Prefix Tree)
- [leetcode]Implement Trie (Prefix Tree)
- [LeetCode] Implement Trie (Prefix Tree)
- leetcode:Implement Trie (Prefix Tree)
- LeetCode Implement Trie (Prefix Tree)
- leetcode Implement Trie (Prefix Tree)
- LeetCode Implement Trie (Prefix Tree)
- 多线程抓取数据
- [LeetCode]100. Same Tree(相同树)
- Linux--RH134---unit 1 自动安装配置虚拟机
- JAVA生产缩略图
- POJ2385 Apple Catching(DP/递推)
- Leetcode-Trie
- Spring注入失败的原因
- linux运维-dhcp服务
- 全页式网站 练习代码暂存
- [Linux基础]Linux基础知识入门及常见命令.
- echarts动态加载数据
- Jmeter分析请求的响应结果
- Console命令详解,让调试js代码变得更简单
- Caffe源码解读(八):使用训练好的模型