#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. urllib库初体验以及中文编码问题的探讨

    提出问题:如何简单抓取一个网页的源码 解决方法:利用urllib库,抓取一个网页的源代码 ------------------------------------------------------- ...

  2. 安装性能测试工具:sysbench和使用apache的ab

    一.软件的用途,它主要包括以下几种方式的测试:1.cpu性能2.磁盘io性能3.调度程序性能4.内存分配及传输速度5.POSIX线程性能6.数据库性能(OLTP基准测试) 这个软件为什么找不到官网呢? ...

  3. 【GOF23设计模式】桥接模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_桥接模式.多层继承结构.银行日志管理.管理系统消息管理.人力资源的奖金计算  未用桥接模式: package com.te ...

  4. [TypeScript] JSON对象转TypeScript对象范例

    [TypeScript] JSON对象转TypeScript对象范例 Playground http://tinyurl.com/nv4x9ak Samples class DataTable { p ...

  5. ArrayList 与 LinkedList

    ArrayList:数组结构,插入删除的效率低,查询的效率较高. LinkedList:链接数据结构,插入删除的效率较高,查询的效率低. 两者的使用 ArrayList:适合用作添加数据,做查询. L ...

  6. sqlserver 存储过程 try catch TRANSACTION (转)

    CREATE PROCEDURE YourProcedure    ASBEGIN    SET NOCOUNT ON; BEGIN TRY---------------------开始捕捉异常    ...

  7. SharePoint 使用PowerShell恢复误删的网站集

    在SharePoint网站集的使用中,我们很有可能会误删我们需要的网站集,SharePoint其实并没有把网站集删掉,只是放到了SPDeletedSite中,这样,我们还可以通过PowerShell找 ...

  8. maven eclipse 插件下载地址

    要用的时候,搜索了半天,自己记录下 单独下载地址 http://maven.apache.org/download.cgi eclipse 更新地址 http://download.eclipse.o ...

  9. 【读书笔记】iOS-Xcode-查找特殊字符的方法

    如图所示,为搜索图框,然后,点击放大镜图标------->Insert Pattern---->即可看到特殊字符----->选择特殊字符进行插入. 参考资料:<iOS开发进阶& ...

  10. Spring(六)AOP切入方式

    一.接口切入方式 实现类 package com.pb.entity; /** * 实体类 */ public class Hello { private String name; private S ...