HDU 1251 统计难题(字典树,map)
来源:互联网 发布:编程软件cimit怎么样 编辑:程序博客网 时间:2024/05/18 17:43
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 42826 Accepted Submission(s): 15388
Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
Sample Input
banana
band
bee
absolute
acm
ba
b
band
abc
Sample Output
2
3
1
0
感觉用map写比较简单
#include<cstdio>#include<cstring>#include<cstdlib>#include<string>#include<map>using namespace std;map<string,int>n;char a[12];int main(){ while(gets(a)&&strlen(a)) {// 不断读入一个串 直到读空就退出来了 n[a]++; for(int i=strlen(a)-1;i>0;i--) { a[i]='\0'; n[a]++; } } while(~scanf("%s",a)) { printf("%d\n",n[a]); } return 0;}
字典树基础题 ,但是被坑了,在杭电提交选 G++ 就内存超限,用c++提交就对了。(ˉ▽ˉ;)…
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;struct node{ node *next[26]; int num; node()//初始化 { num=1; for(int i=0;i<26;i++) next[i]=NULL; }};node *root=new node();//给root申请内存空间void BuildTree(char *a){ node *tree=root; int len=strlen(a); for(int i=0;i<len;i++) { int ii=a[i]-'a'; if(tree->next[ii]==NULL) { tree->next[ii]=new node();//如果为空申请内存空间 } else tree->next[ii]->num++; tree=tree->next[ii]; }}int Find(char *a){ int len=strlen(a); node *p=root;//令p指向字典树的树头 for(int i=0;i<len;i++) { int ii=a[i]-'a'; p=p->next[ii]; if(p==NULL) return 0; } return p->num;}char a[12];int main(){ while(gets(a)&&a[0]!='\0') { BuildTree(a); } while(scanf("%s",a)!=EOF) { printf("%d\n",Find(a)); } return 0;}
阅读全文
0 0
- HDU-1251 统计难题(字典树+map)
- 【HDU】1251 - 统计难题(字典树 || STL - map & string)
- HDU-1251-统计难题(字典树||map)
- HDU 1251 统计难题(字典树,map)
- HDU 1251 (统计难题) 字典树模板&&map实现
- HDU 1251:统计难题【字典树 & string+map】
- HDU-1251 统计难题,字典树或者map!
- hdu 1251-统计难题(字典树||map||数组)
- HDU 1251统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题(字典树)
- hdu 1251 统计难题 (字典树)
- hdu 1251 统计难题(字典树)
- HDU 1251 统计难题(字典树)
- HDU 1251 统计难题(字典树)
- HDU 1251 统计难题(字典树)
- HDU-#1251 统计难题(字典树)
- 剑指Offer:表示数值的字符串
- js的闭包的理解
- STL之序列式容器
- 编程练习2(一)
- 带权并查集 向量并查集 poj1182 食物链
- HDU 1251 统计难题(字典树,map)
- MyEclipse Spring开发教程:用Spring创建iPhone App(2/2)
- CVPR2017_Papers下载爬虫程序
- HDU
- 理解Golang包导入
- 温州嵌入式培训机构
- 计算机系统基础(bomb实验报告)
- android studio如何查看 e.printStackTrace()
- selenium工作原理