HDU ---- (1671) phone list (trie with tags)

Source: Internet
Author: User
Tags define local
Phone list

Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 10837 accepted submission (s): 3735


Problem descriptiongiven a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers:
1. Emergency 911
2. Alice 97 625 999
3. Bob 91 12 54 26
In this case, it's not possible to call Bob, because the central wocould direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. so this list wocould not be consistent.

 

Inputthe first line of input gives a single integer, 1 <= T <= 40, the number of test cases. each test case starts with N, the number of phone numbers, on a separate line, 1 <=n <= 10000. then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.

 

Outputfor each test case, output "yes" if the list is consistent, or "no" otherwise.

 

Sample input239119762599991125417%3123401234401234598346

 

Sample outputnoyes

 

Source 2008 "insigma International Cup" Zhejiang Collegiate Programming Contest-warm up (3) simple dictionary tree: Code: Implement insert, query, delete
 1 /*hdu 1671 ×ÖµäÊ÷*/ 2 #define LOCAL 3 #include<cstdio> 4 #include<cstring> 5 #include<iostream> 6 using namespace std; 7 struct Trie 8 { 9    struct Trie *next[10];10    bool tail;11 };12 char str[10005][12];13 void in_Trie(char *s,Trie *root)14 {15    Trie *cur=root,*newcur;16   for(int i=0;s[i]!=‘\0‘;i++)17   {18        if(cur->next[s[i]-‘0‘]==NULL)19      {20          newcur=new Trie;   //(Trie*)malloc(sizeof(sizeof(Trie)));21          for(int j=0;j<=9;j++)22            newcur->next[j]=NULL;23             newcur->tail=0;24         cur->next[s[i]-‘0‘]=newcur;25      }26     cur=cur->next[s[i]-‘0‘];27   }28    cur->tail=1;29 }30 bool query(char *s,Trie *root)31 {32     int i=0;33     Trie *cur=root;34     for(i=0;s[i];i++){35        if(cur->tail==0&&cur->next[s[i]-‘0‘]!=NULL)36            cur=cur->next[s[i]-‘0‘];37       else break;38     }39  if(s[i]!=‘\0‘) return 1;40    return 0;41 }42 void dele(Trie *root)43 {44   for(int i=0 ; i<=9 ; i++ )45       if(root->next[i]!=NULL)46       dele(root->next[i]);47  // free(root);48  delete root;49 }50 int main()51 {52   #ifdef LOCAL53   freopen("test.in","r",stdin);54   #endif55   int t,i,n;56   Trie *root;57   scanf("%d",&t);58   while(t--)59   {60        root = new Trie ;61        for(int j=0;j<=9;j++)62        root->next[j]=NULL;63        root->tail=0;64 65      scanf("%d",&n);66     for(i=0;i<n;i++){67       scanf("%s",str[i]);68       in_Trie(str[i],root);69     }70 71     for(i=0;i<n;i++)72       if(query(str[i],root)!=0)73         break;74       dele(root);75       if(i>=n)76         printf("YES\n");77       else78         printf("NO\n");79   }80    return 0;81 }
View code

 

HDU ---- (1671) phone list (trie with tags)

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.