[hihoCoder] Trie树
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树的更多相关文章
- HihoCoder——Trie树
本文出自:http://blog.csdn.net/svitter 原题:http://hihocoder.com/contest/hiho2/problem/1 题解:使用Trie树..基础题目.一 ...
- 【Hihocoder】1014 : Trie树
问题:http://hihocoder.com/problemset/problem/1014 给定一个字符串字典dict,输入字符串str, 要求从dict中找出所有以str为前缀的字符串个数. 构 ...
- hihoCoder #1014 : Trie树 [ Trie ]
传送门 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互 ...
- Trie树 hihocoder 1014
Trie树 hihocoder 1014 传送门 字典树的基本应用 #include<queue> #include<cmath> #include<cstdio> ...
- HihoCoder第二周与POJ3630:Trie树的建立
这又是两道一样的题,都是建立trie树的过程. HihoCoder第二周: 这里其实逻辑都很简单,主要在于数据结构struct的使用. #include <iostream> #inclu ...
- 1014 : Trie树 hihocoder
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. ...
- #1014 : Trie树 HihoCoder(字典树)
描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题: ...
- hihoCoder 1014 Trie树 (Trie)
#1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho是一对好朋友.出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮 ...
- Hihocoder #1014 : Trie树 (字典数树统计前缀的出现次数 *【模板】 基于指针结构体实现 )
#1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...
随机推荐
- codesmith 自动生成C# model 实体模板
<%-- Name:自动生成 Author: 陈胜威 Description: 直接生成model类 --%> <%@ Template Language="C#" ...
- python selenium --滚动条处理
转自:http://www.cnblogs.com/fnng/p/3256563.html 本节重点: 控制滚动条到底部 有时候我们需要控制页面滚动条上的滚动条,但滚动条并非页面上的元素,这个时候就需 ...
- python-循环while
while 只要…条件成立,就一直做…. for 循环会在可迭代的序列被穷尽的时候停止,while 则是在条件不成立的时候停止,因此 while 的作用概括成一句话就是:只要…条件成立,就一直做…. ...
- 关于audio标签播放跨域的问题
遇到过的错误: DOMException: The play() request was interrupted by a new load request. DOMException: Failed ...
- DiskLrucCache使用Demo(强烈推荐,非常好用)
DiskLrucCache使用的Demo,这个demo是从网络获取一张图片,保存到本地缓存中(sdcard和内存),当下载成功后.再打开不会又一次向网络请求图片.而是世界使用本地资源. 要使用Disk ...
- 爱国者布局智能硬件,空探系列PM2.5检測仪“嗅霾狗”大曝光
随着6月1日史上最严禁烟令的正式实施,国内包含北京.上海.成都等大中型城市已经在公共场所全面禁烟.众所周知,实施禁烟令的根本在于促进空气的净化,实现环境的改善,要达到这个目的,光有禁烟令是远远 ...
- Python随手记
类属性的本质是变量对象. os.path.abspath(path) 返回绝对路径,如果填入相对路径,默认会在前面加上当前目录,组合成绝对路径. >>> os.path.abspat ...
- jsp tld的function 自定义方法扩展
引入方式示例: <%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %> 写法示例: <? ...
- 堆积木----vector防止内存超限
蒜头君有 nn 块积木,编号分别为 11 到 nn.一开始,蒜头把第 ii 块积木放在位置 ii.蒜头君进行 mm 次操作,每次操作,蒜头把位置 bb 上的积木整体移动到位置 aa 上面.比如 11 ...
- 简单说明什么是递归?什么情况会使用?并使用java实现一个简单的递归程序。
解答: 1)递归做为一种算法在程序设计语言中广泛应用.是指函数/过程/子程序在运行过程中直接或间接调用自身而产生的重入现象. 2)递归算法一般用于解决三类问题: a.数据的定义是按递归定义的.(Fib ...