[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 ...
随机推荐
- pdf.js 在线阅读PDF
在网上找了一下如何在线显示pdf文件.个人还是觉得这个是比较不错的,这里做一个记录. gitHub:https://github.com/mozilla/pdf.js 这是一个开源 ...
- AngularJS 控制器函数
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- vi常用命令学习
(1)移动光标 h : 左移光标l : 右移光标j : 下移光标k : 上移光标 w : 移动到下一个单词词头b : 移动到上一个单词词头e : 移动到本单词的尾部 0 :移动到当前行的开端$ :移动 ...
- Oracle树形结构数据---常见处理情景
Oracle树形结构数据---常见处理情景 1.查看表数据结构 SELECT * FROM QIANCODE.TREE_HIS_TABLE T ORDER BY T.NODE_LEVEL; ...
- wubiuefi-支持新版本ubuntu的wubi
由于某些原因,ubuntu官方不再提供新版的wubi 这就使得部分想快速且安全尝试新版ubuntu的用户望而却步 最近在外文网站找到了wubi的新版本wubiuefi,支持最新版的ubuntu 目前支 ...
- ios应用数据存储方式(归档) - 转
一.简单说明 1.在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦. 2.偏好设置(将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置 ...
- ECSHOP和SHOPEX快递单号查询顺丰插件V8.6专版
发布ECSHOP说明: ECSHOP快递物流单号查询插件特色 本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅 ...
- Laravel 5.5搭建(lunix-ubuntu)
基本配置 PHP >= 7.0.0 PHP OpenSSL 扩展 PHP PDO 扩展 PHP Tokenizer 扩展 PHP XML 扩展 1:nginx sudo apt-get upda ...
- Hadoop(21)-数据清洗(ELT)简单版
有一个诸如这样的log日志 去除长度不合法,并且状态码不正确的记录 LogBean package com.nty.elt; /** * author nty * date time 2018-12- ...
- unity独立游戏开发日记2018/09/27
今天优化了下昨天的代码,并且添加了树木和其他资源的生成.还修复了接近石头后,挖掘图标不出现的bug.目前可以在unity中稳定60-70fps. 详看文章:https://www.cnblogs.co ...