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");
  • 相关阅读:
    【转载】Hadoop 文件副本存放的位置策略
    【原创】Hadoop 常用命令集锦
    【转载】centos6.2下安装星际译王stardict3.0
    【转载】hadoop之failed task任务和killed task任务
    【原创】linux 下远程连接windows桌面
    【原创】Hadoop配置详解
    【原创】Pig 0.10.0 piggybank 如何编译
    【转载】关于hadoop的dfs.replication
    【原创】centos 安装 thrift 0.8
    【原创】ASP.NET常见问题集锦
  • 原文地址:https://www.cnblogs.com/lailailai/p/4532241.html
Copyright © 2011-2022 走看看