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.

 #include <iostream>

 using namespace std;

 class TrieNode {
public:
int count;
TrieNode* children[];
TrieNode() {
count = ;
for (int i = ; i < ; i++)
children[i] = NULL;
}
}; class Dictionary {
public:
Dictionary() {
root = new TrieNode();
} void insert(char* word) {
TrieNode* run = root;
for (int i = ; word[i]; i++) {
if (!(run -> children[word[i] - 'a']))
run -> children[word[i] - 'a'] = new TrieNode();
run = run -> children[word[i] - 'a'];
run -> count++;
}
} int search(char* prefix) {
TrieNode* run = root;
for (int i = ; prefix[i]; i++) {
if (run)
run = run -> children[prefix[i] - 'a'];
else break;
}
if (!run) return false;
return run -> count;
} private:
TrieNode* root;
}; int main(void) {
int dictSize;
while (scanf("%d", &dictSize) != EOF) {
Dictionary dictionary;
char word[];
for (int i = ; i < dictSize; i++) {
scanf("%s", word);
dictionary.insert(word);
}
int querySize;
scanf("%d", &querySize);
char prefix[];
for (int i = ; i < querySize; i++) {
scanf("%s", prefix);
printf("%d\n", dictionary.search(prefix));
}
}
return ;
}

[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. mybatis想要在控制台显示sql语句配置文件

    在src目录下创建一个properties文件 配置内容如下 log4j.rootLogger=debug,stdout,logfile log4j.appender.stdout=org.apach ...

  2. php-fig组织fig-standards的一些标准

    参考: http://psr.phphub.org/ https://github.com/php-fig/fig-standards https://github.com/PizzaLiu/PHP- ...

  3. 点滴积累【C#】---错误日志记录到txt文本里。

    效果: 描述:将系统中的错误信息,try catch到日志里面. 代码: [后端代码] using System; using System.Collections.Generic; using Sy ...

  4. 156. Merge Intervals【easy】

    Given a collection of intervals, merge all overlapping intervals.   Example Given intervals => me ...

  5. 655. Big Integer Addition【easy】

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

  6. 当一个线程进入一个对象的synchronized方法A之后,其他线程是否可进入此对象的synchronized方法B?

    给出答案: 是不能的,其他线程只能访问该对象的非同步方法,同步方法则不能进入; 因为非静态方法上的synchronized修饰符要求执行方法时要获得对象的锁,如果已经进入A方法,说明对象锁已经被取

  7. zoj 1109 Language of FatMouse(字典树)

    Language of FatMouse Time Limit: 10 Seconds      Memory Limit: 32768 KB We all know that FatMouse do ...

  8. jsp时间格式化

    <fmt:formatDate value="${start.time }" pattern="HH:mm" />

  9. JQ Ajax 上传文件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. checkbox 更新回显

    if($row['name']==$_SESSION['name']){$checked="checked disabled";}else{$checked=" &quo ...