Phone List

来源:互联网 发布:2017java 笔试题 编辑:程序博客网 时间:2024/06/06 02:27
Given a list of phone numbers, determine if it is consistent in the sense that 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 call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.


Input
The first line of input gives a single integer, 1 ≤ t ≤ 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.


Output
For each test case, output "YES" if the list is consistent, or "NO" otherwise.


Sample Input
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
Sample Output
NO
YES

字典树问题

#include<cstdio>#include<cstring>#include<iostream> using namespace std;struct node{    node *next[10];    int end;    node(){  //构造函数,方便初始化数据         memset(next,NULL,sizeof(next));        end=0;  //end=0表示一般节点,end=1标志一个电话号的结束     }};node *root;bool insert(char *s){    int i,k,flag;    node *p=root;    for(flag=i=0;s[i];++i){        k=s[i]-'0';        if(p->next[k]==NULL){            p->next[k]=new node();            flag=1;   //标记建立过一个新节点         }         p=p->next[k];        if(p->end) return 0; //碰到结束标志,则返回0     }    p->end=1;  //一个电话号的结束标志     if(!flag) return 0;//字符串插入完毕未出现新建节点的操作,返回0     return 1;}int del(node *p)  //释放字典树空间,否则占用空间太大 {      if(p==NULL)    return 0;    for(int i=0;i<10;i++)    if(p->next[i])del(p->next[i]);    delete p;    return 0;} int main(){    int n,T;    char s[11];    scanf("%d",&T);    while(T--){        bool flag=1;        root=new node();        scanf("%d",&n);        while(n--){            scanf("%s",s);            if(flag) flag=insert(s);//假如出现过混乱情况,则不再进行插入操作         }        del(root);        if(flag) puts("YES");        else puts("NO");    }    return 0;}
上面这个代码有时在一些评测机上会超时

#include<stdio.h>#include<string.h>typedef struct node{    int num;    //标记该字符是否是某一字符串的结尾    struct node *next[10];}node;node memory[1000000];int k;int insert(char *s,node *T){    int i,len,id,j;    node *p,*q;    p=T;    len=strlen(s);    for(i=0;i<len;++i)    {        id=s[i]-'0';        if(p->num==1)   //说明存在先前字符可以作为s的前缀----(先短后长)            return 1;        if(p->next[id]==NULL)        {            q=&memory[k++];            q->num=0;            for(j=0;j<10;++j)                q->next[j]=NULL;            p->next[id]=q;        }        p=p->next[id];    }    for(i=0;i<10;++i)      //如果p的后继结点不为空的话说明s时先前字符的前缀----(先长后短)        if(p->next[i]!=NULL)            return 1;    p->num=1;    return 0;}int main(){    int m,n,flag,i;    node *T;    char s[15];    scanf("%d",&m);    while(m--)    {        k=0;          //每次都从数组下标为0的地方开始分配内存,可以使内存循环利用,从而不会造成内存超限        T=&memory[k++];        T->num=0;        for(i=0;i<10;++i)            T->next[i]=NULL;        flag=0;        scanf("%d",&n);        while(n--)        {            scanf("%s",s);            if(flag)                continue;            if(insert(s,T))                flag=1;        }        if(flag)            printf("NO\n");        else            printf("YES\n");    }    return 0;}



0 0
原创粉丝点击