链接: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 · 自动补全相关推荐

  1. bootstrap-typeahead 自动补全简单的使用教程

    bootstrap-typeahead 自动补全简单的使用教程 参考链接: 参考1 : https://segmentfault.com/a/1190000006036166 参考2 : https: ...

  2. jQuery搜索框自动补全功能插件实现-autocomplete.js

    最近用nodeclub实现股票的输入关键字自动补全股票信息进行搜索功能,原先用jQuery-ui,结果jQuery-ui库太大,所以考虑用其他插件,最终选择使用autocomplete.js,控件简单 ...

  3. IDEA IntelliJ/ DataGrip 修改自动补全快捷键

    系统默认的是Tab键,个人喜欢用空格键作为自动补全键,设置方法如下 Setting-->Keymap-->Editor Actions:Choose Lookup Item Replace ...

  4. 字符串匹配数据结构 --Trie树 高效实现搜索词提示 / IDE自动补全

    文章目录 1. 算法背景 2. Trie 树实现原理 2.1 Trie 树的构建 2.2 Trie树的查找 2.3 Trie树的遍历 2.4 Trie树的时间/空间复杂度 2.5 Trie 树 Vs ...

  5. linux下 为自己编写的程序 添加tab自动补全 功能

    linux下 为自己编写的程序 添加tab自动补全功能 入门 complete 在我的tmp下随便写了一个a.sh, 为他补全 edit /etc/bash_completion.d/foo _foo ...

  6. [解决]eclipse中android自动补全/提示卡机或假死

    这是Eclipse3.6版本的特有问题,想彻底解决此问题的话,还是建议换为3.5/3.4: 在保持版本不变的前提下,可以按如下方法优化下: 解决办法: 1. 找到你的JDK安装目录下的src.zip文 ...

  7. [Go]在vscode中添加对模板文件tmpl的html语法自动补全的支持

    1.打开设置界面 依次点击:"文件" --> "首选项" --> "设置" 2.打开文件配置 依次点击:"文本编辑器& ...

  8. 【Qt】QtCreator中自动补全注释

    1.简述 在QtCreator中编辑代码,可以自动补全函数注释,供doxygen使用并生成文档.doxygen的使用方法,后续会写一个详细的博文. 2.使用方法 在函数前分别输入"/**&q ...

  9. Autocomplete 自动补全(Webform实战篇)

    开篇语 因为项目中需要用到一个自动补全的功能,功能描述: 需求一:新增收件人的时候,自动下拉显示出数据库中所有的收件人信息(显示的信息包括:姓名-收件地址-联系方式) 需求二:选中一个值得时候,分别赋 ...

最新文章

  1. AIX如何查看文件系统分布在哪个物理磁盘上
  2. 从零开始编写自己的C#框架(9)——数据库设计与创建
  3. Servlet使用适配器模式进行增删改查案例(BaseDaoUtilImpl.java)
  4. 课时55.详情和概要标签(理解)
  5. pandas学习笔记五之pandas合并
  6. python find()效率_基于python分别采用同步与异步(协程)方式抓取时光网TOP100电影...
  7. 净水器怎么放_今日头条 | 如何清洗净水器的陶瓷滤芯,要不要来学一下!
  8. Lucene.Net 2.3.1开发介绍 —— 二、分词(一)
  9. 莫队--2038: [2009国家集训队]小Z的袜子(hose)
  10. 16年几篇经典论文对比与总结
  11. 压测工具ab的安装(mac环境)
  12. 第二语言教学的5c标准是哪5c,第二语言教学法知识小结(一)
  13. 多维Ellipse(椭球)形状与方程对应关系分析
  14. ios 开发控件中心点_AppCan
  15. R语言ggplot2 | 如何自定义facet分面的坐标轴范围
  16. 小程序错误:Setting data field collected to undefined is invalid.
  17. 老婆有外遇系列之一--亲子…
  18. 家常菜做法:熬萝卜粉丝
  19. 捷讯fw300r虚拟服务器口号,迅捷FW300R无线路由器WDS无线桥接设置方法
  20. 三大通信运营商天猫电器城开店

热门文章

  1. CCF201312--模拟练习试题参考答案(C++)
  2. [书目20161206]小狗钱钱的人生整理术
  3. Spark 集群安装
  4. MySQL的备份、还原及恢复
  5. batch size自适应log(1)
  6. windows 9X, 2000, xp所有版本注册表设置(1)
  7. 转载-IDEA项目左边栏只能看到文件看不到项目结构
  8. ubuntu创建用户的两种方式
  9. [最小生成树][Dij] Jzoj P5818 做运动
  10. C++中的未定义的行为