字典树(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 ...
随机推荐
- 【GOF23设计模式】责任链模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_责任链模式.公文审批.供应链系统的采购审批.异常链.过滤器和拦截器调用过程 package com.test.chainO ...
- 高性能文件缓存key-value存储—Memcached
1.高性能文件缓存key-value存储—Redis 2.ASP.NET HttpRuntime.Cache缓存类使用总结 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出 ...
- Javascript的封装
js的封装分为以下几种模式: 工厂模式,代码如下: <!doctype html><html lang="en"><head><meta ...
- Arcgis for android 离线查询
参考.. 官方API demo ... 各种资料 以及.. ArcGIS for Android示例解析之高亮要素-----HighlightFeatures ttp://blog.csdn.net/ ...
- SharePoint 2013 设置自定义布局页
在SharePoint中,我们经常需要自定义登陆页面.错误页面.拒绝访问等:不知道大家如何操作,以前自己经常在原来页面改或者跳转,其实SharePoint为我们提供了PowerShell命令,来修改这 ...
- 树莓派上安装2.8寸TFT触摸屏
在某宝的这里买了一个2.8寸的TFT触摸屏,其实就是中国版的ADAFRUIT 结合店家的帮助文档做了一下,一次点亮,这里也记录一下自己的安装过程(话说会硬件是会比会软件有不同的乐趣~~~) 由于店家推 ...
- oracle断电重启之ORA-00600[4194]
1.问题描述 Oracle服务器断电重启以后无法数据库无法正常连接,使用sqlplus envision/envision连接报错.常见的错误有以下这些: ORA-12518: TNS:listene ...
- android 多屏幕适配 : 第一部分
1.在xml布局文件中,控件的宽度和高度用 dp ; 字体大小用 sp 2.根据屏幕的宽高来动态的适配 , 获取屏幕的宽高的两种方法: 第一种方法: /** * 屏幕的宽度 * 屏幕的高度 * ...
- Iterator 迭代器(一)
迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址.迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些行为 ...
- MyBatis入门(二)---一对一,一对多
一.创建数据库表 1.1.创建数据表同时插入数据 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.6.27-log : Database - mybati ...