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. mint-ui下拉加载(demo实例)

    <template> <div class="share"> <div class="header"> <div cl ...

  2. JAVA语言程序设计课后习题----第五单元解析(仅供参考)

    1 本题是水题,题目要求你求最大值.最小值,建议你用Arrays.sort函数进行排序,最大值.最小值就可以确定了 import java.util.Arrays; import java.util. ...

  3. JavaWeb【Servlet】

    概念 Servlet是在服务器上运行的小程序.一个Servlet请求对应一个Java类(对应一个Wrapper容器),可以通过请求-响应模式访问这个驻留在内存中的小程序. Tomcat容器等级 上图表 ...

  4. Websocket @serverendpoint 404

    今天写一个前后端交互的websocket , 本来写着挺顺利的,但测试的时候蒙了,前端websocket发的连接请求竟然连接不上 返回状态Status 报了个404 ,然后看后台onError方法也没 ...

  5. Java笔试题及答案

    1.下列不可作为java语言修饰符的是(D) A) a1 B) $1 C) _1 D) 11 答案:java标识符不能以数字开头,包含英文字母,数字,下划线以及$ 2.有一段java 应用程序,它的主 ...

  6. top命令参数详解

    简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按 ...

  7. 浅谈响应式Web设计与实现思路

    是否还在为你的应用程序适配PC端,移动端,平板而苦苦思索呢,是否在寻找如何一套代码适配多终端方式呢,是否希望快速上手实现你的跨终端应用程序呢,是的话,那就看过来吧,本文阐述响应式UI设计相关理论基础, ...

  8. HDU 6041 - I Curse Myself | 2017 Multi-University Training Contest 1

    和题解大致相同的思路 /* HDU 6041 - I Curse Myself [ 图论,找环,最大k和 ] | 2017 Multi-University Training Contest 1 题意 ...

  9. 2018 焦作网络赛 G Give Candies ( 欧拉降幂 )

    题目链接 题意 : 给出 N 个糖果.老师按顺序给 1~N 编号的学生分配糖果.每个学生要么不分.要么最少分一个.且由于是按顺序发放.那么对于某个有分到糖果的编号为 i 的学生.则 1~(i-1) 这 ...

  10. Codevs 1200 同余方程 2012年NOIP全国联赛提高组

    1200 同余方程 2012年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 求关于 x 同余方程 a ...