当前位置:网站首页>648. Word replacement
648. Word replacement
2022-07-07 21:40:00 【anieoo】
Original link :648. Word substitution

solution:
Hash table stores prefix , Violent algorithm for comparison ,O(n^2)
class Solution {
public:
unordered_map<string, int> dict; // Store word prefixes
string replaceWords(vector<string>& dictionary, string sentence) {
string res; // Return value
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;
}
// String splitting function
vector<string> split(string word, char t) {
vector<string> res; // Return value
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 Trees
const int N = 1e5 + 10;
class Solution {
public:
int son[N][26], cnt[N], idx; // Build a dictionary tree
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]++; // With p Ending words +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 prefix appears, replace
if(!son[p][u]) break; // If no prefix exists then ,break
p = son[p][u];
}
return word;
}
// String splitting function
vector<string> split(string word, char t) {
vector<string> res; // Return value
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); // Build a dictionary tree
vector<string> words = split(sentence, ' ');
string res; // Define the return value
for(int i = 0;i < words.size();i++) {
string tmp = find(words[i]);
res += tmp;
if(i != words.size() - 1) res += ' ';
}
return res;
}
};边栏推荐
- Which financial products will yield high returns in 2022?
- Code of "digital image processing principle and Practice (matlab version)" part2[easy to understand]
- 私募基金在中國合法嗎?安全嗎?
- FatMouse&#39; Trade (Hangdian 1009)
- 恶魔奶爸 A1 语音听力初挑战
- POJ 3140 Contestants Division「建议收藏」
- Mysql子查询关键字的使用方式(exists)
- Demon daddy C
- Le capital - investissement est - il légal en Chine? C'est sûr?
- POJ 3140 contents division "suggestions collection"
猜你喜欢

Goal: do not exclude yaml syntax. Try to get started quickly

Virtual machine network configuration in VMWare

Validutil, "Rethinking the setting of semi supervised learning on graphs"
SQL injection error report injection function graphic explanation

NVR硬盘录像机通过国标GB28181协议接入EasyCVR,设备通道信息不显示是什么原因?

The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization

使用枚举实现英文转盲文

ISO 26262 - considerations other than requirements based testing

Problems encountered in installing mysql8 for Ubuntu and the detailed installation process

The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
随机推荐
Validutil, "Rethinking the setting of semi supervised learning on graphs"
EasyCVR配置中心录像计划页面调整分辨率时的显示优化
HDU4876ZCC loves cards(多校题)
UVA 11080 – Place the Guards(二分图判定)
Datatable data conversion to entity
权限不足
Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
开户必须往账户里面赚钱吗,资金安全吗?
恶魔奶爸 A0 英文零基础的自我提升路
现在网上开户安全么?想知道我现在在南宁,到哪里开户比较好?
Implement secondary index with Gaussian redis
Jetty:配置连接器[通俗易懂]
openGl超级宝典学习笔记 (1)第一个三角形「建议收藏」
[C language] advanced pointer --- do you really understand pointer?
201215-03-19 - cocos2dx memory management - specific explanation "recommended collection"
L'enregistreur de disque dur NVR est connecté à easycvr par le Protocole GB 28181. Quelle est la raison pour laquelle l'information sur le canal de l'appareil n'est pas affichée?
Postgresql数据库character varying和character的区别说明
单词反转实现「建议收藏」
FatMouse&#39; Trade (Hangdian 1009)
I have to use my ID card to open an account. Is the bank card safe? I don't understand it