Phone List hdu 1671

来源:互联网 发布:黑马程序员有用吗 编辑:程序博客网 时间:2024/05/17 06:38

Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7349    Accepted Submission(s): 2527


Problem Description
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:
1. Emergency 911
2. Alice 97 625 999
3. 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
2391197625999911254265113123401234401234598346
 

Sample Output
NOYES
 

Source
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)
 

Recommend
lcy
 
 
 
可以直接带入模板去求,学习字典树的方法我个人认为就是不断的照着模板去推,逐渐熟悉运算过程,才可以灵活变化
 
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define MAXNUM 10using namespace std;//定义字典树typedef struct Trie{    bool flag;//从根到此是否为一个单词    Trie *next[MAXNUM];}Trie;Trie *root;void init(){    root = (Trie *)malloc(sizeof(Trie));    root->flag=false;    for(int i=0;i<MAXNUM;i++)    root->next[i]=NULL;}void insert(char *word){    Trie *tem = root;    while(*word!='\0')    {        if(tem->next[*word-'0']==NULL)        {            Trie *cur = (Trie *)malloc(sizeof(Trie));            for(int i=0;i<MAXNUM;i++)            cur->next[i]=NULL;            cur->flag=false;            tem->next[*word-'0']=cur;        }        tem = tem->next[*word-'0'];        word++;    }    tem->flag=true;}bool search(char *word){    Trie *tem = root;    for(int i=0;word[i]!='\0';i++)    {        if(tem==NULL||tem->next[word[i]-'0']==NULL)        return 0;        tem=tem->next[word[i]-'0'];    }    return tem->flag;}void del(Trie *cur){    for(int i=0;i<MAXNUM;i++)    {        if(cur->next[i]!=NULL)        del(cur->next[i]);    }    free(cur);}int main(){    int t,n;    bool flag;    char phone_num[10005][20];    char num[20];    scanf("%d",&t);    while(t--)    {        init();        flag = true;        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%s",phone_num[i]);            insert(phone_num[i]);        }        for(int i=0;i<n;i++)        {            int len = strlen(phone_num[i]);            memset(num,'\0',sizeof(num));            for(int j=0;j<len-1;j++)            {                num[j]=phone_num[i][j];                if(search(num))                {                    flag = false;                    break;                }            }        }        if(flag)printf("YES\n");        else printf("NO\n");        del(root);    }    return 0;}

 
原创粉丝点击