Statistical puzzles
Problem Descriptionignatius recently encountered a problem, the teacher gave him a lot of words (only lowercase letters, no duplicate words appear), now the teacher asked him to count the number of words prefixed with a string (the word itself is also its own prefix).
The first part of input data is a Word table, one word per line, the length of the word is not more than 10, they represent the word that the teacher handed to Ignatius, and a blank line represents the end of the Word table. The second part is a series of questions, one question per line, and each question is a string.
Note: There is only one set of test data, processing to the end of the file.
Output for each question, give the number of words prefixed with the string.
Sample INPUTBANANABANDBEEABSOLUTEACM BABBANDABC
Sample Output2310
1#include <cstdio>2#include <malloc.h>3#include <cstring>4 using namespacestd;5 6 Const intmaxn= -;7 8 structTire9 {TenTire *NEXT[MAXN]; One intv; A Tire () - { -Memset (Next,0,sizeof(next)); thev=0; - } - }; - +Tire *root=NewTire; - + voidCreattire (Char*str) A { at intlen=strlen (str); -Tire *p=Root; - for(intI=0; i<len;i++) - { - intid=str[i]-'a'; - if(p->next[id]==NULL) in { -Tire *q=NewTire; toq->v=1; +p->next[id]=Q; -P=p->Next[id]; the } * Else $ {Panax Notoginsengp->next[id]->v++; -P=p->Next[id]; the } + } A } the + intFindtire (Char*str) - { $ inti; $ intlen=strlen (str); -Tire *p=Root; - for(i=0; i<len;i++) the { - intid=str[i]-'a';Wuyi if(p->next[id]==NULL) the return 0; -P=p->Next[id]; Wu } - returnP->v; About } $ - intMain () - { - //freopen ("In.txt", "R", stdin); A Chars[ One]; + while(Gets (s) &&s[0]!=' /') the { - Creattire (s); $ } the while(SCANF ("%s", s)! =EOF) the { the intans=Findtire (s); theprintf"%d\n", ans); - } in return 0; the}
HDU 1251 Statistical Puzzles (Tire tree)