#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)模板的更多相关文章

  1. hdu1251+字典树常用模板

    这里只简单给出几个常用的字典树的模板,要看具体介绍的请看:传送门 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现) ...

  2. [数据结构]字典树(Tire树)

    概述: Trie是个简单但实用的数据结构,是一种树形结构,是一种哈希树的变种,相邻节点间的边代表一个字符,这样树的每条分支代表一则子串,而树的叶节点则代表完整的字符串.和普通树不同的地方是,相同的字符 ...

  3. HDU 1251 统计难题(字典树入门模板题 很重要)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  4. hdu1251 字典树trie 模板题

    //字典树模板题.题意:给一个库,每次查询,求以之为前缀的单词数量. #include<iostream> #include<string> #include<vecto ...

  5. HDu-1247 Hat’s Words,字典树裸模板!

    Hat's Words 题意:给出一张单词表求有多少个单词是由单词表里的两个单词组成,可以重复!按字典序输出这些单词. 思路:先建一个字典树,然后枚举每个单词,把每个单词任意拆分两部分然后查找. 目测 ...

  6. hdu 1671 Phone List 字典树模板

    Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...

  7. 3道入门字典树例题,以及模板【HDU1251/HDU1305/HDU1671】

    HDU1251:http://acm.hdu.edu.cn/showproblem.php?pid=1251 题目大意:求得以该字符串为前缀的数目,注意输入格式就行了. #include<std ...

  8. Chip Factory(01字典树)

    Chip Factory http://acm.hdu.edu.cn/showproblem.php?pid=5536 Time Limit: 18000/9000 MS (Java/Others)  ...

  9. hdu 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    M ...

随机推荐

  1. hibernate初步4

    JPA 1.JPA概述 JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据.,而Hi ...

  2. 静态导入Static import

    静态导入Static import 要使用静态成员(方法和变量)我们必须给出提供这个静态成员的类. 使用静态导入可以使被导入类的静态变量和静态方法在当前类直接可见,使用这些静态成员无需再给出他们的类名 ...

  3. Android应用开发基础之九:内容提供者(ContentProvider)

    内容提供者 应用的数据库是不允许其他应用访问的 内容提供者的作用:就是让别的应用访问到你的数据库 自定义内容提供者,继承ContentProvider类,重写增删改查方法,在方法中写增删改查数据库的代 ...

  4. VS2010在空解决方案中添加项目

    如题,在空解决方案中添加第一个项目的时候会看不到那个solution解决方案文件,而是你当前添加的项目,当你再添加其他项目的时候就悲催了,找不到这个solution,只能在这个项目文件上新加文件,很郁 ...

  5. javascript宿主对象之window.screen、window.close()/open()、window.moveTo、window.resizeTo

    window.screen属性所提供的是浏览器以外的信息.这里只简单的概述一下: screen.availWidth - 可用的屏幕宽度 (除去操作系统菜单) screen.availHeight - ...

  6. Atitit.木马 病毒 免杀 技术 360免杀 杀毒软件免杀 原理与原则 attilax 总结

    Atitit.木马 病毒 免杀 技术 360免杀 杀毒软件免杀 原理与原则 attilax 总结 1. ,免杀技术的用途2 1.1. 病毒木马的编写2 1.2. 软件保护所用的加密产品(比如壳)中,有 ...

  7. iOS多线程邂逅

    .线程之间的通信 //有一个特别耗时的操作,比如说网络请求,开启子线程去请求网络,我们一般是要在主线程更新UI,如何从子线程跳转到主线程? #import "ViewController.h ...

  8. iOS 清理缓存功能的实现第二种方法

    /** * 清理缓存第二种方法 * * @param sender <#sender description#> */ - (void)clearCache:(id)sender { // ...

  9. C++中static用法总结

    1用于局部变量 C++中局部变量有三种: (1)auto:此关键词常常省略.auto type a 常常简写为type a. 如: int a=auto int a 存储在内存的栈中,只在此局部区域有 ...

  10. 大家一起和snailren学java-(七)多态

    “这个系列觉得没必要这么写,不然质量不会高,还是看一段时间,自己提炼吧” 多态,也称作动态绑定,后期绑定,是三个基本特征中非常重要的一个特征.通过多态,可以消除类型之间的耦合关系.同时多态提供了扩展程 ...