当前位置:网站首页>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;
}
};
边栏推荐
- EasyCVR配置中心录像计划页面调整分辨率时的显示优化
- 私募基金在中國合法嗎?安全嗎?
- 国家正规的股票交易app有哪些?使用安不安全
- UVA 11080 – place the guards
- 恶魔奶爸 B3 少量泛读,完成两万词汇量+
- [function recursion] do you know all five classic examples of simple recursion?
- What stocks can a new account holder buy? Is the stock trading account safe
- HDU4876ZCC loves cards(多校题)
- Codeforces round 296 (Div. 2) A. playing with paper[easy to understand]
- Problems encountered in installing mysql8 for Ubuntu and the detailed installation process
猜你喜欢
NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?
Redis - basic use (key, string, list, set, Zset, hash, geo, bitmap, hyperloglog, transaction)
The maximum number of meetings you can attend [greedy + priority queue]
Ubuntu安装mysql8遇到的问题以及详细安装过程
South China x99 platform chicken blood tutorial
The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
Dry goods sharing | devaxpress v22.1 original help document download collection
L2:ZK-Rollup的现状,前景和痛点
SQL injection error report injection function graphic explanation
解决uni-app中uni.request发送POST请求没有反应。
随机推荐
Restapi version control strategy [eolink translation]
Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)
South China x99 platform chicken blood tutorial
Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
Codeforces Round #275 (Div. 2) C – Diverse Permutation (构造)[通俗易懂]
Implementation of mahout Pearson correlation
201215-03-19 - cocos2dx memory management - specific explanation "recommended collection"
开户还得用身份证银行卡安全吗,我是小白不懂
sqlHelper的增删改查
Demon daddy A1 speech listening initial challenge
Awk processing JSON processing
Arlo's troubles
easyui 日期控件清空值
Unity3d 4.3.4f1 execution project
margin 等高布局
Codeforces round 296 (Div. 2) A. playing with paper[easy to understand]
Navicat connect 2002 - can't connect to local MySQL server through socket '/var/lib/mysql/mysql Sock 'solve
恶魔奶爸 指南帖——简易版
uva 12230 – Crossing Rivers(概率)「建议收藏」
Restore backup data on persistent volumes