HDU 1251 statistics

Source: Internet
Author: User
His sister's, knocked out, the computer crashed, all disappeared, and again ,... What is the pace?
# Include <stdio. h> # include <string. h> # include <stdlib. h> # define zero 0 # define alph_len 26/* 26 letters */const char first_char = 'a'; typedef struct node {struct node * child [alph_len]; /* store the next character */int n;/* record the number of times the current word appears */} node, * node; node root; /* The root node of the dictionary tree (no characters are stored) * // * Insert the word */void insert (char * Str) {int I, index, Len; node current = NULL, newnode = NULL; Len = strlen (STR); current = root;/* the current node is Root Node */for (I = 0; I <Len; I ++)/* Insert characters one by one */{Index = STR [I]-first_char; /* obtain the subscript of this character */If (current-> child [Index]! = NULL)/* The character is already in the dictionary tree */{current = Current-> child [Index];/* modify the current node location */(current-> N) + +;/* The current word appears again. If the character */} else/* has not appeared, the node */{newnode = (node) calloc (1, sizeof (node);/* Add a node and initialize */current-> child [Index] = newnode; current = newnode; /* modify the current node location */current-> n = 1; /* this new word appears */}/* search for the word */INT find_word (char * Str) {int I, index, Len; node current = NULL; Len = strlen (Str ); Current = root;/* Search Start from the root node */for (I = 0; I <Len; I ++) {Index = STR [I]-first_char; /* obtain the subscript of this character */If (current-> child [Index]! = NULL)/* The current character exists in the dictionary tree */{current = Current-> child [Index];/* modify the location of the current node */} else {return zero; /* the word does not match after comparison. The dictionary tree does not contain the word */} Return Current-> N; /* Number of times this word appears */} int main () {char TMP [11]; int I; root = (node) calloc (1, sizeof (node )); while (gets (TMP), strcmp (TMP ,"")! = Zero) {insert (TMP);} while (scanf ("% s", TMP )! = EOF) {I = find_word (TMP); printf ("% d \ n", I);} return 0 ;}

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.