[leetcode-648-Replace Words]
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:
- The input will only have lower-case letters.
- 1 <= dict words number <= 1000
- 1 <= sentence words number <= 1000
- 1 <= root length <= 100
- 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]的更多相关文章
- LeetCode 648. Replace Words (单词替换)
题目标签:HashMap 题目给了我们一个array 的 root, 让我们把sentence 里面得每一个word 去掉它得 successor. 把每一个root 存入hash set,然后遍历s ...
- 【LeetCode】648. Replace Words 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 字典 前缀树 日期 题目地址:https:/ ...
- 648. Replace Words
Problem statement In English, we have a concept called root, which can be followed by some other wor ...
- LeetCode 1234. Replace the Substring for Balanced String
原题链接在这里:https://leetcode.com/problems/replace-the-substring-for-balanced-string/ 题目: You are given a ...
- 648. Replace Words 替换成为原来的单词
[抄题]: In English, we have a concept called root, which can be followed by some other words to form a ...
- Leetcode 648.单词替换
单词替换 在英语中,我们有一个叫做 词根(root)的概念,它可以跟着其他一些词组成另一个较长的单词--我们称这个词为 继承词(successor).例如,词根an,跟随着单词 other(其他),可 ...
- LC 648. Replace Words
In English, we have a concept called root, which can be followed by some other words to form another ...
- Java实现 LeetCode 648 单词替换(字典树)
648. 单词替换 在英语中,我们有一个叫做 词根(root)的概念,它可以跟着其他一些词组成另一个较长的单词--我们称这个词为 继承词(successor).例如,词根an,跟随着单词 other( ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
随机推荐
- LinQ 简介
LinQ是3.0新加的语法.用起来比较方便,我们可以使用较简单的方法来过滤数据和处理数据. 使用场景: 可以看到LINQ使用场景还是很多的.现在写项目基本都会用到. 在出现委托之前,我们来查找对象 在 ...
- ffmpeg一些filter用法、以及一些功能命令
来源:http://blog.csdn.net/dancing_night/article/details/46776903 1.加字幕 命令:ffmpeg -i <input> -fil ...
- o'Reill的SVG精髓(第二版)学习笔记——第一章
1.1图形系统 计算机中描述图形信息的两大系统是栅格系统(raster graphics)和矢量图形(vector graphics) 1.1.4矢量图形的用途 ①计算机辅助绘图(CAD)程序. ②设 ...
- Python 学习笔记(六)Python第一个程序
Python 语句 赋值语句 1.将3对象赋值给了变量a 2.将3,4赋值给了变量a,b >>> a = 3 >>> a ,b = 3,4 >>> ...
- 菜鸟笔记 -- Chapter 6.1 面向对象概述
6.1 面向对象概述 在程序开发的初期人们使用结构化开发语言,但是随着时间的流逝,软件的规模越来越庞大,结构化语言的弊端逐渐暴露出来,开发周期被无限制拖延,产品质量也不尽如人意,人们终于发现结构化语言 ...
- Angularjs基础(五)
AngularJS Select(选项框) AngularJS 可是使用数组或对象创建一个下拉列表选项.使用ng-options创建选项框 在AngularJS 中我们可以使用ng-option指令来 ...
- LeetCode 中级 - 第k个排列(60)
可以用数学的方法来解, 因为数字都是从1开始的连续自然数, 排列出现的次序可以推 算出来, 对于n=4, k=15 找到k=15排列的过程: 1 + 对2,3,4的全排列 (3!个) 2 + 对1,3 ...
- Java分享笔记:创建多线程 & 线程同步机制
[1] 创建多线程的两种方式 1.1 通过继承Thread类创建多线程 1.定义Thread类的子类,重写run()方法,在run()方法体中编写子线程要执行的功能. 2.创建子线程的实例对象,相当于 ...
- CSS基础全荟
一.CSS概述 1.css是什么?? 层叠样式表 2.css的引入方式 1.行内样式 在标签上加属性style="属性名1:属性值1;属性名2:属性值2;..." 2.内嵌式 ...
- doc命令操作数据库(下)
1.给数据表添加一组数据: 2.给数据表添加多组数据: 3.对数据进行删除和修改: 4.用select查询单个或多个数据信息: 5.去除重复值: 6.查询的各种用法: between的用法: 查询排序 ...