Phone Number

来源:互联网 发布:淘宝网人气女模特 编辑:程序博客网 时间:2024/05/21 11:23

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[10];    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<10;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(){    //freopen("a.txt","r",stdin);    char s[1010];    int n;    while(scanf("%d\n",&n),n)    {        flag=0;        Node *root=(Node*)malloc (sizeof (Node));        root->n = 0;        int i;        for (i = 0; i < 10; i++) root->child[i] = NULL;        for(i=0;i<n;i++)        {            gets(s);            Insert(root,s);        }        if(!flag) printf("YES\n");        else printf("NO\n");    }    return 0;}

0 0
原创粉丝点击