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

思路:

思路很朴素,就是将句子里每一个单词从到尾求子串,如果这个子串出现在了dict里,那么就把这个单词替换掉。

string replaceWords(vector<string>& dict, string sentence)
{
map<string, string>mpdic;
for (auto s : dict)mpdic[s] = s;
vector<string>word;
stringstream ss(sentence);
string tmp;
while (ss >> tmp)word.push_back(tmp);
for (auto &str : word)
{
for (int i = ; i < str.length();i++)
{
if (mpdic.count(str.substr(,i)))
{
str = str.substr(, i);
break;
}
}
}
string ret = word[];
for (int i = ; i < word.size();i++)ret += " " + word[i];
return ret;
}

[leetcode-648-Replace Words]的更多相关文章

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

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

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

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

  3. 648. Replace Words

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

  4. LeetCode 1234. Replace the Substring for Balanced String

    原题链接在这里:https://leetcode.com/problems/replace-the-substring-for-balanced-string/ 题目: You are given a ...

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

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

  6. Leetcode 648.单词替换

    单词替换 在英语中,我们有一个叫做 词根(root)的概念,它可以跟着其他一些词组成另一个较长的单词--我们称这个词为 继承词(successor).例如,词根an,跟随着单词 other(其他),可 ...

  7. LC 648. Replace Words

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

  8. Java实现 LeetCode 648 单词替换(字典树)

    648. 单词替换 在英语中,我们有一个叫做 词根(root)的概念,它可以跟着其他一些词组成另一个较长的单词--我们称这个词为 继承词(successor).例如,词根an,跟随着单词 other( ...

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

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

  10. leetcode 学习心得 (4)

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

随机推荐

  1. 在cengos中安装zabbix server/agent, 并创建一个简单demo

    添加zabbix更新源 rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch ...

  2. ProjectServer如何创建时间表

    默认配置的ProjectServer是没有时间表的,任务汇报的时候不能汇报工时,只能汇报任务的百分比. 但如果有企业一定要用工时来汇报的话,我们就需要开启时间表. 点击服务器设置-->时间报告阶 ...

  3. 菜鸟崛起 DB Chapter 4 MySQL 5.6的数据库引擎

    数据库存储引擎是数据库底层的软件组件,我们平常看不到,但是却与我们操作数据库息息相关.DBMS使用数据引擎进行创建.查询.更新和删除数据操作.不同的存储引擎提供不同的存储机制.索引技巧.锁定水平等功能 ...

  4. BionicApi 学习笔记

    1.内存管理 malloc, realloc, free new, delete2.文件输入操作 fopen, fwrite, fputs, fputc, fprintf, fflush fread, ...

  5. ABAP术语-Business Object Type

    Business Object Type 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/10/1033480.html Generic de ...

  6. Java中的文件和stream流的操作代码

    1.Java中FileRead方法的运用代码及详解 package example2;import java.io.FileReader;import java.io.IOException;clas ...

  7. nginx: [error] open() "/var/run/nginx.pid" failed (2: No such file or directory)

    解决办法: nginx nginx -s reload

  8. nginx 安装目录详解

    rpm -ql nginx 路径 类型 介绍 /etc/logrotate.d/nginx  配置文件  Nginx 日志轮转,用于logrotate服务日志切割 /etc/nginx /etc/ng ...

  9. Angular : 绑定, 参数传递, 路由

    如何把jquery导入angular npm install jquery --savenpm install @type/jquery --save-dev "node_modules/z ...

  10. go web处理上传

    要使表单能够上传文件,第一步就是添加form的enctype属性,enctype属性有如下三种情况: application/x-www-form-urlencoded 表示在发送前编码所有字符(默认 ...