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. 团队项目:二次开发--v.2.1--软件工程

    原先代码,对于基本对象的Get,Set方法构造函数等方法与实现基本功能的方法统一放到了一起,容易造成代码不清晰,别人比较难阅读的情况.而且其中代码冗余比较多. 改进代码,进行了层次的分析,将基本对象与 ...

  2. iOS.ObjC.Basic-Knowledge

    1. ObjC的基础 2. ObjC2.0中的编译指令 3. ObjC Runtime 4. ObjC Object Model 5. ObjC的新语法 6. FQA 1. ObjC的基础 2. Ob ...

  3. boost::asio 学习草稿

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio/ 可以多个线程拥有io_ ...

  4. sublime3 多行编辑.摘抄

    Sublime text 3是一个非常强大的网站编辑工具. 这里小云深深的被它的快速编辑多行内容功能所吸引. 先说下,使用下面的功能要安装一个叫emmet的插件.没有的话,自行度娘吧. 下面就来看下具 ...

  5. div添加滚动条常见属性

    由于页面上的表里的末一列的内容太多,显示的内容不美观了,就想在这一列上加滚动条,在网上搜了一下,用div可以实现,感觉还不错,下面的是在网上查到的.  想在div里添加滚动条设置一下style就ok了 ...

  6. 27. Green Building 绿色建筑

    27. Green Building 绿色建筑 ①When we think of green buildings,we tend to think of new ones-the kind of h ...

  7. javascript对数组分页

    function pagination(pageNo, pageSize, array) { var offset = (pageNo - 1) * pageSize; return (offset ...

  8. 使用bat批处理文件备份postgresql数据库

    @echo offset pgsql_path=d:\"Program Files"\PostgreSQL\9.3\bin\   //安装目录set database=postgr ...

  9. 在windows7下创建ftp服务站点

    1.开始->控制面板->程序(点击“卸载程序”)->启动或关闭windows功能->Internet Information Services(Internet信息服务)-&g ...

  10. Matlab 中以分数显示结果

    转http://www.blogbus.com/shijuanfeng-logs/234881647.html Matlab,计算得到的结果一般是小数形式. 但为了更精确表示,我们有时候需要用到分数形 ...