• 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");
  • 相关阅读:
    [转]AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
    C#文档注释
    VS 单元测试项目,测试的时候运行按钮不可用
    获取泛型类的Type
    Sql 复习(4)
    Sql 复习(3)
    sqlserver的soundex
    sqlserver的trim
    Sql 复习(2)
    糟糕的设计
  • 原文地址:https://www.cnblogs.com/lailailai/p/4532241.html
Copyright © 2020-2023  润新知