当前位置:网站首页>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;
}
};
边栏推荐
- Devil daddy A0 English zero foundation self-improvement Road
- EasyCVR配置中心录像计划页面调整分辨率时的显示优化
- L2:ZK-Rollup的现状,前景和痛点
- Ad domain group policy management
- gridView自己定义做时间排版「建议收藏」
- Meta force force meta universe system development fossage model
- 死锁的产生条件和预防处理[通俗易懂]
- 智能交通焕发勃勃生机,未来会呈现哪些巨变?[通俗易懂]
- What stocks can a new account holder buy? Is the stock trading account safe
- 解决使用uni-app MediaError MediaError ErrorCode -5
猜你喜欢
你可曾迷茫?曾经的测试/开发程序员,懵懂的小菜C鸟升级......
MySQL storage expression error
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
The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization
[200 opencv routines] 223 Polygon fitting for feature extraction (cv.approxpolydp)
Virtual machine network configuration in VMWare
Usage of MySQL subquery keywords (exists)
Ubuntu安装mysql8遇到的问题以及详细安装过程
ISO 26262 - considerations other than requirements based testing
The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
随机推荐
How to integrate Google APIs with Google's application system (1) -introduction to Google APIs
sqlHelper的增删改查
【JDBC Part 1】概述、获取连接、CRUD
What is the reason for the abnormal flow consumption of 4G devices accessing the easygbs platform?
开户必须往账户里面赚钱吗,资金安全吗?
解决uni-app中uni.request发送POST请求没有反应。
Unity3d 4.3.4f1执行项目
Usage of MySQL subquery keywords (exists)
Magic weapon - sensitive file discovery tool
How much does it cost to develop a small program mall?
Focusing on safety in 1995, Volvo will focus on safety in the field of intelligent driving and electrification in the future
[matrix multiplication] [noi 2012] [cogs963] random number generator
A brief understanding of the in arc__ bridge、__ bridge_ Retained and__ bridge_ transfer
Is it safe to open an account online now? I want to know where I can open an account in Nanning now?
Jetty:配置连接器[通俗易懂]
Devil daddy B1 hearing the last barrier, break through with all his strength
MinGW MinGW-w64 TDM-GCC等工具链之间的差别与联系「建议收藏」
华泰证券可以做到万一佣金吗,万一开户安全嘛
Word inversion implements "suggestions collection"
FatMouse&#39; Trade(杭电1009)