新闻详情

新闻详情

首页 / 资讯中心 / 详情

194 · 寻找单词(前缀和二分法)

发布时间:2026/9/8 14:21:51来源:尧图网络
194 · 寻找单词(前缀和二分法)
链接LintCode 炼码题解双指针方法class Solution { public: /** * param str: the string * param dict: the dictionary * return: return words which are subsequences of the string */ vectorstring findWords(string str, vectorstring dict) { // write your code here. std::vectorstd::string result; for (auto q : dict) { if (valid(str, q)) { result.push_back(q); } } return result; } private: // 双指针方法 bool valid1(const std::string p, const std::string q) { if (p.size() 0) { return false; } int i 0; int j 0; while (i p.size() j q.size()) { if (p[i] q[j]) { i; j; } else { i; } } if (j q.size()) { return true; } return false; } };-二分法求解时间复杂度O(dict中所有字符的长度*log(str字符串长度))--将str字符对应的位置存在到ch2pos的hash表中--遍历dict中所有的元素与str进行对比--对比str和dict元素使用双指针进行对比--dict中对应字符在hash对应字符串中查找当前位置的地方class Solution { public: /** * param str: the string * param dict: the dictionary * return: return words which are subsequences of the string */ vectorstring findWords(string str, vectorstring dict) { // write your code here. std::unordered_mapchar, std::vectorint ch2pos; for (int i 0; i str.size(); i) { // 存储每个字符对应的位置 ch2pos[str[i]].push_back(i); } std::vectorstd::string result; // 判断是否为对应的单词 for (auto word : dict) { if (valid(ch2pos, str, word)) { // 追加结果 result.push_back(word); } } return result; } private: int find_left_index(const std::vectorint pos, int cur_pos) { int left 0; int right pos.size()-1; while (left 1 right) { int mid left (right-left)/2; if (pos[mid] cur_pos) { left mid; } else if (pos[mid] cur_pos) { right mid; } else { left mid; } } // 求得 cur_pos的位置 if (pos[left] cur_pos) { return pos[left]; } if (pos[right] cur_pos) { return pos[right]; } return -1; } bool valid(const std::unordered_mapchar, std::vectorint ch2pos, const std::string p, const std::string q) { if (p.size() 0) { return false; } int i 0; int j 0; while (i p.size() j q.size()) { if (p[i] q[j]) { i; j; } else { auto ite ch2pos.find(q[j]); // 如果当前字符不存在 if (ite ch2pos.end()) { return false; } // 查找当前位置该字符存在的位置 int pos find_left_index(ite-second, i); // 不存在该单词位置 if (pos -1) { return false; } /*if (p[pos] ! q[j]) { return false; }*/ // 更新移动下一个位置 i pos1; j; } } // 对比的单词到达末尾 if (j q.size()) { return true; } return false; } };class Solution { public: /** * param str: the string * param dict: the dictionary * return: return words which are subsequences of the string */ vectorstring findWords(string str, vectorstring dict) { // write your code here. std::vectorstd::string result; if (str.size() 0) { return result; } // table[i][j]当前这个字符i位置开始后面最接近的下字符j的位置是那个下标 std::vectorstd::vectorint table(str.size()1, std::vectorint(26, -1)); for (int i str.size() - 1; i 0; --i) { for (int j 0; j 26; j) { table[i][j] table[i1][j]; if (str[i] - a j) { // 如果是他自己就是它当前的位置 table[i][j] i; } } } for (auto q : dict) { if (valid(table, str, q)) { result.push_back(q); } } return result; } private: bool valid(const std::vectorstd::vectorint table, const std::string p, const std::string q) { if (p.size() 0) { return false; } int i 0; int j 0; while (i p.size() j q.size()) { /*if (p[i] q[j]) { i; j; } else {*/ // 获得字符q[j]的位置 int pos table[i][q[j]-a]; // 不存在这个位置 if (pos -1) { return false; } // 继续往后面查找 i pos 1; j; //} } if (j q.size()) { return true; } return false; } };class Solution { public: /** * param str: the string * param dict: the dictionary * return: return words which are subsequences of the string */ vectorstring findWords(string str, vectorstring dict) { // write your code here. int len str.size(); if (len 0) { return {}; } int dict_len dict.size(); if (dict_len 0) { return {}; } vectorvectorint nexts build(str); vectorstring result; for (auto str : dict) { if (is_same(nexts, len, str)) { result.push_back(str); } } return result; } bool is_same(vectorvectorint nexts, int len, const std::string str) { int i 0; int j 0; while (i len j str.size()) { int index str[j] - a; if (nexts[i][index] len) { //return false; break; } i nexts[i][index] 1; j; } // i在走道超过了可能j还没有走到最后 if (j str.size()) { return true; } return false; } vectorvectorint build(const std::string str) { int len str.size(); if (len 0) { return {}; } vectorvectorint nexts(len, vectorint(26, len)); for (int i str.size()-1; i 0; --i) { if (i ! str.size()-1) { nexts[i] nexts[i1]; } nexts[i][str[i]-a] i; /*for (char ch a; ch z; ch) { if (str[i] ch) { nexts[i][ch-a] i; } }*/ } return nexts; } };
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

PyCharm免费集成Google Colab 强化云端Python开发体验 2026/9/8 15:07:04

PyCharm免费集成Google Colab 强化云端Python开发体验

Colab是一种跟云端相关的服务, 用户能够于交互式环境里, 把代码组合起来运行, 还能展示公式, 绘制可视化图表, 撰写说明文档。这个服务不需要在本地进行安装, 不用复杂配置, 针对用户免费开放了各种高性能计算资源, 其中涵盖GPU和TPU, 与此同时, 它会把所有的Colab笔记本都统一…

阅读更多 →
超大文件分片上传与断点续传:WebUploader深度改造实践 2026/9/8 15:07:04

超大文件分片上传与断点续传:WebUploader深度改造实践

1. 项目背景与需求拆解 1.1 军工行业卫星视频文件的特殊性 先说结论:军工行业的卫星视频上传需求,和普通企业的视频上传完全是两个物种。 我最早接到这个需求时,对方提出“卫星视频文件普遍在 10GB 到 200GB 之间,部分原始帧数据…

阅读更多 →
2步拿到全球Offer|我的AI海外求职工作流 2026/9/8 15:07:04

2步拿到全球Offer|我的AI海外求职工作流

1. 为什么选择海外求职 国内职场竞争日益激烈,与其在红海中卷生卷死,不如把目光投向全球市场。远程办公的普及让「身在本地、服务全球」成为现实,海外岗位在薪资、工作节奏和职业成长空间上往往更具吸引力。而 AI 工具的成熟,恰好…

阅读更多 →
揭秘!外贸人工SEO优化的独特渠道大公开 2026/9/8 15:07:04

揭秘!外贸人工SEO优化的独特渠道大公开

痛点深度剖析我们团队在实践中发现,外贸SEO优化存在诸多困境。众多外贸企业在关键词与内容上问题凸显,用中文思维选英文关键词,导致所选词汇并非海外客户常用;内容创作也受限于翻译腔与缺乏行业深度。流量获取更是难题&#xff0c…

阅读更多 →
Claude Code深度评测:安装配置、接入第三方模型与实战边界解析 2026/9/8 15:07:04

Claude Code深度评测:安装配置、接入第三方模型与实战边界解析

1. "没有人是专家":Claude Code凭什么说出这句话,我花了一周验证 先说个最近的观察。我公司在做内部数据分析平台,组里有个从来没写过正经代码的运营同事。上个月她需要把客户发来的几百个Excel表按规则合并清洗,放以前…

阅读更多 →
2026年AI科研软件推荐指南:沁言学术赋能学术研究全流程 2026/9/8 15:04:00

2026年AI科研软件推荐指南:沁言学术赋能学术研究全流程

引言:进入2026年,AI 技术在科研领域的渗透已从“单点工具”迈向“全流程赋能”。面对市面上琳琅满目的 AI 科研软件,研究者常陷入选择困境:文献检索、写作润色、数据分析等工具往往各自为政,难以形成闭环。真正的科研提…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞