"Hdoj" 1251 statistical puzzle "Data structure-trie tree bare topic"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.