POJ 3630 HDU 1671(字典树)

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 nlines 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

大致题意:给你一串电话号码,让你判断能不能打通所有的电话,第一组测试数据中的第一个电话号码是911,第三个电话号码输入到911是就会拨打出去,所以第三个电话号码就打不通了。


思路:这道题是关于字典树的应用,题目考查的并不多,但是这道题用动态分配内存会超时的,因为动态分配内存也需要时间,所以这时候我们就需要先把空间开好了,然后再模拟动态分配内存,同时,HDU上的1671和这道题一样,不过杭电上的可以用动态分配内存来做,不过也需要注意每一个循环输入过后都要把之前分配的内存清空,不然会超空间的。


具体代码如下:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

char str[12];
int n,m,cnt,cur;
struct TrieNode
{
    bool flag;
    int next[10];
    void init()
    {
        flag=false;
        memset(next,-1,sizeof(next));
    }
} t[100240]; // 先分配好空间

void in(char *s,int n)
{
    int p=0;
    for(int i=0; i<n; i++)
    {
        int x=s[i]-'0';
        if(t[p].next[x]==-1)
        {
            t[cur].init();
            t[p].next[x]=cur++;  // 模拟动态分配内存。
        }
        p=t[p].next[x];
        if(t[p].flag)   //判断当前这个号码是否被其他号码占用了。
        {
            cnt=0;
            return ;
        }
    }
    t[p].flag=true;
    for(int i=0; i<10; i++)  //判断当前号码的终点是否有其他的号码会用到。
        if(t[p].next[i]!=-1)
        {
            cnt=0;
            return ;
        }
    return ;
}

int main()
{
    int len;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&m);
        t[0].init();
        cnt=cur=1;
        while(m--)
        {
            scanf("%s",str);
            if(cnt&&str[0]!='\0')
            {
                len=strlen(str);
                in(str,len);
            }
        }
        if(cnt)
            printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


再附上在HDU上的AC代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

char str[12];
int n,m,cnt;

struct TrieNode
{
    int flag;
    TrieNode *next[10];
    TrieNode()
    {
        flag=0;
        for(int i=0; i<10; i++)
            next[i]=NULL;
    }
};
TrieNode *head;

void BuildTrie(char *s,int len)
{
    TrieNode *p=head;
    for(int i=0; s[i]!='\0'; i++)
    {
        int index=s[i]-'0';
        if(!p->next[index])
        {
            p->next[index]=new TrieNode;  // 动态分配内存。
        }
        p=p->next[index];
        if(p->flag)
        {
            cnt=0;
            return ;
        }
    }
    p->flag=1;
    for(int i=0; i<10; i++)
        if(p->next[i])
        {
            cnt=0;
            return ;
        }
    return ;
}

int deal(TrieNode *T)
{
    int i;
    for(i=0; i<10; i++)
    {
        if(T->next[i]!=NULL)
            deal(T->next[i]);  //需要用DFS来清空内存。
    }
    free(T);
    return 0;
}

int main()
{
    int len;
    scanf("%d",&n);
    while(n--)
    {
        head= new TrieNode;
        cnt=1;
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",str);
            if(cnt&&str[0]!='\0') // 有一个电话不能打通后就跳出判断,可以节省很多时间
            {
                len=strlen(str);
                BuildTrie(str,len);
            } 
        }
        deal(head);
        if(cnt)
            printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}



  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值