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

Runtime: 64 ms, faster than 45.77% of C++ online submissions for Replace Words.

#include <vector>
#include <string>
using namespace std; class TrieNode{
public:
string word;
TrieNode* children[];
TrieNode(){
for(int i=; i<; i++) children[i] = nullptr;
word = "";
}
};
class Trie{
public:
TrieNode* root;
Trie() {
root = new TrieNode();
}
void buildtrie(vector<string>& words){
for(int i=; i<words.size(); i++){
TrieNode* tmp = root;
for(int j=; j<words[i].size(); j++){
int idx = words[i][j] - 'a';
if(!tmp->children[idx]) tmp->children[idx] = new TrieNode();
tmp = tmp->children[idx];
}
tmp->word = words[i];
}
} string hasprefix(string word){
TrieNode* tmp = root;
string prefix = "";
//cout << word << endl;
for(int i=; i<word.size(); i++){
int idx = word[i] - 'a';
//cout << word << " " << idx << endl;
if(!tmp->children[idx]) return prefix;
tmp = tmp->children[idx];
if(tmp->word != ""){
//cout << prefix << endl;
prefix = tmp->word;
break;
}
}
return prefix;
} }; class Solution {
public:
string replaceWords(vector<string>& dict, string sentence) {
Trie trie = Trie();
trie.buildtrie(dict);
vector<int> spaceidx;
for(int i=; i<sentence.size(); i++){
if(sentence[i] == ' ') spaceidx.push_back(i);
}
vector<string> sent_vec;
int idx = ;
if(spaceidx.size() == ){
sent_vec.push_back(sentence);
}else{
for(int i=; i<spaceidx.size(); i++){
sent_vec.push_back(sentence.substr(idx, spaceidx[i] - idx));
idx = spaceidx[i] + ;
}
}
//for(auto x : spaceidx) cout << x << endl;
sent_vec.push_back(sentence.substr(idx));
//cout << sent_vec.size() << endl;
//for(auto s : sent_vec) cout << s << endl;
vector<string> retvec; for(int i=; i<sent_vec.size(); i++){
string tmpstr = trie.hasprefix(sent_vec[i]);
//cout << sent_vec[i] << endl;
if(tmpstr == "") retvec.push_back(sent_vec[i]);
else retvec.push_back(tmpstr);
}
string ret = "";
for(auto s : retvec) ret += s + " ";
return ret.substr(,ret.size()-);
}
};

LC 648. Replace Words的更多相关文章

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

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

  2. 648. Replace Words

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

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

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

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

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

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

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

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

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

  7. leetcode 学习心得 (4)

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

  8. pt-online-schema-change 最佳实践(转)

    pt的详细步骤 Step 1: Create the new table. Step 2: Alter the new, empty table. This should be very quick, ...

  9. All LeetCode Questions List 题目汇总

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

随机推荐

  1. Java注解【五、注解实战】

    需求: 1.表:用户ID,用户名,年龄,邮箱. 2.实现方法,传入实体,打印sql. 实现: 1.表: package Annotation; @Table("user") pub ...

  2. Delphi CreateFile函数

  3. 第十一章·Filebeat-使用Filebeat收集日志

    Filebeat介绍及部署 Filebeat介绍 Filebeat附带预构建的模块,这些模块包含收集.解析.充实和可视化各种日志文件格式数据所需的配置,每个Filebeat模块由一个或多个文件集组成, ...

  4. Linux内核网络报文简单流程

    转:http://blog.csdn.net/adamska0104/article/details/45397177 Linux内核网络报文简单流程2014-08-12 10:05:09 分类: L ...

  5. python模块、面向对象编程

    目录: 模块补充 xml 面向对象 一.模块补充 shutil: 文件复制模块:进行文件copy.压缩: 使用方法: 将文件内容拷贝到另一个文件中,可以部分内容 shutil.copyfileobj( ...

  6. E - We Need More Bosses CodeForces - 1000E (tarjan缩点,树的直径)

    E - We Need More Bosses CodeForces - 1000E Your friend is developing a computer game. He has already ...

  7. html标签被div嵌套页面字体变大的解决办法

    html标签被div嵌套页面字体变大的解决办法 <div> <html> <head> <title></title> </head& ...

  8. linux syslog支持 ubuntu

    linux  syslog支持 linux  syslog支持 linux  syslog支持 ??????? https://wenku.baidu.com/view/8cc6b50a0202074 ...

  9. 生产问题之泛型自动推断(JDK1.7新特性)

    今天提完代码,新来同事拉下代码后,如下代码出现异常: List<TblBlockMoneyDtl> transData = new ArrayList<>(); 分析原因后发现 ...

  10. pycharm中报错:ImportError: No module named 'skimage'

    pycharm中直接setting是没办法成功的,会提示is requied C++ bool.....先进入那个页面安装好需要的tools,这个安装比较慢:http://landinghub.vis ...