博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1251 统计难题
阅读量:6976 次
发布时间:2019-06-27

本文共 2288 字,大约阅读时间需要 7 分钟。

D - 统计难题
Time Limit:2000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u
Submit     

Description

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

Input

输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 
注意:本题只有一组测试数据,处理到文件结束. 
 

Output

对于每个提问,给出以该字符串为前缀的单词的数量. 
 

Sample Input

banana band bee absolute acm ba b band abc
 

Sample Output

2 3 1 0
 
爆内存代码
1 #include
2 #include
3 #include
4 #include
5 #define len strlen(s) 6 using namespace std; 7 typedef struct node{ 8 int v; 9 node *next[26];10 }node;11 node root;12 int build(char *s){13 node *p=&root,*q;14 for(int i=0;i
next[id]==NULL){17 q=(node*)malloc(sizeof(root));18 q->v=1;19 for(int j=0;j<26;j++)20 q->next[j]=NULL;21 p->next[id]=q;22 p=p->next[id];23 }24 else{25 p->next[id]->v++;26 p=p->next[id];27 }28 }29 }30 int findt(char *s){31 node *p=&root;32 for(int i=0;i
next[id];35 if(p==NULL) return 0;36 }37 return p->v;38 }39 int main(){40 char str[15];41 for(int i=0;i<26;i++)42 root.next[i]=NULL;43 while(gets(str)&&str[0]!='\0')44 build(str);45 memset(str,NULL,sizeof str);46 while(~scanf("%s",str)){47 int ans=findt(str);48 printf("%d\n",ans);49 }50 return 0;51 }

 

代码
1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 vector
vec[26]; 7 int cnt,flag=0;int len[26]; 8 string str; 9 int main()10 {11 while(getline(cin,str)){12 int tmp=str[0]-'a';13 if(str==""){14 flag=1;15 for(int i=0;i<26;i++) len[i]=vec[i].size();16 continue;17 }18 if(!flag) vec[tmp].push_back(str);19 else{20 cnt=0;21 for(int i=0;i

 

 

 

转载于:https://www.cnblogs.com/shenben/p/5459878.html

你可能感兴趣的文章
Jenkins实现自动化部署
查看>>
golang martini 源码阅读笔记之martini核心
查看>>
UniDac数据库连接池
查看>>
C#学习十之windows应用和windows phone应用中的SQLite操作
查看>>
hdu Largest Rectangle in a Histogram
查看>>
关于redis
查看>>
MySQL外键约束
查看>>
node(Buffer缓存区)
查看>>
emplace_back减少内存拷贝和移动
查看>>
linux学习之centos(三):网卡配置
查看>>
面经中高频知识点归纳(二)
查看>>
setTimeout()和setInterval() 何时被调用执行
查看>>
5、新干线大爆炸事件
查看>>
第十八条:接口优于抽象类
查看>>
Spring中的AOP——在Advice方法中获取目标方法的参数(转)
查看>>
称重管理系统,您的磅房安装了吗
查看>>
11-vue的使用
查看>>
动态规划算法(java)
查看>>
os模块的几种方法
查看>>
tensorflow API _ 4 (Logging with tensorflow)
查看>>