zoukankan      html  css  js  c++  java
  • LeetCode Add and Search Word

    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
    

    Note:
    You may assume that all words are consist of lowercase letters a-z.

    不顺,每次都不顺

    class CharNode {
    public:
        int count;
        CharNode* child[26];
        CharNode(int cnt = 0) : count(cnt) {
            for (int i = 0; i<26; i++) {
                child[i] = NULL;
            }
        }
    };
    
    class WordDictionary {
    private:
        CharNode root;
        void add(const string& str) {
            CharNode* current = &root;
            int pos = 0;
            int len = str.size();
            char ch;
            while (pos < len) {
                ch = str[pos] - 'a';
                if (current->child[ch] == NULL) {
                    current->child[ch] = new CharNode();
                }
                if (pos == len - 1) {
                    current->child[ch]->count++;
                }
                current = current->child[ch];
                pos++;
            }
        }
        
        bool dfs_search(CharNode* at, string& word, int pos) {
            if (at == NULL) {
                return false;
            }
            int len = word.size();
            if (len == pos && at != NULL && at->count > 0) {
                return true;
            } else if (pos >= len) {
                return false;
            }
            if (word[pos] == '.') {
                // wildcard
                for (int i=0; i<26; i++) {
                    if (dfs_search(at->child[i], word, pos + 1)) {
                        return true;
                    }
                }
            } else {
                return dfs_search(at->child[word[pos] - 'a'], word, pos + 1);
            }
            return false;
        }
    public:
        WordDictionary() {
            // placeholder for '' string
            root.count = 1;
        }
        // Adds a word into the data structure.
        void addWord(string word) {
            add(word);
        }
    
        // 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 dfs_search(&root, word, 0);
        }
    };
    
    // Your WordDictionary object will be instantiated and called as such:
    // WordDictionary wordDictionary;
    // wordDictionary.addWord("word");
    // wordDictionary.search("pattern");
  • 相关阅读:
    PC逆向之代码还原技术,第三讲汇编中加法的代码还原
    PC逆向之代码还原技术,第二讲寻找程序入口点
    C++STL模板库适配器之优先级队列
    C++STL模板库适配器之queue队列
    C++STL模板库适配器之stack容器
    iBinary C++STL模板库关联容器之map/multimap
    C++STL模板库关联容器之set/multiset
    C++STL模板库序列容器之deque
    C++STL模板库序列容器之List容器
    C++STL模板库序列容器之vector
  • 原文地址:https://www.cnblogs.com/lailailai/p/4532241.html
Copyright © 2011-2022 走看看