当前位置:编程学习 > C/C++ >>

HDU 1671 Phone List

Phone List
Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5784    Accepted Submission(s): 1988
 
 
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
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
 
 
Sample Output
NO
YES
 
 
Source
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)
 
 
Recommend
lcy
解题思路:新番字典树第一弹,主要是套模板,在建树过程中对每个号码的尾部进行标记,每输入一个号码分两种情况讨论,如果这个号码是之前输入的某个号码的前缀,或者这个号码的前缀是之前输入的某个号码,均不符合要求,输出NO,否则输出YES。注意每组判断输出完毕后释放这个Trie树的空间,再重新构建树,否则会MLE。
 
 
[cpp]  
#include<cstdio>  
#include<cstring>  
#include<iostream>  
using namespace std;  
typedef struct TrieNode  
{  
    bool flag;  
    int c;  
    struct TrieNode *next[10];  
    TrieNode()  
    {  
        c=0;  
        flag=false;  
        memset(next,0,sizeof(next));  
    }  
};  
char str[10];  
TrieNode *root=NULL;  
bool CreatTree(char s[],int k)  
{  
    int i,len;  
    bool flag=true;  
    len=strlen(s);  
    TrieNode *p=root;  
    TrieNode *temp;  
    for(i=0;i<len;i++)  
    {  
        if(p->flag)  
            //如果新输入的号码是之前某个号码的前缀  
            {  
                flag=false;  
                break;  
            }  
        if(p->next[s[i]-'0']==NULL)  
        {  
            temp=new TrieNode;  
            p->next[s[i]-'0']=temp;  
        }  
        p=p->next[s[i]-'0'];  
        p->c++;//对号码沿Trie树走过的路径进行标记  
    }  
    if(!flag)  
        return false;  
    else if(p->c>1&&k)//大于1的话就说明已经有以此路径上的数字串作为前缀  
        return false;  
    else  
    {  
        p->flag=true;//标记号码尾部  
        return true;  
    }  
}  
void Delete(TrieNode *node)  
    //递归释放Trie树每一层的空间  
{  
    int i;  
    for(i=0;i<10;i++)  
    {  
        if(node->next[i])  
            Delete(node->next[i]);  
        delete node->next[i];  
        node->next[i]=0;  
    }  
}  
int main()  
{  
    int i,j,n,t;  
    bool flag;  
    scanf("%d",&t);  
    while(t--)  
    {  
        root=new TrieNode;  
        flag=false;  
        scanf("%d",&n);  
        memset(str,'\0',sizeof(str));  
        for(i=0;i<n;i++)  
        {   www.zzzyk.com
            scanf("%s",&str);  
            if(flag)  
                continue;  
            if(!CreatTree(str,i))  
                flag=true;  
            memset(str,'\0',sizeof(str));  
        }  
        if(flag)  
            printf("NO\n");  
        else  
            printf("YES\n");  
        Delete(root);  
    }  
    return 0;  
}  
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,