Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=1251
Node *head= (node*) malloc (sizeof(node)), for (int i=0; i<26; i++) { Head->next[i] = NULL; head->sum = 0;}
Can be changed to node *head=new node ();
Must be submitted in C + +, or g++ memory overrun-_-
#include <stdio.h>#include<math.h>#include<string.h>#include<algorithm>#include<stdlib.h>using namespacestd;structnode{intsum; Node*next[ -];};voidBuildlibtree (Node *head,Chars[]) {Node*p = head,*Q; for(intI=0; T[n]; i++) { intK = S[i]-'a'; if(P->next[k] = =NULL) {Q= (node*)malloc(sizeof(node)); for(intj=0; j< -; J + +) {Q->NEXT[J] =NULL; Q->sum =0; } P->NEXT[K] =Q; } P= p->Next[k]; P->sum++; }}intQuery (Node *head,Chars[]) {Node*p =Head; for(intI=0; T[n]; i++) { intK = s[i]-'a'; if(P->next[k] = =NULL)return 0; P= p->Next[k]; } returnP->sum;}intMain () {Chars[ -]; Node*head= (node*)malloc(sizeof(node)); for(intI=0; i< -; i++) {Head->next[i] =NULL; Head->sum =0; } while(Gets (s), s[0]) Buildlibtree (head, s); while(SCANF ("%s", s)! =EOF) printf ("%d\n", Query (head, s)); return 0;}
Statistical Puzzles---hdu1251 dictionary tree templates