https://leetcode.com/problems/add-and-search-word-data-structure-design/

本题是在Trie树进行dfs+backtracking操作。

Trie树模板代码见:http://www.cnblogs.com/fu11211129/p/4952255.html

题目介绍:

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.

  1. struct Trie{
  2. Trie *next[]; //include character '.'
  3. bool isWord;
  4. Trie() {
  5. for(auto &n : this->next) n = NULL;
  6. this->isWord = false;
  7. }
  8. };
  9. class WordDictionary {
  10. public:
  11. Trie *root;
  12. WordDictionary() {
  13. this->root = new Trie();
  14. }
  15.  
  16. void insert(string s) {
  17. Trie *p = this->root;
  18. for(auto &c: s) {
  19. int idx = c - 'a';
  20. if(!p->next[idx]) p->next[idx] = new Trie();
  21. p = p->next[idx];
  22. }
  23. p->isWord = true;
  24. }
  25.  
  26. void addWord(string word) {
  27. insert(word);
  28. }
  29.  
  30. bool dfs(Trie *p, string word, int idx) {
  31. if(idx == word.size()-) {
  32. if(word[idx] == '.') {
  33. for(int i=;i<;++i) {
  34. if(p->next[i] != NULL && p->next[i]->isWord) return true;
  35. }
  36. return false;
  37. }
  38. else {
  39. int nidx = word[idx] - 'a';
  40. if(p->next[nidx] == NULL) return false;
  41. else return p->next[nidx]->isWord;
  42. }
  43. }
  44.  
  45. if(word[idx] == '.') {
  46. for(int i=;i<;++i) {
  47. if(p->next[i] != NULL && dfs(p->next[i], word, idx+)) return true;
  48. }
  49. }
  50. else {
  51. int nidx = word[idx] - 'a';
  52. if(! p->next[nidx]) return false;
  53. if(p->next[nidx] && dfs(p->next[nidx], word, idx+)) return true;
  54. }
  55. return false;
  56. }
  57.  
  58. // Returns if the word is in the data structure. A word could
  59. // contain the dot character '.' to represent any one letter.
  60. bool search(string word) {
  61. bool flag = false;
  62. for(int i=;i<;++i) {
  63. if(root->next[i] != NULL) {
  64. flag = true; break;
  65. }
  66. }
  67. if(!flag) return false;
  68.  
  69. return dfs(root, word, );
  70. }
  71. };
  72.  
  73. // Your WordDictionary object will be instantiated and called as such:
  74. // WordDictionary wordDictionary;
  75. // wordDictionary.addWord("word");
  76. // wordDictionary.search("pattern");

leetcode@ [211] Add and Search Word - Data structure design的更多相关文章

  1. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  2. (*medium)LeetCode 211.Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  3. [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  4. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

  5. [leetcode]211. Add and Search Word - Data structure design添加查找单词 - 数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  6. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  7. 【LeetCode】211. Add and Search Word - Data structure design

    Add and Search Word - Data structure design Design a data structure that supports the following two ...

  8. 【刷题-LeetCode】211. Add and Search Word - Data structure design

    Add and Search Word - Data structure design Design a data structure that supports the following two ...

  9. 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...

随机推荐

  1. hdu 4403

    水水的dfs #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath& ...

  2. 【leetcode】Combination Sum II (middle) ☆

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. URAL 1260 Nudnik Photographer(递推)

    题目链接 题意 : 给你1到n这n个数,排成一排,然后1放在左边最开始,剩下的数进行排列,要求排列出来的数列必须满足任何两个相邻的数之间的差不能超过2,问你有多少种排列 思路 : 对于dp[n], n ...

  4. 【BZOJ 1038】 1038: [ZJOI2008]瞭望塔

    1038: [ZJOI2008]瞭望塔 Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 ...

  5. Android 通过http访问服务器

    目前Android 与服务器交互有两种方式:1.Socket 2. Http : 但由于Http的封装性以及性能比socket要好,所以推荐使用http方式和服务器交互: 通过http访问服务器有三种 ...

  6. Android:时间控件

    1.选择时间TimePicker    监听器:OnTimeChangedListener(obj,int hour,int minute); 常用: 获取时:getCurrentHour(). 获取 ...

  7. SRM 586 DIV1 L1

    可以化简为求n条线段的最大覆盖问题,需要注意的是对于实数而言. #include <iostream> #include <vector> #include <strin ...

  8. 关于C的一些理解

    关于字符数组和字符指针 关于相互赋值问题一只有疑问,其实是自己搞不清指针和地址的关系.地址可以指向一块内存但是不一定存在于内存,比如字符数组名,数组名是地址,但是不实际存在于内存中,无法修改,而字符指 ...

  9. IPC是什么意思?

    IPC(Inter-Process Communication,进程间通信)IPC ( Instruction per Clock 及CPU每一时钟周期内所执行的指令多少) IPC代表了一款处理器的设 ...

  10. Where is Vasya?

    Where is Vasya? Vasya stands in line with number of people p (including Vasya), but he doesn't know ...