统计难题

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 33996    Accepted Submission(s): 12855

Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
 
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

注意:本题只有一组测试数据,处理到文件结束.

 
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
 
Sample Input
banana
band
bee
absolute
acm

ba
b
band
abc

 
Sample Output
2
3
1
0
 
Author
Ignatius.L
 
Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1075 1247 1671 1298 1800 
 
题解:
基本上是Tire树的模板
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define N 500010
struct node{
int w,next[];
}tire[N];
int n,m,cnt;
char s[];
void build(){
int now=,len=strlen(s);
for(int i=;i<len;i++){
if(!tire[now].next[s[i]-'a'+]) tire[now].next[s[i]-'a'+]=++cnt;
now=tire[now].next[s[i]-'a'+];
tire[now].w++;
}
}
int query(){
int now=,len=strlen(s);
for(int i=;i<len;i++){
if(!tire[now].next[s[i]-'a'+]) return ;
now=tire[now].next[s[i]-'a'+];
}
return tire[now].w;
}
int main(){
while(gets(s)){
if(!strcmp(s,"")) break;
build();
}
while(gets(s)) printf("%d\n",query());
return ;
}

hdu1251的更多相关文章

  1. hdu1251(字典树)

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

  2. hdu1251+字典树常用模板

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

  3. hdu1251 && hud 1247 (字典树)

    hdu1251 题目 这道题,主要是在主函数的输入输出上犹豫了. #include<stdio.h> #include<cstring> #include<iostrea ...

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

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

  5. Trie 字典树,hdu1251

    参考博客:https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html 字典树就是单词树,顺着一条路径到达终止结点就形成一个单词,该单词的前缀包含在这 ...

  6. *HDU1251 字典树

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

  7. hdu1251 统计难题

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1251 题目: 统计难题 Time Limit: 4000/2000 MS (Java/Othe ...

  8. hdu1251 统计难题 字典树

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  9. hdu1251 字典树or map

    一道字典树的题,不过看起来用map更为简单 传送门 题意: 给出一堆字符串构成一个字典,求字典里以某字符串为前缀的字符串有几个 思路: 输入字符串时把字符串的前缀全部存进map并标记次数 查询时直接输 ...

  10. hdu1251在词典里统计前缀出现的个数

    banana band bee absolute acm    ba b band abc #include<iostream> using namespace std; //数据结构 s ...

随机推荐

  1. 守护进程和inetd超级服务器

    守护进程: 1 系统启动时,由系统初始化脚本启动.一般在/etc目录下,或者以/etc/rc开头的目录 2 许多网络服务器由inetd超级服务器启动 3 cron守护进程按规则定期执行一些程序 4 用 ...

  2. 基本的TCP编程

    int socket(int family,int type,int protocol); family: AF_INET ipv4协议 AF_INET6 ipv6协议 AF_LOCAL unix域协 ...

  3. center os 6.5 vsftpd 登陆出现 530 错误拒绝 解决方法

    别管那么多 把 /etc/vsftpd/ftpusers  里面的用户名删掉就好了.

  4. uva208 - Firetruck

    Firetruck The Center City fire department collaborates with the transportation department to maintai ...

  5. boost::token_compress_on

    对于场景:string s = "123456",用"3","4"切分,默认情况下(boost::token_compress_off),切 ...

  6. 我的javascript学习之路(一)对象之基础

    做了2个多月的ajax项目,对js的学习觉得了深入了不少,把自己的一些学习的经验贴出来,希望对初学者有所帮助,也希望大家能指出我的错误之处或者不当之处. javascript 是基于对象的语言,为什么 ...

  7. C++ CopyFile

    复制文件 关键点 CopyFile The CopyFile function copies an existing file to a new file. The CopyFileEx functi ...

  8. PHP定时执行任务的实现(转)

    ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行. set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去 $int ...

  9. ubuntu13.04装配oracle11gR2

    http://jingyan.baidu.com/album/bea41d435bc695b4c41be648.html?picindex=2 http://www.360doc.com/conten ...

  10. 关于Android中TextView显示多个空格

    一.直接填写文字,输入多少,显示多少,如下: android:text="AAA     AAA"     ————————>显示:AAA     AAA 二.通过设置str ...