Poj 3630 phone list (Dictionary tree + static dictionary tree)

Source: Internet
Author: User
Phone list
Time limit:1000 ms   Memory limit:65536 K
Total submissions:22235   Accepted:6885

Description

Given 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:

  • Emergency 911
  • Alice 97, 625, 999
  • 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.

Input

The first line of input gives a single integer, 1 ≤T≤ 40, the number of test cases. Each test case startsN, The number of phone numbers, on a separate line, 1 ≤N≤ 10000. Then followsNLines with one unique phone number on each line. A phone number is a sequence of at most ten digits.

Output

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

Sample Input

2391197625999911254265113123401234401234598346

Sample output

NOYES

Source

Nordic 2007

A classic topic in the dictionary tree. After so many things have been done to the dictionary tree, the idea of the dictionary tree should be clear. I started to see this question, my idea is to first build a dictionary tree, add a flag at the end, and judge whether there is a sign in the middle of the dictionary tree. Later I found that this idea could not be done. Later I changed my mind; there are two cases for this question. The first is to enter a long string and then a short string. If this is the case, if there are no new nodes in this process, the shorter string is a prefix of the long string. In the second case, the shorter string is entered first, if a long string is input and the end mark of the shorter string is read in this case, it indicates that the shorter string is a prefix of the longer string, in other cases, it is not a public prefix;

This problem exists on many OJ instances. You can use the dictionary tree directly on hduoj and nyoj, and use static Arrays on poj, the space allocated with malloc is time-consuming;

The following is the code that directly uses the dictionary to build water;

# Include <cstdlib> # include <cstdio> # include <cstring> typedef struct node // The struct {int count; // count, mark the end of struct node * Next [10];} node; node * newnode () // create a new node {node * q; q = (node *) malloc (sizeof (node); q-> COUNT = 0; For (INT I = 0; I <10; I ++) q-> next [I] = NULL; return Q;} int build (node * t, char * s) // create a dictionary tree {node * q; q = T; int I, K, Tag, Len; len = strlen (s); For (TAG = I = 0; I <Len; I ++) {k = s [I]-'0 '; if (Q-> next [k] = NULL ){ Q-> next [k] = newnode (); tag = 1; // new node generated} q = Q-> next [k]; If (Q-> count) // return 0;} Q-> COUNT = 1; // If (! Tag) return 0; return 1;} int del (node * P) // release the dictionary tree space; otherwise, the occupied space is too large {If (P = NULL) return 0; for (INT I = 0; I <10; I ++) if (p-> next [I]) del (p-> next [I]); free (p); Return 0;} int main () {// freopen ("1.txt"," r ", stdin); node * t; int n, m, flag; char s [12]; scanf ("% d", & N); While (n --) {T = (node *) malloc (sizeof (node )); // assign the header node T-> COUNT = 0; For (INT I = 0; I <10; I ++) T-> next [I] = NULL; flag = 1; scanf ("% d", & M); For (INT I = 0; I <m; I ++) {s CANF ("% s", S); If (FLAG) Flag = build (t, s);} del (t); If (! Flag) printf ("NO \ n"); else printf ("Yes \ n");} return 0 ;}
When I started to write data in the dictionary tree, I made a very bad mistake. I put my node out of the loop, and then I submitted it always wa. After a long time, I couldn't find any errors, in addition, an exception occurred when I put the del function in that position. Later, I realized that a set of data is equivalent to creating a dictionary tree; this error should not be made!

The following code is written using static arrays. For the first time, the dictionary tree is written using static arrays and others' code is referenced;

# Include <cstdlib> # include <cstdio> # include <cstring> typedef struct node // The struct {bool end; // The ending sign of int A [10];} node; node phonelist [100000]; int X; int flag; void Init () {flag = 1; // flag x = 0; // initial position; for (INT I = 0; I <100000; I ++) {phonelist [I]. end = false; For (Int J = 0; j <10; j ++) phonelist [I]. A [J] =-1 ;}} int build (char * s) // create a dictionary tree {int K, now = 0, tag = 0; // initial position int Len = strlen (s); For (INT I = 0; I <Len; I ++) {k = s [I]-'0'; I F (phonelist [now]. A [k] =-1) {tag = 1; // enter a new location phonelist [now]. A [k] = ++ X; // assign now = X to the array; // next position} else {now = phonelist [now]. A [k]; If (phonelist [now]. end) return 0; // end sign of a word} phonelist [now]. end = true; // If (! Tag) return 0; return 1;} int main () {// freopen ("1.txt"," r ", stdin); int n, m; char s [12]; scanf ("% d", & N); While (n --) {Init (); scanf ("% d", & M); For (INT I = 0; I <m; I ++) {scanf ("% s", S); If (FLAG) Flag = build (s) ;}if (FLAG) printf ("Yes \ n"); else printf ("NO \ n");} return 0 ;}

The general idea is the same, but the writing method is different. I also made a mistake here. I began to define the arrays in the struct into the struct type, I have not found any errors for a long time. It has been a waste of time. To reduce the number of such low-level errors, the most important thing about algorithms is thinking! Train your thinking skills !!

This question can also be done in another way, and they can be sorted and compared directly using the Quick Sort;

The following code is written by someone else. For more information, see;

#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;char phone[10000][11];int cmp(const void* a,const void* b){    return strcmp((char*)a,(char*)b);}int main(void){    int t;    scanf("%d",&t);    while(t--)    {        int i,n;        bool flag=false;        scanf("%d",&n);        for(i=0;i<n;i++)        {            scanf("%s",phone[i]);        }        qsort(phone,n,sizeof(phone[0]),cmp);        for(i=0;i<n-1;i++)        {            if(strncmp(phone[i],phone[i+1],strlen(phone[i]))==0)            {                flag=true;                break;            }        }        if(flag==false)            printf("YES\n");        else            printf("NO\n");    }    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.