HDU 1671 phone list

Source: Internet
Author: User
# Include <stdio. h> # include <string. h> # include <stdlib. h> # define zero 0 const int first_char = '0'; char num [11111] [20]; typedef struct node {struct node * child [20]; /* 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;/* start with the 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 this word */} If (current-> N)> 1) return 1; return 0 ;} void release (node root) {int I; If (null = root) {return ;}for (I = 0; I <20; I ++) {If (root-> child [I]! = NULL) {release (root-> child [I]) ;}} free (Root); root = NULL;} int main () {int I, n, m, check; scanf ("% d", & N); While (n --) {scanf ("% d", & M); root = (node) calloc (1, sizeof (node); // n times wrong for (I = 0; I <m; I ++) {scanf ("% s", num [I]); insert (Num [I]);} Check = 0; for (I = 0; I <m; I ++) {If (find_word (Num [I]) {check = 1; break ;}}if (check = 1) printf ("NO \ n"); If (check = 0) printf ("Yes \ n"); release (Root);} 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.