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
原创粉丝点击