当前位置: 首页 > news >正文

leetcode 困难题 745.Prefix and Suffix Search 前缀和后缀搜索

Problem: 745. Prefix and Suffix Search 前缀和后缀搜索

解题过程

ASCII内,"{"刚好在"z"后面,所以算是特殊字符,按照提示拼起来,然后放入到字典树当中去,并且在{后面的前缀需要求出最大的索引

查询的话直接在字典树上面找就可以,若是空则-1,最后返回最大的索引

Code

class trie { public: bool isend = false; int index = -1; trie* arr[27] = {nullptr}; }; class WordFilter { public: trie* root, *ptr; WordFilter(vector<string>& words) { root = new trie; string tp; for( int i = 0; i < words.size(); i++ ) { ptr = root; tp = "-" + words[i] + "{" + words[i]; while(true) { ptr = root; tp = tp.substr(1, tp.size() - 1); bool pre = false; for(int j = 0; j < tp.size(); j++) { if(ptr->arr[tp[j]-'a'] == nullptr) { ptr->arr[tp[j]-'a'] = new trie; } ptr = ptr->arr[tp[j]-'a']; if(pre) { ptr->index = max(ptr->index, i); } if(tp[j]=='{') pre = true; } if(tp[0]=='{') break; } } } int f(string pref, string suff) { string combine = suff + "{" + pref; ptr = root; for(int i = 0; i < combine.size(); i++) { if(ptr->arr[combine[i]-'a']==nullptr) { return -1; } ptr = ptr->arr[combine[i]-'a']; } return ptr->index; } }; /** * Your WordFilter object will be instantiated and called as such: * WordFilter* obj = new WordFilter(words); * int param_1 = obj->f(pref,suff); */
http://www.gsyq.cn/news/99441.html

相关文章:

  • 力扣1965-丢失信息的雇员
  • 提示词工程技巧-要 “角色化”,而非 “观点化” 学习
  • 英语_阅读_Arrest Report Sheet_待读
  • AI在测试中的应用:现状与未来
  • Linux修改网络配置和IP地址
  • ‌面试技巧:拿下测试岗位的秘诀
  • 网络安全异想天开(不定期更新)
  • 测试左移:构建软件质量的早期防线
  • Flutter 多端落地实战:Web 与桌面应用的性能优化、SEO 与用户体验全攻略
  • [SWPUCTF 2018]SimplePHP
  • 系统启动和DNS
  • Springboot连锁家政保洁管理系统03zmn(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • Flutter 测试全栈指南:从单元测试到黄金路径验证的工程化实践
  • 本凡码农引领杭州小程序开发解决方案赋能企业创新与发展
  • FlutterOpenHarmony商城App标签选择组件开发
  • OP-TEE HelloWorld 文件保护实战:把一份 `helloworld.txt` 交给 Secure World 保管(EKB → PTA → CA)
  • 掌握 Open Graph 协议:让你的网页在社交媒体上大放异彩
  • OOP题目集 4~5 及 课堂测验总结性 Blog
  • 计算机考研408【计算机网络】核心知识点总结
  • 交换机.路由器.防火墙-技术提升【6.8】
  • 记一次服务器异常宕机导致的系统异常
  • 中国板材品牌哪家好?亦木良品,环保板材/全屋定制板材/装修环保板材/衣柜专用板材/桦木板/FOSB板/橡胶木/多层板/颗粒板定制实力厂家,老牌子,质量稳定性价比高 - 全局中转站
  • 破局与进化:数字时代下软件测试人才的机遇与挑战
  • 生物识别系统的测试安全性与漏洞防护实践
  • 智能测试指标动态权重分配研究
  • 考研408--数据结构--day2--顺序表及其增删改查 - 指南
  • 基于单片机的PID调节脉动真空灭菌器上位机远程监控设计
  • 【time-rs】time库 ComponentRange 错误类型详解(error/component_range.rs)
  • std::promise 重难点
  • 大梵公考:国考省考每一年的岗位一样吗?