SETI is receiving some weird signals for the past few days. After converting them to our number system they found out that some numbers are repeating. Due to travelling millions of miles signal gets distorted. Now they asked you check the consistency of their data sets. The consistency is that, no number is the prefix another number in that data set. Let's consider a data set:
1. 123
2. 5123456
3. 123456
In this data set, numbers not consistent, because the first number is the prefix of the third one.
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case starts with an integer n (1 ≤ n ≤ 10000) denoting the total numbers in their list. Each of the next n lines contains one unique phone number. A number is a sequence of digits and has length from 1 to 10.
input:
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
output:
Case 1: NO
Case 2: YES
#include<bits/stdc++.h>
using namespace std;
long long n, m, t, cs=1, i, j, last=1, xx, cur, k, x, fl;
string s;
struct trie
{
bool endmark;
int next[11];
} r[100009];
void new_trie(int cur)
{
for(int zz=0; zz<10; zz++)
r[cur].next[zz]=-1;
r[cur].endmark=false;
}
void insrt()
{
cur=0, k=s.size();
for(int z=0; z<k; z++)
{
x=(int)s[z]-48;
if(r[cur].next[x]==-1)
{
r[cur].next[x]=last;
new_trie(last++);
cur=r[cur].next[x];
}
else
{
cur=r[cur].next[x];
if(r[cur].endmark==true)
fl=1;
if(z==k-1)
fl=1;
}
if(fl==1)
break;
}
r[cur].endmark=true;
}
int main()
{
cin>>t;
while(t--)
{
cin>>n;
fl=0, last=1;
new_trie(0);
for(i=0; i<n; i++)
{
cin>>s;
insrt();
}
cout<<"Case "<<cs++<<": ";
if(fl==1)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
return 0;
}