Portal: Statistical Puzzles
Test instructions
The dictionary tree is a bare question.
Analysis
The dictionary tree board, but this problem needs to be noted.
About the dictionary tree can only refer to Hihocoder Hiho a second week
Use g++ to commit the memory Limit exceeded, which can be AC with C + + submission.
The difference between g++ and C + + submissions
Reference: The differences between g++ and C + + in Language options in OJ
C + + is a computer programming language, and g++ is a C + + compiler.
Choosing C + + means that you will use the most standard C + + compilation method, which is ANSI C + + compilation.
Choosing g++ means that you are using the most popular compilers in the GNU project (in fact, we are familiar with Code::Blocks's own compilers).
Similar to the choice of C and GCC, the former is the standard C compiler, the latter is compiled with GCC.
My AC Code
#include <iostream> #include <cstdlib> #include <cstring> #define REP (i,a,b) for (int i=a;i<=b;i++) using namespace Std;const int maxn=26;struct trienode{//The number of current node prefixes int prefix; trienode* NEXT[MAXN];}; typedef trienode TRIE;//Root does not represent any letter void insert (Trie *root,char *s) {trie* p=root; while (*s!= ') {if (p->next[*s-' a ']==null) {trie* temp=new Trie (); for (int i=0;i<maxn;i++) {temp->next[i]=null; } temp->prefix=1; p->next[*s-' a ']=temp; P=p->next[*s-' a ']; } else {p=p->next[*s-' a ']; p->prefix++; } s++; }}int count (Trie *root,char *pre) {trie* p=root; while (*pre!= ') {if (p->next[*pre-' a ']==null) return 0; else {p=p->next[*pre-' a ']; pre++; }} return p->prefix;} void del (TRie *root) {for (int i=0;i<maxn;i++) {if (root->next[i]!=null) del (root->next[i]); } free (root);} int main () {char s[16]; Trie *root=new Trie (); for (int i=0;i<maxn;i++) root->next[i]=null; root->prefix=0; while (1) {gets (s); if (strcmp (S, "") ==0) {break; } insert (Root,s); } while (gets (s)) {printf ("%d\n", Count (root,s)); } del (root); return 0;}
"Hdoj" 1251 statistical puzzle "Data structure-trie tree nude"