HDU1251 统计难题(字典树)
来源:互联网 发布:coredata 更新数据 编辑:程序博客网 时间:2024/05/23 15:45
Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
Sample Input
bananabandbeeabsoluteacmbabbandabc
Sample Output
2310
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<set>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define eps 1e-8typedef __int64 ll;using namespace std;#define N 27struct node{ int num; node *nest[N];} ;node *root;int ans;void inint(){int i,j;root=(node *)malloc(sizeof(node));root->num=0;for(i=0;i<N;i++)root->nest[i]=NULL;}void inset(char *c){ node * cur=root; while(*c){if(cur->nest[*c-'a']==NULL){node *temp=(node*)malloc(sizeof(node));for(int i=0;i<N;i++)temp->nest[i]=NULL;temp->num=0;cur->nest[*c-'a']=temp;}cur=cur->nest[*c-'a'];cur->num+=1;c++;}}void seach(char *c){int i,j;node *cur=root;int len=strlen(c);for(i=0;i<len;i++){if(cur->nest[c[i]-'a']==NULL)return ;cur=cur->nest[c[i]-'a'];}ans+=cur->num;}int main(){int i,j;char c[N];inint();while(gets(c)){if(!c[0]) break;inset(c);}while(~scanf("%s",c)){ans=0;seach(c);printf("%d\n",ans);}return 0;}
0 0
- hdu1251 统计难题(字典树)
- hdu1251统计难题(字典树)
- hdu1251 统计难题(字典树)
- hdu1251 统计难题(字典树)
- hdu1251统计难题(字典树模版)
- HDU1251 统计难题(字典树模板)
- HDU1251统计难题(字典树模板)
- HDU1251 统计难题(字典树)
- 统计难题(hdu1251,字典树)
- hdu1251 统计难题 (字典树查找)
- hdu1251统计难题(字典树)
- hdu1251 统计难题(字典树)
- hdu1251 统计难题(字典树)
- HDU1251-统计难题(字典树)
- hdu1251 统计难题 字典树
- 【字典树】 hdu1251 统计难题
- hdu1251 统计难题(字典树)
- 统计难题(hdu1251字典树)
- CentOS下多节点Openstack安装(五)—— cinder安装
- (十一)桥梁模式
- 更好的使用google www.google.com/ncr
- 定义返回函数指针(地址)的函数
- 5、VTK在图像处理中的应用
- HDU1251 统计难题(字典树)
- ACM--steps--3.1.5--
- Java4Android学习六 - 向上转型和向下转型
- 调试系统初始时的视角
- adb 命令使用之抓取log并过滤。
- Fill the Square
- 欢迎使用CSDN-markdown编辑器
- OnlyBill 记账软件
- 将十进制转换成八进制