当前位置:网站首页>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;
}
};边栏推荐
- [tpm2.0 principle and Application guide] Chapter 16, 17 and 18
- How to implement safety practice in software development stage
- Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
- 脑洞从何而来?加州大学最新研究:有创造力的人神经连接会「抄近道」
- 10 schemes to ensure interface data security
- Numpy——2. Shape of array
- 2022-07-04 matlab reads video frames and saves them
- Hutool - lightweight DB operation solution
- How much does it cost to develop a small program mall?
- Three forms of multimedia technology commonly used in enterprise exhibition hall design
猜你喜欢

50亿,福建又诞生一只母基金
![Learn open62541 -- [67] add custom enum and display name](/img/98/e5e25af90b3f98c2be11d7d21e5ea6.png)
Learn open62541 -- [67] add custom enum and display name

Creative changes brought about by the yuan universe

App capture of charles+drony

我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信

3. About cookies

How to choose the appropriate automated testing tools?

虚拟数字人里的生意经

Basic concepts and properties of binary tree

Redis
随机推荐
Mathematical analysis_ Notes_ Chapter 11: Fourier series
10 schemes to ensure interface data security
我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信
超分辨率技术在实时音视频领域的研究与实践
PTA 1101 B是A的多少倍
反爬虫的重点:识别爬虫
L1-025 positive integer a+b (Lua)
IP netns command (memo)
The top of slashdata developer tool is up to you!!!
testing and SQA_ Dynamic white box test [easy to understand]
Basic operation of chain binary tree (implemented in C language)
杰理之相同声道的耳机不允许配对【篇】
SD_ DATA_ RECEIVE_ SHIFT_ REGISTER
[information security laws and regulations] review
Recommend free online SMS receiving platform in 2022 (domestic and foreign)
AI写首诗
Flipping Game(枚举)
Version 2.0 of tapdata, the open source live data platform, has been released
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products