字典树(Tire)模板
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
node *ne[];
int cnt;
}*head;
void insert(char *s)
{
node *p=head,*q;
for(int i=;s[i];i++)
{
int id=s[i]-'a';
if(p->ne[id]!=NULL)
{
p=p->ne[id];
p->cnt++;
}
else
{
q=(node*)malloc(sizeof(node));
for(int i=;i<;i++)
q->ne[i]=NULL;
q->cnt=;
p->ne[id]=q;
p=p->ne[id];
}
}
}
int Q(char *s)//查询有多少以该串为前缀的串的数量
{
node *p=head;
for(int i=;s[i];i++)
{
int id=s[i]-'a';
if(p->ne[id]!=NULL)
p=p->ne[id];
else
return ;
}
return p->cnt;
}
void clean(node *p)//释放空间
{
if(p==NULL) return ;
for(int i=;i<;i++)
{
if(p->ne[i]!=NULL)
clean(p->ne[i]);
}
free(p);
return ;
}
int main()
{
int n,m;
char s[];
while(scanf("%d",&n)!=EOF)
{
head=(node*)malloc(sizeof(node));
for(int i=;i<;i++)
head->ne[i]=NULL;
while(n--)
{
scanf("%s",s);
insert(s);
}
scanf("%d",&m);
while(m--)
{
scanf("%s",s);
printf("%d\n",Q(s));
}
clean(head);
}
return ;
}
hiho1014
#include<stdio.h>
#include<string.h>
const int N=1e5+;
int t[N*][],cnt;
void insert(char *s){
int now=;
for(int i=;s[i];i++){
int id=s[i]-'a';
if(!t[now][id]){
t[now][id]=++cnt;
}
now=t[now][id];
t[now][]++;
}
}
int Q(char *s){
int now=;
for(int i=;s[i];i++){
int id=s[i]-'a';
if(t[now][id])
now=t[now][id];
else
return ;
}
return t[now][];
}
int main(){
int n;
char s[];
while(scanf("%d",&n)!=EOF){
memset(t,,sizeof(t));cnt=;
while(n--){
scanf("%s",s);
insert(s);
}
scanf("%d",&n);
while(n--){
scanf("%s",s);
printf("%d\n",Q(s));
}
}
return ;
}
字典树(Tire)模板的更多相关文章
- hdu1251+字典树常用模板
		
这里只简单给出几个常用的字典树的模板,要看具体介绍的请看:传送门 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现) ...
 - [数据结构]字典树(Tire树)
		
概述: Trie是个简单但实用的数据结构,是一种树形结构,是一种哈希树的变种,相邻节点间的边代表一个字符,这样树的每条分支代表一则子串,而树的叶节点则代表完整的字符串.和普通树不同的地方是,相同的字符 ...
 - HDU 1251 统计难题(字典树入门模板题 很重要)
		
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
 - hdu1251 字典树trie 模板题
		
//字典树模板题.题意:给一个库,每次查询,求以之为前缀的单词数量. #include<iostream> #include<string> #include<vecto ...
 - HDu-1247 Hat’s Words,字典树裸模板!
		
Hat's Words 题意:给出一张单词表求有多少个单词是由单词表里的两个单词组成,可以重复!按字典序输出这些单词. 思路:先建一个字典树,然后枚举每个单词,把每个单词任意拆分两部分然后查找. 目测 ...
 - hdu 1671 Phone List  字典树模板
		
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
 - 3道入门字典树例题,以及模板【HDU1251/HDU1305/HDU1671】
		
HDU1251:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题目大意:求得以该字符串为前缀的数目,注意输入格式就行了. #include<std ...
 - Chip Factory(01字典树)
		
Chip Factory http://acm.hdu.edu.cn/showproblem.php?pid=5536 Time Limit: 18000/9000 MS (Java/Others) ...
 - hdu 1251 统计难题(字典树)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others) M ...
 
随机推荐
- HttpClient总结一之基本使用
			
最近工作中是做了一个handoop的hdfs系统的文件浏览器的功能,是利用webhdfs提供的rest api来访问hdfs来与hdfs进行交互的,其中大量使用HttpClient,之前一直很忙,没什 ...
 - 利用PBFunc在Powerbuilder中解析Json对象
			
利用PBFunc工具在Powerbuilder解析json,只需要调用getattribute方法来获取 解析unicode格式的json: n_pbfunc_json lnv_json lnv_js ...
 - RAID选项
			
RAID:Redundant Array Independent Disk(独立磁盘构成的具有冗余能力的阵列) 最常见的为RAID类型为:0,1,5和10:3和6很少见,但在某些环境中仍然有用. RA ...
 - handler机制的原理(转)
			
Handler概述 andriod提供了Handler 和 Looper 来满足线程间的通信.Handler先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(MessageEx ...
 - jquery只能输入数字方法
			
本方法为验证文本框的输入内容,如果输入的是数字,则提示"√".否则提示“必填,且只能输入数字字符”.在线体验效果:http://keleyi.com/keleyi/phtml/zz ...
 - Nunit在VS2010加载不了程序集的解决办法
			
本机环境: Win7 64位 旗舰版 VS2010 Nunit2.6.3 故障重现步骤: 1.在启动外部应用程序中增加C:\Program Files (x86)\NUnit 2.6.3\bin\nu ...
 - 为什么Android应该根据屏幕分辨率来加载不同的图片文件
			
1.图片在xxhdpi,手机是hdpi的 我们有一个手机是hdpi的.我们还有一个图片,我们把他放在xxhdpi下.当手机显示的时候,系统会去hdpi中找,发现没有图片,最终在xxhpi中找到.终于找 ...
 - 【转】Android中处理崩溃异常
			
大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了 ...
 - Mac 以太网连接 报无效的服务器地址 BasicIPv6ValidationError
			
Mac 以太网连接 报无效的服务器地址 BasicIPv6ValidationError 用Mac这么久,一直是用WiFi连接网络,没搞过以太网连接,我也是醉了 显然 Mac 不能像 Windows ...
 - wxPython简单入门
			
wxPython简介 wxPython 是 Python 语言的一套优秀的 GUI 图形库,允许 Python 程序员很方便的创建完整的.功能键全的 GUI 用户界面. wxPython 是作为优秀 ...