Phone Number

来源:互联网 发布:python 改变工作路径 编辑:程序博客网 时间:2024/05/21 11:16
A - Problem A :Phone Number
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Submit Status

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

20120123452120123450

Sample Output

NOYES

#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;}


0 0