当前位置:网站首页>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;
}
};
边栏推荐
- Navicat connect 2002 - can't connect to local MySQL server through socket '/var/lib/mysql/mysql Sock 'solve
- Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
- 恶魔奶爸 B1 听力最后壁垒,一鼓作气突破
- [function recursion] do you know all five classic examples of simple recursion?
- 恶魔奶爸 指南帖——简易版
- 智能交通焕发勃勃生机,未来会呈现哪些巨变?[通俗易懂]
- Can Huatai Securities achieve Commission in case of any accident? Is it safe to open an account
- GridView defines its own time for typesetting "suggestions collection"
- The cyberspace office announced the measures for data exit security assessment, which will come into force on September 1
- Demon daddy C
猜你喜欢
【JDBC Part 1】概述、获取连接、CRUD
[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
Using enumeration to realize English to braille
Validutil, "Rethinking the setting of semi supervised learning on graphs"
Dry goods sharing | devaxpress v22.1 original help document download collection
Debugging and handling the problem of jamming for about 30s during SSH login
为什么Win11不能显示秒数?Win11时间不显示秒怎么解决?
SQL注入报错注入函数图文详解
Goal: do not exclude yaml syntax. Try to get started quickly
C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point
随机推荐
Validutil, "Rethinking the setting of semi supervised learning on graphs"
What are the official stock trading apps in the country? Is it safe to use
OpenGL super classic learning notes (1) the first triangle "suggestions collection"
Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
Demon daddy B2 breaks through grammar and completes orthodox oral practice
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?
Ubuntu安装mysql8遇到的问题以及详细安装过程
I have to use my ID card to open an account. Is the bank card safe? I don't understand it
South China x99 platform chicken blood tutorial
MySQL约束之默认约束default与零填充约束zerofill
UVA 11080 – place the guards
Use br to recover backup data on azure blob storage
How much does it cost to develop a small program mall?
【JDBC Part 1】概述、获取连接、CRUD
Take the intersection of two sets
Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]
Implementation of mahout Pearson correlation
Codeforces 474 F. Ant colony
现在网上开户安全么?想知道我现在在南宁,到哪里开户比较好?
What stocks can a new account holder buy? Is the stock trading account safe