当前位置:网站首页>648. 单词替换
648. 单词替换
2022-07-07 17:07:00 【anieoo】
原题链接:648. 单词替换

solution:
哈希表存储前缀,暴力算法进行比较,O(n^2)
class Solution {
public:
unordered_map<string, int> dict; //存储单词前缀
string replaceWords(vector<string>& dictionary, string sentence) {
string res; //返回值
for(auto &x : dictionary) dict[x]++;
vector<string> words = split(sentence, ' ');
for(auto &word : words) {
for(int i = 0;i < word.size();i++) {
string tmp = word.substr(0, i + 1);
if(dict[tmp]) {
word = tmp;
break;
}
}
}
for(int i = 0;i < words.size();i++) {
res += words[i];
if(i != words.size() - 1) res += ' ';
}
return res;
}
//字符串分割函数
vector<string> split(string word, char t) {
vector<string> res; //返回值
int n = word.size();
for(int i = 0;i < n;i++) {
int j = i;
string item;
while(j < n && word[j] != t) item += word[j++];
i = j;
res.push_back(item);
}
return res;
}
};trie树
const int N = 1e5 + 10;
class Solution {
public:
int son[N][26], cnt[N], idx; //建立字典树
void add(string word) {
int p = 0;
for(int i = 0;i < word.size();i++) {
int u = word[i] - 'a';
if(!son[p][u]) son[p][u] = ++idx;
p = son[p][u];
}
cnt[p]++; //以p结尾的单词+1
}
string find(string word) {
int p = 0;
for(int i = 0;i < word.size();i++) {
int u = word[i] - 'a';
if(cnt[p]) return word.substr(0, i); //如果出现前缀则替换
if(!son[p][u]) break; //如果不存在前缀则,break
p = son[p][u];
}
return word;
}
//字符串分割函数
vector<string> split(string word, char t) {
vector<string> res; //返回值
int n = word.size();
for(int i = 0;i < n;i++) {
int j = i;
string item;
while(j < n && word[j] != t) item += word[j++];
i = j;
res.push_back(item);
}
return res;
}
string replaceWords(vector<string>& dictionary, string sentence) {
for(auto &word : dictionary) add(word); //建立字典树
vector<string> words = split(sentence, ' ');
string res; //定义返回值
for(int i = 0;i < words.size();i++) {
string tmp = find(words[i]);
res += tmp;
if(i != words.size() - 1) res += ' ';
}
return res;
}
};边栏推荐
- Flipping Game(枚举)
- 2022.07.02
- Zhong Xuegao wants to remain innocent in the world
- How many are there (Lua)
- ip netns 命令(备忘)
- Tips and tricks of image segmentation summarized from 39 Kabul competitions
- UVALive – 4621 Cav 贪心 + 分析「建议收藏」
- PV static creation and dynamic creation
- In the first half of 2022, I found 10 books that have been passed around by my circle of friends
- Classification and application of enterprise MES Manufacturing Execution System
猜你喜欢
![Interview vipshop internship testing post, Tiktok internship testing post [true submission]](/img/69/b27255c303150430df467ff3b5cd08.gif)
Interview vipshop internship testing post, Tiktok internship testing post [true submission]

杰理之手动配对方式【篇】

Three forms of multimedia technology commonly used in enterprise exhibition hall design

A hodgepodge of ICER knowledge points (attached with a large number of topics, which are constantly being updated)
![[information security laws and regulations] review](/img/da/c9318ea8999c3ee629b0e48ab78581.png)
[information security laws and regulations] review

如何给“不卖笔”的晨光估值?

链式二叉树的基本操作(C语言实现)

5billion, another master fund was born in Fujian

10 schemes to ensure interface data security

Business experience in virtual digital human
随机推荐
Flipping Game(枚举)
鸿蒙智能家居【1.0】
Reject policy of thread pool
Policy mode - unity
Pasqal首席技术官:模拟量子计算率先为工业带来量子优势
SD_ DATA_ RECEIVE_ SHIFT_ REGISTER
[tpm2.0 principle and Application guide] Chapter 9, 10 and 11
二叉树的基本概念和性质
【Base64笔记】「建议收藏」
Borui data was selected in the 2022 love analysis - Panoramic report of it operation and maintenance manufacturers
POJ 1182 :食物链(并查集)[通俗易懂]
coming! Gaussdb (for Cassandra) new features appear
Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
L1-019 who falls first (Lua)
Desci: is decentralized science the new trend of Web3.0?
Where does brain hole come from? New research from the University of California: creative people's neural connections will "take shortcuts"
Number - number (Lua)
多个kubernetes集群如何实现共享同一个存储
杰理之关于 TWS 声道配置【篇】
I feel cheated. Wechat tests the function of "size number" internally, and two wechat can be registered with the same mobile number