• [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.

    字典树,注意匹配 '.' 的时候要对所有的子树进行匹配,这时就得用DFS了。

     1 class WordDictionary {
     2 private:
     3     struct trinode {
     4         trinode *ch[26];
     5         bool iskey;
     6         trinode(): iskey(false) {
     7             for (auto &a : ch) a = NULL;
     8         }
     9     };
    10     
    11     trinode *root;
    12     
    13     void _addWord(string s) {
    14         trinode *p = root;
    15         for (auto &a : s) {
    16             int idx = a - 'a';
    17             if (p->ch[idx] == NULL) p->ch[idx] = new trinode();
    18             p = p->ch[idx];
    19         }
    20         p->iskey = true;
    21     }
    22     
    23     bool _search(trinode *root, string s, int pos) {
    24         if (pos == s.length()) return root->iskey;
    25         if (s[pos] == '.') {
    26             for (auto &p : root->ch) {
    27                 if (p != NULL && _search(p, s, pos + 1)) return true;
    28             }
    29             return false;
    30         } else {
    31             int idx = s[pos] - 'a';
    32             return (root->ch[idx] && _search(root->ch[idx], s, pos + 1));
    33         }
    34     }
    35     
    36 public:
    37     WordDictionary() {
    38         root = new trinode();
    39     }
    40     // Adds a word into the data structure.
    41     void addWord(string word) {
    42         _addWord(word);
    43     }
    44 
    45     // Returns if the word is in the data structure. A word could
    46     // contain the dot character '.' to represent any one letter.
    47     bool search(string word) {
    48         return _search(root, word, 0);
    49     }
    50 };
    51 
    52 // Your WordDictionary object will be instantiated and called as such:
    53 // WordDictionary wordDictionary;
    54 // wordDictionary.addWord("word");
    55 // wordDictionary.search("pattern");

     搜索的时候可以只有遇到 '.' 的时候才递归,否则就迭代。可以提高一点点效率。

     1     bool _search(trinode *root, string s, int pos) {
     2         if (pos == s.length()) return root->iskey;
     3         trinode *p = root;
     4         for (; pos < s.length(); ++pos) {
     5             if (s[pos] != '.') {
     6                 int idx = s[pos] - 'a';
     7                 if (p->ch[idx] == NULL) return false;
     8                 p = p->ch[idx];
     9             } else {
    10                 for (int idx = 0; idx < 26; ++idx) {
    11                     if (p->ch[idx] && __search(p->ch[idx], s, pos + 1)) return true;
    12                 }
    13                 return false;
    14             }
    15         }
    16         return p->iskey;
    17     }
  • 相关阅读:
    Map集合
    Collection的另外一个子类LinkedList&Set集合
    多项式牛顿迭代 学习笔记
    《混凝土数学》第二章 和式 学习笔记
    洛谷P5039 最小生成树 题解
    gdfzoj#236 | 提高组练习题16 Set
    CF979E 题解
    CF1039D 题解
    CF886E 题解
    CF1061C 题解
  • 原文地址:https://www.cnblogs.com/easonliu/p/4507917.html
Copyright © 2020-2023  润新知