HCPC2014校赛训练赛 3 B.背单词 (3.15)
2014-03-15 17:34
309 查看
B.背单词 | |||||
| |||||
Description | |||||
大四了,Leyni感觉好惆怅,因为找不到工作,所以最后决定考研了,可是Leyni的英语好差,没办法,先从最基本的背单词开始吧。那么多单词怎么才好背呢,话说考研界盛传利用前缀背单词,貌似好神奇的样子。因为英语单词很多,Leyni想要知道以一个特定字符串做前缀的单词有多少,于是他来找你帮忙了。 | |||||
Input | |||||
输入首先包含若干行小写单词,表示字典里的单词,以END结束,然后是若干个询问字符串,表示单词的前缀。输入到文件结束。最多不超过50000个单词。每个单词长度不超过15。 | |||||
Output | |||||
对于每一个询问字符串,每行输出一个整数,表示单词表里有多少单词以此字符串作为前缀。 | |||||
Sample Input | |||||
a ab aba abcaa END aba a ab abcd | |||||
Sample Output | |||||
1 4 3 0 | |||||
Author | |||||
曹振海 |
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; struct node { int count; node *childs[26]; node() { count=0; int i; for(i=0;i<26;i++) childs[i]=NULL; } }; node *root=new node; node *current,*newnode; void insert(char *str) { int i,m; current=root; for(i=0;i<strlen(str);i++) { m=str[i]-'a'; if(current->childs[m]!=NULL) { current=current->childs[m]; ++(current->count); } else { newnode=new node; ++(newnode->count); current->childs[m]=newnode; current=newnode; } } } int search(char *str) { int i,m; current=root; for(i=0;i<strlen(str);i++) { m=str[i]-'a'; if(current->childs[m]==NULL) return 0; current=current->childs[m]; } return current->count; } int main() { char str[20]; while(gets(str),strcmp(str,"END")) insert(str); while(gets(str)!=NULL) printf("%d\n",search(str)); return 0; }
结果:
77702 | B | Accepted | G++ | 32ms | 14792k | 985B | 2014-03-15 15:04:02 |
相关文章推荐
- HCPC2014校赛训练赛 3 D.截取方案数 (3.15)
- hlgHCPC2014校赛训练赛 1 BB.序列问题
- HCPC2014校赛训练赛 4 A.Alphabet Cookies (3.16)
- HCPC2014校赛训练赛 4 B.Sequential Game (3.16)
- HCPC2014校赛训练赛 4 H.Car Race Game (3.16)
- HCPC2014校赛训练赛 6 A.萌萌哒十五酱的情书~ (3.23)
- HCPC2013校赛训练赛 4
- HCPC2013校赛训练赛 1
- HCPC2013校赛训练赛 1
- HCPC2013校赛训练赛 2
- 2014校赛酱油记
- 2014工大校赛题目以及解
- 北航校赛2014 预赛 题解
- 2014工大校赛题目以及解
- 2014哈商大ICPC/ACM校赛解题报告
- 地大校赛2014 题解
- 北航校赛2014 预赛 题解
- 【NOIP2014八校联考第2场第2试9.28】单词接龙
- 【2014】教你如何高效背单词最新方法
- OUC_2014 Spring Training 省赛组队训练赛 #1总结