【hdu】 统计难题 (my first 字典序)
2012-08-05 19:07
162 查看
统计难题
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131070/65535K (Java/Other)Total Submission(s) : 1 Accepted Submission(s) : 1
[align=left]Problem Description[/align]
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
[align=left]Input[/align]
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.
注意:本题只有一组测试数据,处理到文件结束.
[align=left]Output[/align]
对于每个提问,给出以该字符串为前缀的单词的数量.
[align=left]Sample Input[/align]
banana
band
bee
absolute
acm
ba
b
band
abc
[align=left]Sample Output[/align]
2
3
1
0
#include<cstdio> #include<cstring> using namespace std; struct Word { int count; Word * p[26]; }*a; void Insert(char str[]) { Word *head=a; int len=strlen(str); for(int i=0;i<len;i++) { int j=str[i]-'a'; if(head->p[j]==NULL) { head->p[j]=new Word; head=head->p[j]; head->count=1; for(int k=0;k<26;k++) head->p[k]=NULL; } else { head=head->p[j]; head->count++; } } return ; } int Search(char str[]) { int len=strlen(str); int i=0; Word *head=a; while(i<len) { int j=str[i]-'a'; if(head->p[j]!=NULL) { head=head->p[j];i++; } else return 0; } return head->count; } int main() { char str[11],que[11]; a = new Word; for(int i=0;i<26;++i) a->p[i]= NULL; while(gets(str) && strcmp(str, "") != 0) Insert(str); while(scanf("%s",que)!=EOF) { printf("%d\n",Search(que)); } return 0; }
相关文章推荐
- HDU 1251 统计难题
- 【字典树】hdu 1251 统计难题
- HDU 1251 统计难题
- HDU - 1251 统计难题
- HDU 1251 统计难题【字典树】
- (HDU - 1251)统计难题
- hdu 1251 统计难题
- HDU 1251 统计难题 【字典树模板题】
- HDU1251——统计难题(字典树模板)
- HDU 1251 统计难题
- HDU 1251 统计难题
- hdu 1251(字典树) 统计难题 http://acm.hdu.edu.cn/showproblem.php?pid=1251
- hdu 1251 统计难题(字典树)
- hdu_1251 统计难题 trie模版
- HDU1251 统计难题【字典树】
- HDU 1251 统计难题
- HDU 1251 统计难题
- hdu 统计难题
- HDU 1251 统计难题 (字典树)
- HDU 1251-统计难题(Trie)