[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是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...
随机推荐
- 使用DotNetZip压缩与解压缩
下载地址:http://dotnetzip.codeplex.com/ 解压后找到\\DotNetZipLib-DevKit-v1.9\zip-v1.9\Release下的Ionic.Zip.dll文 ...
- Oracle 性能调优案例(代码级别)
业务案例一: 业务:千万记录表中查询出50条符合条件的记录. 现象:oracle部署时跨机器,业务取得数据耗时10ms.造成业务性能不达标. 为了突出主题,对于异常分支,均已省略. 对于通常写法, o ...
- 利用dd命令制作u盘iso镜像
现在安装系统都是用u盘安装,那么制作u盘的iso镜像就是必须的了.现在此类工具倒是不少,但是,好用的不多,有的还收费.唉,还是用dd吧,老配方,老味道. 首先:要df -h一下,看看u盘的盘符,类似 ...
- Vivado Logic Analyzer的使用(二)
本文基于Vivado 2014.2,阅读前请参考前文http://blog.chinaaet.com/detail/37264 之前的设计都是出发后直接捕获数据.其实,与chipscope类似,可以设 ...
- APK反编译之一:基础知识
作者:lpohvbe | http://blog.csdn.net/lpohvbe/article/details/7981386 这部分涉及的内容比较多,我会尽量从最基础开始说起,但需要读者一定的a ...
- C# 版 防止 DNS 污染,获取域名真实 IP 地址
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net. ...
- Redis-ha(sentinel)搭建
服务器描述:本次搭建是用来测试,所以是在一台服务器上搭建三个redis服务(一主两从) 服务角色 端口 Redis.conf名称 sentinel配置文件名称 sentinel端口 redis日志路径 ...
- sscanf及sprintf
在程序中,我们肯定会遇到许多处理字符串的操作,当然C++中的string类已经做了很好了,但是也不要忘了C中的sscanf和sprintf 这两个函数用法跟printf和scanf用法很相似,只不过数 ...
- SockIOPool
1. SockIOPool – SockIO池化管理,为上层提供的接口是实例化函数[主要是指定memcached服务器地址,各个机器的权重]:根据key&hashCode获取SockIO-网络 ...
- sqoop从hdfs 中导出数据到mysql
bin/sqoop export \ --connect "jdbc:mysql://mini1:3306/study?useUnicode=true&characterEncodi ...