POJ【3630】Phone List

来源:互联网 发布:java urlencode 中文 编辑:程序博客网 时间:2024/05/19 03:25
Phone List
Time Limit: 1000MS        Memory Limit: 65536K
Total Submissions: 26462        Accepted: 7976

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:

    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



题意即给定n个字符串,如果任意一个是另一个的前缀则输出NO,否则输出YES

可以用字典树实现,很明显只有短的字符串可能是长的字符串的前缀,如果我们把所有字符串从短到长排序,然后依次加入字典树中,并且把最后一个字母的flag标记为1,那么如果下面的字符串经过flag=1的点,说明满足“任意一个是另一个的前缀”这一条件,反之如果最后还是没有到达,那么不满足,代码如下:


#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct node{
    int chi[26];
    void init()//清零函数
    {
        for (int w=0;w<=25;w++)
        chi[w]=0;
    }
};//给字典树定义的结构体,chi【26】代表每个父亲节点下的子节点,因为有多组数据,必须更新,init用于更新
struct forstr{int l;char s[20];};//用于给字符串排序
forstr qq[10004];
node tre[100000];//树
int flag[100004];
int tot=0;//tot是编号
int cmp(forstr aa,forstr bb)
{
return aa.l<bb.l;
}
int add(char s[])
{
    int now=0;
    int l=strlen(s);
    for (int i=0;i<l;i++)
    {
        int tmp=s[i]-'0';
        if (tre[now].chi[tmp]!=0)//如果下一个点已经编号
        {
            if (flag[tre[now].chi[tmp]]!=0)//如果下一个节点已经编号并且flag为1,说明满足条件
            {
                return 1;
            }
            if (i==l-1)     flag[tre[now].chi[tmp]]=1; //如果这个字符已经是最后,修改flag

            now=tre[now].chi[tmp];//更新到下一个点
            continue;
        }
        else //反之如果下一个点,没有编号,那么给下一个点编号
        {
            tot++;
            tre[now].chi[tmp]=tot;
            flag[tot]=0;
            tre[tot].init();//多组数据下必须更新,一组数据不用
            now=tre[now].chi[tmp];
            if (i==l-1)     flag[tot]=1;//如果这个字符已经是最后,修改flag
        }
    }
    //cout<<flag[1]<<flag[2]<<flag[3]<<endl;
    return 0;
}






int main()
{
    int n;
    cin>>n;
    int m;
    for (int i=1;i<=n;i++)
        {
        cin>>m;
        for (int j=1;j<=m;j++)
            {
            scanf("%s",&qq[j].s);//读入必须用scanf,否则TLX
                    //PS:日后还要多用char数组啊,string改char,差点没气了
            qq[j].l=strlen(qq[j].s);//这个函数很慢,而且是在循环里,其实也可以不排序直接建树,不影响
            }
        tot=0;
        memset(flag,0,sizeof(flag));

                tre[0].init();//记得初始化,特别是这一句

        sort(qq+1,qq+1+m,cmp);
                int f=0;
        for (int j=1;j<=m;j++)
                     if (add(qq[j].s)==1)
                {
                cout<<"NO"<<endl;
                f=1;
                break;
                }
        if (f==1) continue;
        cout<<"YES"<<endl;//注意输出格式,T×D没有文末endl竟然presentation wrong(答案正确,输出错误),shit
    
        }
    return 0;
}


最后感谢同学DERIT的帮助,他的微博上有很多经典题目,http://blog.csdn.net/deritt/article/details/50041863,去看看吧


0 0
原创粉丝点击