Description
We know that if a phone number A is another phone number B’s prefix, B is not able to be called. For an example, A is 123 while B is 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 two numbers A and B that A is B’s prefix.
Input
The input consists of several test cases. The first line of input in 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 is followed by a line containing one zero.
Output
For each test case, if there exits a phone number that cannot be called, print “NO”, otherwise print “YES” instead.
Sample Input
2 012 012345 2 12 012345 0
Sample Output
NO YES
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef struct Node
{
Node *child[26];
int n;
}Node;
int flag;
void Insert (Node *root, char *str)
{
int len = strlen (str);
Node *ans = root;
for (int i =0; i < len; i++)
{
if (ans->child[str[i]-'0'] == NULL)
{
Node *NewNode = (Node*) malloc (sizeof (Node));
NewNode->n = 1;
for (int j = 0;j<26;j++) NewNode->child[j] = NULL;
ans->child[str[i]-'0'] = NewNode;
ans = NewNode;
}
else
{
ans = ans->child[str[i]-'0'];
ans->n++;
if(ans->n>1)
flag=1;
}
}
}
int main ()
{
int t;
char s[10010];
while(scanf("%d",&t)!=EOF)
{
getchar();
if(t==0)
break;
flag=0;
Node *root=(Node*)malloc(sizeof(Node));
root->n=0;
for(int i=0;i<26;i++)
root->child[i] = NULL;
for(int i=0;i<t;i++)
{
gets(s);
Insert(root,s);
}
if(!flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}