http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2151
Phone number Time limit:1000ms Memory limit:65536k have questions? Dot here ^_^ Title DescriptionWe know that if a phone number A is another phone number B ' s prefix, and B is not able to be called. For the example, A is 123 while B was 12345, after pressing 123, we call A, and not able to call B.
Given N phone numbers, your task is to find whether there exits, numbers a and b that's a is B ' s prefix.
InputThe input consists of several test cases.
The first line of input on each test case contains one integer
n (0<
n<1001), represent the Number of phone numbers.
The next line contains
N integers, describing the phone numbers.
The last case was followed by a line containing one zero. OutputFor each test case, if there exits a phone number that cannot be called, print "NO", otherwise print "YES" instead.Sample input
20120123452120123450
Sample output
NOYES
Hint Source 2010 The first ACM University student Program design contest in Shandong Province sample program
AC Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5 using namespacestd;6 7 structnode8 {9 intcount;TenNode *child[Ten]; One node () A { -Count=0; - inti; the for(i=0; i<Ten; i++) -child[i]=0; - } - }; + -Node *current, *NewNode; + A voidInsert (node *root,Char*ss) at { - //printf ("%s*****\n", SS); - intI, M; -Current=Root; - for(i=0; I<strlen (ss); i++) - { inm=ss[i]-'0'; - if(current->child[m]!=NULL) to { +Current=current->Child[m]; -+ + (current->count); the } * Else $ {Panax NotoginsengNewnode=Newnode; -+ + (newnode->count); thecurrent->child[m]=NewNode; +Current=NewNode; A } the } + } - $ intSearch (Node *root,Char*ss) $ { - //printf ("%s*****\n", SS); - intI, M; theCurrent=Root; - for(i=0; I<strlen (ss); i++)Wuyi { them=ss[i]-'0'; - if(current->child[m]==NULL) Wu return 0; -Current=current->Child[m]; About } $ returnCurrent->count; - } - - intMain () A { + Charstr[ -], s[1010][ -]; the intT, flag, I; - while(SCANF ("%d",&t)) $ { the if(t==0) Break; theflag=0; theNode *root=Newnode; the for(i=0; i<t; i++) - { inscanf"%s", str); the strcpy (s[i], str); the //puts (s[i]); About Insert (root, str); the } the for(i=0; i<t; i++) the { + if(Search (root, s[i])-1) - { theflag=1;Bayi Break; the } the } - if(flag==0) printf ("yes\n"); - Elseprintf"no\n"); the } the return 0; the}
View Code
Sdutoj 2151 Phone Number