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. 原创博客>>>解决粘包问题的方法

    目录 原创博客>>>解决粘包问题的方法 原创博客>>>解决粘包问题的方法 服务端: import socket import struct service=sock ...

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

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

  3. Dijkstra 优先队列优化

    #include <iostream> #include <queue> #include <vector> using namespace std; ; stru ...

  4. CSS基础学习-2.CSS选择器(上)

    元素选择符 关系选择符 属性选择符 伪类选择符 伪对象选择符 一.元素选择符 1.通配符:*{ } 2.类选择符:.类名称{ } 3.id选择符::#id名称{ } 4.类型选择符(标签选择符):标签 ...

  5. Spring 事务相关

    事务类型 数据库事务类型有本地事务和分布式事务: 本地事务:就是普通事务,能保证单台数据库上的操作的ACID,被限定在一台数据库上: 分布式事务:涉及两个或多个数据库源的事务,即跨越多台同类或异类数据 ...

  6. c++实例之通讯录管理系统之清空联系人功能(七)

    #include<iostream> using namespace std; constexpr auto MAX = ; //联系人结构体 struct Person { string ...

  7. Jenkins-邮件模板

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. Vue: 购物车数量加减按钮

    效果图: HTML: <div class="label"> <p class="buy_num">购买数量</p> < ...

  9. [pwnable.kr]--alloca

    0x00: 好久没玩了...去年十月以后就没玩过了TAT 这几天把peach的坑,winafl的坑填了下,就来搞下pwn. 0x01: 这个程序是给了源码的 #include <stdio.h& ...

  10. java+大文件断点续传

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/1269085759/up6-jsp-mysq ...