Phone List
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 26328 |
|
Accepted: 7938 |
Description
Given a list of phone numbers, determine if it is consistent in the sense this 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 would direct your-to-the-emergency line as soon as Y OU had dialled the first three digits of Bob ' s phone number. So the list would not being consistent.
Input
The first line of input gives a single integer, 1≤ t ≤40, the number of test cases. Each test case is starts with n, the number of the phone numbers, and on a separate line, 1≤ n ≤10000. Then follows n lines with a unique phone number on each line. A phone number is a sequence of at the most ten digits.
Output
For each test case, the output "YES" If the list is consistent, or "NO" otherwise.
Sample Input
2391197625999911254265113123401234401234598346
Sample Output
NOYES
Test instructions: Just ask whether there is a serial number in these numbers is the prefix of another string of numbers, a typical trie tree template problem;
AC Code:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>UsingNamespace Std;Constint N=100010;Char Str[N][10];int TNCnt, Trie[N][20],flag, Val[N];voidBuild_checktrie(Char*s,Int V){int Len=Strlen(s), U=0;For(int I=0; I<len; I++){int num=s[I]-' 0 ';If(!trie[u][num]){For(Int J=0; j<=10; j++){Trie[CNT][j]=0;} val[CNT]=0; Trie[u][num]=cnt++;}Else{If(I==len-1){Flag=0;}If(Val[Trie[u][num]]){Flag=0;}} u=trie[u][num];} val[u]=v;}IntMain(){scanf("%d", &t);While(t--){Memset(trie,0,sizeof(trie));Memset(Val,0,sizeof(Val)); Cnt=1;scanf("%d", &n);For(int I=1; I<=n; I++){scanf('%s 'Str[I]);} flag=1;For(int I=1; I<=n; I++){Build_checktrie(str[I],1if (!flag Break if (Flagprintf ( "YES \n "); else printf ( "No\n" ); return 0;< Span class= "Sh-cbracket" >
poj3630 Phone List (trie tree template title)