In English, we have a concept called root, which can be followed by some other words to form another longer word - let's call this word successor. For example, the root an, followed by other, which can form another word another.

Now, given a dictionary consisting of many roots and a sentence. You need to replace all the successor in the sentence with the root forming it. If a successor has many roots can form it, replace it with the root with the shortest length.

You need to output the sentence after the replacement.

Example 1:

Input: dict = ["cat", "bat", "rat"]
sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"

Note:

  1. The input will only have lower-case letters.
  2. 1 <= dict words number <= 1000
  3. 1 <= sentence words number <= 1000
  4. 1 <= root length <= 100
  5. 1 <= sentence words length <= 1000
class Solution {
public:
string replaceWords(vector<string>& dict, string sentence) {
set<string> myDict;
for(int i=;i<dict.size();i++)
myDict.insert(dict[i]);
vector<string> mySentence;
for(int i=,j=;i<sentence.size() && j<=sentence.size();j++)//把字符串分成单词
{
if(sentence[j]==' ' || j==sentence.size())
{
mySentence.push_back(sentence.substr(i,j-i));
i=j+;
}
}
string res="";
for(int i=;i<mySentence.size();i++)
{
for(int j=;j<mySentence[i].size();j++)
{
if(myDict.find(mySentence[i].substr(,j+)) != myDict.end())
{
mySentence[i]=mySentence[i].substr(,j+);
break;
}
}
res+=(mySentence[i]+" ");
}
return res.substr(,res.size()-);
}
};

Trie-648. Replace Words的更多相关文章

  1. 648. Replace Words

    Problem statement In English, we have a concept called root, which can be followed by some other wor ...

  2. LC 648. Replace Words

    In English, we have a concept called root, which can be followed by some other words to form another ...

  3. 【LeetCode】648. Replace Words 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 字典 前缀树 日期 题目地址:https:/ ...

  4. 648. Replace Words 替换成为原来的单词

    [抄题]: In English, we have a concept called root, which can be followed by some other words to form a ...

  5. LeetCode 648. Replace Words (单词替换)

    题目标签:HashMap 题目给了我们一个array 的 root, 让我们把sentence 里面得每一个word 去掉它得 successor. 把每一个root 存入hash set,然后遍历s ...

  6. 算法与数据结构基础 - 字典树(Trie)

    Trie基础 Trie字典树又叫前缀树(prefix tree),用以较快速地进行单词或前缀查询,Trie节点结构如下: //208. Implement Trie (Prefix Tree)clas ...

  7. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  8. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  10. Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

    All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...

随机推荐

  1. Golang之waitgroup用法

    我敲下一堆代码,终于长出了果实,今天是个伟大日子 package main import ( "fmt" "sync" "time" ) / ...

  2. Extended Backus–Naur Form

    From Wikipedia, the free encyclopedia In computer science, Extended Backus–Naur Form (EBNF) is a fam ...

  3. BZOJ4407 于神之怒加强版 - 莫比乌斯反演

    题解 非常裸的莫比乌斯反演. 但是反演完还需要快速计算一个积性函数(我直接用$nlogn$卷积被TLE了 推荐一个博客 我也不想再写一遍了 代码 #include<cstring> #in ...

  4. Mina 系列(二)之基础

    Mina 系列(二)之基础 Mina 使用起来多么简洁方便呀,就是不具备 Java NIO 的基础,只要了解 Mina 常用的 API,就可以灵活使用并完成应用开发. 1. Mina 概述 首先,看 ...

  5. 构造函数constructor 与析构函数destructor(二)

    (1)转换构造函数 转换构造函数的定义:转换构造函数就是把普通的内置类型转换成类类型的构造函数,这种构造函数只有一个参数.只含有一个参数的构造函数,可以作为两种构造函数,一种是普通构造函数用于初始化对 ...

  6. LVDS_IP仿真分析

    这个一个对tx_outclock移相180度后的仿真结果. tx_outclock的时钟沿与数据中心对齐. tx_coreclock时钟与inclock时钟频率相等,但有相差.

  7. i9-9900k烤机

    新装机一台,记录烤机参数 硬件配置: cpu: i9-9900k 主板:技嘉Z390 AORUS PRO WIFI 内存:海盗船ddr4 3200 显卡:技嘉gtx1080ti 硬盘:三星970Pro ...

  8. android插件化简述

    2015年是Android插件化技术突飞猛进的一年,随着业务的发展各大厂商都碰到了Android Native平台的瓶颈: 从技术上讲,业务逻辑的复杂导致代码量急剧膨胀,各大厂商陆续出到65535方法 ...

  9. 从0学习JQ

    转 张子秋的博客 为以后用到的时候好查询! 从零开始学习jQuery (一) 开天辟地入门篇 从零开始学习jQuery (二) 万能的选择器 从零开始学习jQuery (三) 管理jQuery包装集 ...

  10. MFC中的Invalidate、OnDraw、OnPaint函数的作用

    MFC中的Invalidate.OnDraw.OnPaint函数的作用 CWnd::Invalidate voidInvalidate( BOOL bErase = TRUE ); 该函数的作用是使 ...