HDU 1671 phone list (Dictionary tree)

Source: Internet
Author: User

tag: blog OS Io AR for 2014 SP Log On

# Include <cstdio> # include <algorithm> # include <cstring> # include <iostream> # include <map> # define max 15 using namespace STD; typedef struct trie_node {bool flag; // whether the subtree struct trie_node * Next [Max];} trie; void insert (trie * root, char * Str) {trie * P = root; int Len = strlen (STR); For (INT I = 0; I <Len; I ++) {If (p-> next [STR [I]-'0'] = NULL) {trie * temp = (trie *) malloc (sizeof (trie )); for (Int J = 0; j <Max; j ++) temp-> Next [J] = NULL; temp-> flag = false; P-> next [STR [I]-'0'] = temp; P-> flag = true ;} P = p-> next [STR [I]-'0'];} bool find (trie * root, char * Str) {trie * P = root; int Len = strlen (STR); For (INT I = 0; I <Len; I ++) {If (p-> next [STR [I]-'0'] = NULL) return false; P = p-> next [STR [I]-'0'];} return p-> flag;} void del (trie * root) // release space {for (INT I = 0; I <Max; I ++) {If (root-> next [I]! = NULL) del (root-> next [I]);} Free (Root);} int main () {int t, n, I; char STR [10010] [15]; while (~ Scanf ("% d", & T) {While (t --) {scanf ("% d", & N); trie * root = (trie *) malloc (sizeof (trie); for (I = 0; I <Max; I ++) {root-> next [I] = NULL ;} root-> flag = false; for (I = 0; I <n; I ++) {scanf ("% s", STR [I]); insert (root, STR [I]) ;}for (I = 0; I <n; I ++) {If (find (root, STR [I]) break ;} if (I = N) printf ("Yes \ n"); else printf ("NO \ n"); del (Root) ;}} return 0 ;}

HDU 1671 phone list (Dictionary tree)

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.