当前位置:网站首页>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;
}
};边栏推荐
- Do you know all four common cache modes?
- [information security laws and regulations] review
- Cadre de validation des données Apache bval réutilisé
- Uvalive – 4621 CAV greed + analysis "suggestions collection"
- Seize Jay Chou
- Pasqal首席技术官:模拟量子计算率先为工业带来量子优势
- App capture of charles+drony
- 鸿蒙智能家居【1.0】
- I feel cheated. Wechat tests the function of "size number" internally, and two wechat can be registered with the same mobile number
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
猜你喜欢

Do you know all four common cache modes?

抢占周杰伦

Reuse of data validation framework Apache bval

超分辨率技术在实时音视频领域的研究与实践

RISCV64

SD_ DATA_ RECEIVE_ SHIFT_ REGISTER

杰理之相同声道的耳机不允许配对【篇】

Seize Jay Chou

A hodgepodge of ICER knowledge points (attached with a large number of topics, which are constantly being updated)

99% of people don't know that privatized deployment is also a permanently free instant messaging software!
随机推荐
2022上半年朋友圈都在传的10本书,找到了
Numpy——axis
testing and SQA_动态白盒測试[通俗易懂]
Interview vipshop internship testing post, Tiktok internship testing post [true submission]
Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
Teach your sister to write the message queue hand in hand
Basic operation of chain binary tree (implemented in C language)
Short selling, overprinting and stock keeping, Oriental selection actually sold 2.66 million books in Tiktok in one month
Redis cluster and expansion
博睿数据入选《2022爱分析 · IT运维厂商全景报告》
杰理之关于 TWS 声道配置【篇】
Uvalive – 4621 CAV greed + analysis "suggestions collection"
AI写首诗
Do you know all four common cache modes?
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
2022.07.05
Pasqal首席技术官:模拟量子计算率先为工业带来量子优势
POJ 1182 :食物链(并查集)[通俗易懂]
2022.07.02
How much does it cost to develop a small program mall?