Poj4089: phone number

Source: Internet
Author: User
Total time limit: 1000 ms memory limit: 65536kb describes some phone numbers for you. Check whether they are consistent, that is, whether a phone number is the prefix of another phone number. For example, Emergency 911 Alice 97 625 999bob 91 12 54 26 in this example, we cannot call Bob because emergency's phone is its prefix, when Bob is called, emergency is connected first, so these phone numbers are inconsistent. The first line of the input is an integer T, 1 ≤ T ≤ 40, indicating the number of test data. The first line of each test example is an integer N, 1 ≤ n ≤ 10000, And the next n rows are a phone number of no more than 10 digits. For each test data output, if "yes" is consistent, if "no" is not output ". Sample input: 2391197625999911254265113123401234401234598346 sample output: Noyes

 

The idea is very simple, that is, the letter tree, also known as the trie tree. Then I started to write it silly... Naturally wa.

If you forget to pay attention to one thing, the normal trie tree only determines the prefix. This also determines the suffix.

That is, if the input order is:

11

110

No must be determined.

In addition, if the input order is:

110

11

It also needs to be determined that it is no.

It's easy to notice this.

View the Code directly:

 1 #include <iostream> 2 #include <stdio.h> 3  4 using namespace std; 5  6 struct P 7 { 8     //int i; 9     P* next[10];10     bool flag;11 }s;12 13 void init()14 {15     s.flag = false;16     //s.i = -1;17     for(int i = 0; i < 10; i ++)18         (s.next)[i] = NULL;19 }20 char a[11];21 bool add(int i,P* p)22 {23     if(p->flag == true)24         return false;25     if(a[i] == ‘\0‘)26     {27         p->flag = true;28         bool f = true;29         for(int j = 0; j < 10; j ++)30             if((p->next)[j] !=NULL)31                 f = false;32         return f;33     }34     int x = a[i]-‘0‘;35     if((p->next)[x] == NULL)36     {37         (p->next)[x] = new P();38         (p->next)[x]->flag = false;39         for(int j = 0; j < 10; j ++)40         {41             (((p->next)[x])->next)[j] = NULL;42         }43     }44     if(add(i+1,(p->next)[x]))45         return true;46     return false;47 }48 49 int main()50 {51     int t;52     scanf("%d",&t);53     while(t--)54     {55         init();56         int n;57         scanf("%d",&n);58         bool f=true;59         char c;60         scanf("%c",&c);61         while(n--)62         {63             cin.getline(a,11);64             if(!add(0,&s))65                 f = false;66         }67         if(f)68             cout<<"YES"<<endl;69         else70             cout<<"NO"<<endl;71     }72     return 0;73 }

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.