鱼C论坛

 找回密码
 立即注册
查看: 1099|回复: 2

[技术交流] C++刷leetcode(676. 实现一个魔法字典)【字典树】【深度优先搜索】

[复制链接]
发表于 2020-6-9 15:57:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2020-6-9 15:58 编辑

题目描述:
  1. 实现一个带有buildDict, 以及 search方法的魔法字典。

  2. 对于buildDict方法,你将被给定一串不重复的单词来构建一个字典。

  3. 对于search方法,你将被给定一个单词,并且判定能否只将这个单词中一个字母换成另一个字母,使得所形成的新单词存在于你构建的字典中。

  4. 示例 1:

  5. Input: buildDict(["hello", "leetcode"]), Output: Null
  6. Input: search("hello"), Output: False
  7. Input: search("hhllo"), Output: True
  8. Input: search("hell"), Output: False
  9. Input: search("leetcoded"), Output: False
  10. 注意:

  11. 你可以假设所有输入都是小写字母 a-z。
  12. 为了便于竞赛,测试所用的数据量很小。你可以在竞赛结束后,考虑更高效的算法。
  13. 请记住重置MagicDictionary类中声明的类变量,因为静态/类变量会在多个测试用例中保留。 请参阅这里了解更多详情。
复制代码



  1. class MagicDictionary {
  2. private:
  3.     bool isEnd;
  4.     MagicDictionary* next[26];
  5. public:
  6.     /** Initialize your data structure here. */
  7.     MagicDictionary() {
  8.         isEnd = false;
  9.         memset(next, 0, sizeof(next));

  10.     }
  11.    
  12.     /** Build a dictionary through a list of words */
  13.     void buildDict(vector<string> dict) {
  14.         for(auto cha : dict){
  15.             MagicDictionary* node = this; //注意这块儿的书写!!!!!!!!
  16.             for(auto cha1 : cha){
  17.                 if(node -> next[cha1 - 'a'] == NULL){
  18.                     node -> next[cha1 - 'a'] = new MagicDictionary();
  19.                 }
  20.                 node = node -> next[cha1 - 'a'];
  21.             }
  22.             node -> isEnd = true;
  23.         }
  24.     }
  25.    
  26.     /** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
  27.     void dfs(bool& res, string& word, MagicDictionary* cur, int sum, int step){
  28.         if(sum > 1 || step > word.size()) return;
  29.         if(step == word.size()){
  30.             if(sum == 1 && cur -> isEnd) res = true;
  31.             return;
  32.         }
  33.         for(int i = 0; i < 26; i++){
  34.             MagicDictionary* temp = cur;//注意这块儿的书写!!!!!!!!
  35.             if(temp -> next[i] != NULL){
  36.                 temp = temp -> next[i];
  37.                 if(i != word[step] - 'a'){
  38.                     dfs(res, word, temp, sum + 1, step + 1);//注意这块儿的书写!!!!!!!!
  39.                 }else{
  40.                     dfs(res, word, temp, sum, step + 1);
  41.                 }
  42.             }
  43.         }
  44.     }
  45.     bool search(string word) {
  46.         bool res = false;
  47.         MagicDictionary* node = this;
  48.         dfs(res, word, node, 0, 0);
  49.         return res;
  50.     }
  51. };

  52. /**
  53. * Your MagicDictionary object will be instantiated and called as such:
  54. * MagicDictionary* obj = new MagicDictionary();
  55. * obj->buildDict(dict);
  56. * bool param_2 = obj->search(word);
  57. */
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-6-9 15:57:49 | 显示全部楼层
注意递归的书写格式,以及树的初始化存储。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-9 15:58:28 | 显示全部楼层
掉了好多坑,总算想明白了这道题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-4 18:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表