231 · 自动补全
链接:LintCode 炼码
题解:
class Typeahead {
private:struct Trie {Trie() {end = false;}std::unordered_map<char, Trie*> next;bool end;std::unordered_set<std::string> words;};void search_trie(Trie* cur, std::vector<std::string>& result) {if (cur->end) {for (auto& word : cur->words) {result.push_back(word);}}for (auto entry : cur->next) {search_trie(entry.second, result);}}void insert_trie(const std::string& word, const std::string& real_word) {Trie* cur = _root;for (auto& ch : word) {if (cur->next.find(ch) == cur->next.end()) {cur->next[ch] = new (std::nothrow)Trie;}cur = cur->next[ch];}cur->end = true;cur->words.insert(real_word);}Trie* _root;
public:/** @param dict: A dictionary of words dict*/Typeahead(unordered_set<string> dict) {// do intialization if necessary_root = new (std::nothrow)Trie;for (auto& word : dict) {insert_trie(word, word);for (int i = 0; i < word.size(); ++i) {for (int j = 1; i + j <= word.size(); ++j) {insert_trie(word.substr(i, j), word);}}}}/** @param str: a string* @return: a list of words*/vector<string> search(string &str) {// write your code herevector<std::string> result;Trie* cur = _root;for (auto& ch : str) {if (cur->next.find(ch) == cur->next.end()) {return result;}cur = cur->next[ch];}search_trie(cur, result);//result.erase(unique(result.begin(), result.end()), result.end());std::unordered_set<std::string> tmp(result.begin(), result.end());result.clear();std::vector<std::string> real_res(tmp.begin(), tmp.end());return real_res;}};
231 · 自动补全相关推荐
- bootstrap-typeahead 自动补全简单的使用教程
bootstrap-typeahead 自动补全简单的使用教程 参考链接: 参考1 : https://segmentfault.com/a/1190000006036166 参考2 : https: ...
- jQuery搜索框自动补全功能插件实现-autocomplete.js
最近用nodeclub实现股票的输入关键字自动补全股票信息进行搜索功能,原先用jQuery-ui,结果jQuery-ui库太大,所以考虑用其他插件,最终选择使用autocomplete.js,控件简单 ...
- IDEA IntelliJ/ DataGrip 修改自动补全快捷键
系统默认的是Tab键,个人喜欢用空格键作为自动补全键,设置方法如下 Setting-->Keymap-->Editor Actions:Choose Lookup Item Replace ...
- 字符串匹配数据结构 --Trie树 高效实现搜索词提示 / IDE自动补全
文章目录 1. 算法背景 2. Trie 树实现原理 2.1 Trie 树的构建 2.2 Trie树的查找 2.3 Trie树的遍历 2.4 Trie树的时间/空间复杂度 2.5 Trie 树 Vs ...
- linux下 为自己编写的程序 添加tab自动补全 功能
linux下 为自己编写的程序 添加tab自动补全功能 入门 complete 在我的tmp下随便写了一个a.sh, 为他补全 edit /etc/bash_completion.d/foo _foo ...
- [解决]eclipse中android自动补全/提示卡机或假死
这是Eclipse3.6版本的特有问题,想彻底解决此问题的话,还是建议换为3.5/3.4: 在保持版本不变的前提下,可以按如下方法优化下: 解决办法: 1. 找到你的JDK安装目录下的src.zip文 ...
- [Go]在vscode中添加对模板文件tmpl的html语法自动补全的支持
1.打开设置界面 依次点击:"文件" --> "首选项" --> "设置" 2.打开文件配置 依次点击:"文本编辑器& ...
- 【Qt】QtCreator中自动补全注释
1.简述 在QtCreator中编辑代码,可以自动补全函数注释,供doxygen使用并生成文档.doxygen的使用方法,后续会写一个详细的博文. 2.使用方法 在函数前分别输入"/**&q ...
- Autocomplete 自动补全(Webform实战篇)
开篇语 因为项目中需要用到一个自动补全的功能,功能描述: 需求一:新增收件人的时候,自动下拉显示出数据库中所有的收件人信息(显示的信息包括:姓名-收件地址-联系方式) 需求二:选中一个值得时候,分别赋 ...
最新文章
- AIX如何查看文件系统分布在哪个物理磁盘上
- 从零开始编写自己的C#框架(9)——数据库设计与创建
- Servlet使用适配器模式进行增删改查案例(BaseDaoUtilImpl.java)
- 课时55.详情和概要标签(理解)
- pandas学习笔记五之pandas合并
- python find()效率_基于python分别采用同步与异步(协程)方式抓取时光网TOP100电影...
- 净水器怎么放_今日头条 | 如何清洗净水器的陶瓷滤芯,要不要来学一下!
- Lucene.Net 2.3.1开发介绍 —— 二、分词(一)
- 莫队--2038: [2009国家集训队]小Z的袜子(hose)
- 16年几篇经典论文对比与总结
- 压测工具ab的安装(mac环境)
- 第二语言教学的5c标准是哪5c,第二语言教学法知识小结(一)
- 多维Ellipse(椭球)形状与方程对应关系分析
- ios 开发控件中心点_AppCan
- R语言ggplot2 | 如何自定义facet分面的坐标轴范围
- 小程序错误:Setting data field collected to undefined is invalid.
- 老婆有外遇系列之一--亲子…
- 家常菜做法:熬萝卜粉丝
- 捷讯fw300r虚拟服务器口号,迅捷FW300R无线路由器WDS无线桥接设置方法
- 三大通信运营商天猫电器城开店