当前位置:网站首页>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;
}
};
边栏推荐
- [tpm2.0 principle and Application guide] Chapter 9, 10 and 11
- Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
- 嵌入式面试题(算法部分)
- Business experience in virtual digital human
- In 2021, the national average salary was released. Have you reached the standard?
- 杰理之快速配对,不支持取消配对【篇】
- 解决远程rviz报错问题
- 抢占周杰伦
- Pasqal首席技术官:模拟量子计算率先为工业带来量子优势
- Reject policy of thread pool
猜你喜欢
Continuous test (CT) practical experience sharing
Interview vipshop internship testing post, Tiktok internship testing post [true submission]
數據驗證框架 Apache BVal 再使用
Learn open62541 -- [67] add custom enum and display name
Creative changes brought about by the yuan universe
博睿数据入选《2022爱分析 · IT运维厂商全景报告》
Basic concepts and properties of binary tree
2022.07.02
Three forms of multimedia technology commonly used in enterprise exhibition hall design
CMD command enters MySQL times service name or command error (fool teaching)
随机推荐
Multimodal point cloud fusion and visual location based on image and laser
The top of slashdata developer tool is up to you!!!
數據驗證框架 Apache BVal 再使用
杰理之测试盒配置声道【篇】
PV static creation and dynamic creation
String - string (Lua)
[tpm2.0 principle and Application guide] Chapter 9, 10 and 11
手把手教姐姐写消息队列
Redis cluster and expansion
企业MES制造执行系统的分类与应用
RISCV64
【MIME笔记】
Antisamy: a solution against XSS attack tutorial
最长公共前缀(leetcode题14)
如何给“不卖笔”的晨光估值?
Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
ES6笔记一
Redis publishing and subscription
Basic operation of chain binary tree (implemented in C language)
【牛客网刷题系列 之 Verilog进阶挑战】~ 多bit MUX同步器