鱼C论坛

 找回密码
 立即注册
查看: 1519|回复: 1

[技术交流] C++刷LeetCode(648. 单词替换)【字典树】

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

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

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

x
本帖最后由 糖逗 于 2021-1-26 10:33 编辑

题目描述:
  1. 在英语中,我们有一个叫做 词根(root)的概念,它可以跟着其他一些词组成另一个较长的单词——我们称这个词为 继承词(successor)。例如,词根an,跟随着单词 other(其他),可以形成新的单词 another(另一个)。

  2. 现在,给定一个由许多词根组成的词典和一个句子。你需要将句子中的所有继承词用词根替换掉。如果继承词有许多可以形成它的词根,则用最短的词根替换它。

  3. 你需要输出替换之后的句子。

  4.  

  5. 示例 1:

  6. 输入:dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
  7. 输出:"the cat was rat by the bat"
  8. 示例 2:

  9. 输入:dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
  10. 输出:"a a b c"
  11. 示例 3:

  12. 输入:dictionary = ["a", "aa", "aaa", "aaaa"], sentence = "a aa a aaaa aaa aaa aaa aaaaaa bbb baba ababa"
  13. 输出:"a a a a a a a a bbb baba a"
  14. 示例 4:

  15. 输入:dictionary = ["catt","cat","bat","rat"], sentence = "the cattle was rattled by the battery"
  16. 输出:"the cat was rat by the bat"
  17. 示例 5:

  18. 输入:dictionary = ["ac","ab"], sentence = "it is abnormal that this solution is accepted"
  19. 输出:"it is ab that this solution is ac"
  20.  

  21. 提示:

  22. 1 <= dictionary.length&#160;<= 1000
  23. 1 <= dictionary[i].length <= 100
  24. dictionary[i]&#160;仅由小写字母组成。
  25. 1 <= sentence.length <= 10^6
  26. sentence&#160;仅由小写字母和空格组成。
  27. sentence 中单词的总量在范围 [1, 1000] 内。
  28. sentence 中每个单词的长度在范围 [1, 1000] 内。
  29. sentence 中单词之间由一个空格隔开。
  30. sentence&#160;没有前导或尾随空格。

  31. 来源:力扣(LeetCode)
  32. 链接:https://leetcode-cn.com/problems/replace-words
  33. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码




  1. class Solution {
  2. public:
  3.     //字典树的结构
  4.     struct TrieTree{
  5.         bool flag;
  6.         map<char, TrieTree*> next;
  7.         TrieTree(): flag(false){};
  8.     };
  9.     string replaceWords(vector<string>& dictionary, string sentence) {
  10.         //构建前缀树
  11.         //先建立根节点
  12.         TrieTree* root = new TrieTree();
  13.         for(auto word : dictionary){
  14.             TrieTree* node = root;//从根节点开始搜索
  15.             for(auto cha : word){
  16.                 if((node -> next).count(cha) == 0){
  17.                     node -> next[cha] = new TrieTree();
  18.                 }
  19.                 node = node -> next[cha];
  20.             }
  21.             node -> flag = true;
  22.         }
  23.         //搜索前缀返回结果
  24.         string res;
  25.         int start = 0, end = 0;
  26.         int len = sentence.size();
  27.         for(int i = 0; i < len; i++){
  28.             start = i;
  29.             TrieTree* node = root;//从根节点开始找
  30.             while(i < len && sentence[i] != ' '){
  31.                 if(node -> flag == true || (node -> next).count(sentence[i]) == 0)break;
  32.                 node = node -> next[sentence[i]];
  33.                 i++;
  34.             }
  35.             if(node -> flag == true){
  36.                 //找到了
  37.                 end = i;
  38.                 //定位到下一个start的位置
  39.                 while(i < len && sentence[i] != ' ')i++;
  40.             }else if((node -> next).count(sentence[i]) == 0){
  41.                 //没找到
  42.                 //定位到下一个start的位置
  43.                 while(i < len && sentence[i] != ' ')i++;
  44.                 end = i;
  45.             }
  46.             res += " " + sentence.substr(start, end - start);
  47.         }
  48.         res.erase(res.begin());
  49.         return res;
  50.     }
  51. };
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-12-19 14:38:27 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-7 13:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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