This is a application of the Trie data structure, with minor extension. The critical part in this problem is to count all the words that have a particualr prefix and the problem has given nice hints to make this extension.

Two functions require to be implemented: add a word to the Trie and search the Trie for all words with a particular prefix.

The code is as follows.

If you are not familiar with Trie, you may refer to this solution first to get the basic idea of it.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class TrieNode {
  6. public:
  7. int count;
  8. TrieNode* children[];
  9. TrieNode() {
  10. count = ;
  11. for (int i = ; i < ; i++)
  12. children[i] = NULL;
  13. }
  14. };
  15.  
  16. class Dictionary {
  17. public:
  18. Dictionary() {
  19. root = new TrieNode();
  20. }
  21.  
  22. void insert(char* word) {
  23. TrieNode* run = root;
  24. for (int i = ; word[i]; i++) {
  25. if (!(run -> children[word[i] - 'a']))
  26. run -> children[word[i] - 'a'] = new TrieNode();
  27. run = run -> children[word[i] - 'a'];
  28. run -> count++;
  29. }
  30. }
  31.  
  32. int search(char* prefix) {
  33. TrieNode* run = root;
  34. for (int i = ; prefix[i]; i++) {
  35. if (run)
  36. run = run -> children[prefix[i] - 'a'];
  37. else break;
  38. }
  39. if (!run) return false;
  40. return run -> count;
  41. }
  42.  
  43. private:
  44. TrieNode* root;
  45. };
  46.  
  47. int main(void) {
  48. int dictSize;
  49. while (scanf("%d", &dictSize) != EOF) {
  50. Dictionary dictionary;
  51. char word[];
  52. for (int i = ; i < dictSize; i++) {
  53. scanf("%s", word);
  54. dictionary.insert(word);
  55. }
  56. int querySize;
  57. scanf("%d", &querySize);
  58. char prefix[];
  59. for (int i = ; i < querySize; i++) {
  60. scanf("%s", prefix);
  61. printf("%d\n", dictionary.search(prefix));
  62. }
  63. }
  64. return ;
  65. }

[hihoCoder] Trie树的更多相关文章

  1. HihoCoder——Trie树

    本文出自:http://blog.csdn.net/svitter 原题:http://hihocoder.com/contest/hiho2/problem/1 题解:使用Trie树..基础题目.一 ...

  2. 【Hihocoder】1014 : Trie树

    问题:http://hihocoder.com/problemset/problem/1014 给定一个字符串字典dict,输入字符串str, 要求从dict中找出所有以str为前缀的字符串个数. 构 ...

  3. hihoCoder #1014 : Trie树 [ Trie ]

    传送门 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互 ...

  4. Trie树 hihocoder 1014

    Trie树 hihocoder 1014 传送门 字典树的基本应用 #include<queue> #include<cmath> #include<cstdio> ...

  5. HihoCoder第二周与POJ3630:Trie树的建立

    这又是两道一样的题,都是建立trie树的过程. HihoCoder第二周: 这里其实逻辑都很简单,主要在于数据结构struct的使用. #include <iostream> #inclu ...

  6. 1014 : Trie树 hihocoder

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. ...

  7. #1014 : Trie树 HihoCoder(字典树)

    描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题: ...

  8. hihoCoder 1014 Trie树 (Trie)

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho是一对好朋友.出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮 ...

  9. Hihocoder #1014 : Trie树 (字典数树统计前缀的出现次数 *【模板】 基于指针结构体实现 )

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...

随机推荐

  1. codesmith 自动生成C# model 实体模板

    <%-- Name:自动生成 Author: 陈胜威 Description: 直接生成model类 --%> <%@ Template Language="C#" ...

  2. python selenium --滚动条处理

    转自:http://www.cnblogs.com/fnng/p/3256563.html 本节重点: 控制滚动条到底部 有时候我们需要控制页面滚动条上的滚动条,但滚动条并非页面上的元素,这个时候就需 ...

  3. python-循环while

    while 只要…条件成立,就一直做…. for 循环会在可迭代的序列被穷尽的时候停止,while 则是在条件不成立的时候停止,因此 while 的作用概括成一句话就是:只要…条件成立,就一直做…. ...

  4. 关于audio标签播放跨域的问题

    遇到过的错误: DOMException: The play() request was interrupted by a new load request. DOMException: Failed ...

  5. DiskLrucCache使用Demo(强烈推荐,非常好用)

    DiskLrucCache使用的Demo,这个demo是从网络获取一张图片,保存到本地缓存中(sdcard和内存),当下载成功后.再打开不会又一次向网络请求图片.而是世界使用本地资源. 要使用Disk ...

  6. 爱国者布局智能硬件,空探系列PM2.5检測仪“嗅霾狗”大曝光

        随着6月1日史上最严禁烟令的正式实施,国内包含北京.上海.成都等大中型城市已经在公共场所全面禁烟.众所周知,实施禁烟令的根本在于促进空气的净化,实现环境的改善,要达到这个目的,光有禁烟令是远远 ...

  7. Python随手记

    类属性的本质是变量对象. os.path.abspath(path) 返回绝对路径,如果填入相对路径,默认会在前面加上当前目录,组合成绝对路径. >>> os.path.abspat ...

  8. jsp tld的function 自定义方法扩展

    引入方式示例: <%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %> 写法示例: <? ...

  9. 堆积木----vector防止内存超限

    蒜头君有 nn 块积木,编号分别为 11 到 nn.一开始,蒜头把第 ii 块积木放在位置 ii.蒜头君进行 mm 次操作,每次操作,蒜头把位置 bb 上的积木整体移动到位置 aa 上面.比如 11  ...

  10. 简单说明什么是递归?什么情况会使用?并使用java实现一个简单的递归程序。

    解答: 1)递归做为一种算法在程序设计语言中广泛应用.是指函数/过程/子程序在运行过程中直接或间接调用自身而产生的重入现象. 2)递归算法一般用于解决三类问题: a.数据的定义是按递归定义的.(Fib ...