首页 > 其他分享 >字典树--字典树题母

字典树--字典树题母

时间:2022-10-26 21:06:03浏览次数:58  
标签:index sentence -- 树题 next Trie dict root 字典


648. Replace Words

Medium

457110FavoriteShare

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"
typedef struct Trie_node{
bool exist;
struct Trie_node* next[26];
Trie_node():exist(false){
memset(next,0,sizeof(Trie_node*) * 26);
}
}TrieNode,*Trie;
class Solution {
public:
string replaceWords(vector<string>& dict, string sentence) {
string Result;
if(sentence.length() == 0){
return Result;
}
Trie root = new TrieNode();
BuildTrieTree(root,dict);

stringstream ss(sentence);
string temp;
ss >> temp;
Result += FindStr(root,temp);
while(ss >> temp){
Result = Result + " " + FindStr(root,temp);
}
return Result;
}
void BuildTrieTree(Trie root,vector<string>& dicts){
for(auto dict : dicts){
Trie p = root;
for(int i = 0;i < dict.length();i ++){
int index = dict[i] - 'a';
if(p->next[index] == NULL){
p->next[index] = new Trie_node();
}
p = p->next[index];
if(i == dict.length() - 1){
p->exist = true;
}
}
}
}
string FindStr(Trie root,const string& str){
Trie p = root;
for(int i = 0;i < str.length();i ++){
int index = str[i] - 'a';
if(p->next[index]){
if(p->next[index]->exist){
return str.substr(0,i + 1);
}
p = p->next[index];
}
else{
return str;
}
}
return str;
}
};

 

标签:index,sentence,--,树题,next,Trie,dict,root,字典
From: https://blog.51cto.com/u_13121994/5798507

相关文章

  • 连线问题(数学题)
    题目描述某一天,Alice比较无聊,于是她为自己发明了一个游戏玩。首先她在纸上画了一个圆,然后从这个圆的圆弧上均匀地取出n个点,这n个点将圆n等分。接下来,Alice每次从这......
  • 倒序排序求次数平方和最大值
    题目描述有一种有趣的字符串价值计算方式:统计字符串中每种字符出现的次数,然后求所有字符次数的平方和作为字符串的价值例如:字符串"abacaba",里面包括4个'a',2个'b',1个......
  • 贪心找零钱
    题目描述楚乔、宇文玥和燕洵在日本旅行,经过了几天的游玩之后,钱包里出现了大量硬币,楚乔决定用钱包里的硬币为宇文玥和燕洵在自动贩卖机买水。楚乔的钱包里有1元、5元、10元、......
  • 有时间再研究吧
    #include<bits/stdc++.h>usingnamespacestd;structpt{intx;intpos;pt(inta,intb):x(a),pos(b){}};structcmp{//重写比较函数booloperator......
  • 利用map对数组中的元素及其下标进行存储
    题目描述小摩有一个N个数的数组,他想将数组从小到大排好序,但是萌萌的小摩只会下面这个操作:任取数组中的一个数然后将它放置在数组的最后一个位置。问最少操作多少次可以使得......
  • Java8新特性-函数式接口
    一、函数式接口1.1简介首先的是一个接口接口内有且只有一个抽象方法为防止破坏函数式接口,最好是在接口上使用@FunctionalInterface注解修饰定义一个函数接口pa......
  • 根据股票code去重然后取每只股票最新的数据
    List<CoverLog>list=collect2.stream().filter(distinctByKey(CoverLog::getStockCode)).collect(Collectors.toList());Map<String,CoverLog>collect4=collect2.st......
  • 美洲大蠊动态规划
    忘得差不多了,来善后一下。0x1树形dp树形dp,说简单了就是在树上按照树从根结点向叶子结点走的递归顺序+dp的状态转移的维护。所以从根结点开始,维护一个点时,先维护它所有......
  • meet-in-the-middle
    一个优化暴力的知名小trick,早就知道,记一下。没啥好的题,手搓一道吧:给定一个\(n\timesn\)矩阵,第\(i\)行第\(j\)列元素为\(a_{i,j}\),求是否存在一条左上到右下的路......
  • 子数组相加和为母数组的和的一半(动态规划题目)
    boolIsMagical(vector<int>&vec){intlen=vec.size();intsum=accumulate(vec.begin(),vec.end(),0);if(sum&1)returnfalse;intmid=......