hdu 1671 phone list Trie 树

本文介绍了一种算法,用于检查电话号码列表是否具有一致性,即任一号码都不是另一个号码的前缀。通过使用Trie树结构并按号码长度排序,确保了在插入过程中能够及时发现并阻止不一致的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

是否存在串是别的串的前缀,有的话输出no,否则 yes

那么长度从短到长排序

插入的时候发现有这样的前缀,直接return 输出no

#include <bits/stdc++.h>
using namespace std;

struct node
{
    char s[20];
    int len;
}no[10010];

const int MAXN = 1e5+10;
const int CASE_SIZE = 11;

struct Trie
{
    int child[MAXN][CASE_SIZE],value[MAXN],trieN,root;
    void init()
    {
        trieN=root=0;value[root]=0;
        memset(child[root],-1,sizeof(child[root]));
    }
    int newnode()
    {
        trieN++;value[trieN]=0;
        memset(child[trieN],-1,sizeof(child[trieN]));
        return trieN;
    }
    int insert(char *s)
    {
        int x=root;
        for(int i=0;s[i];i++)
        {
            int d=s[i]-'0';
            if(child[x][d]==-1)
                child[x][d]=newnode();
            x=child[x][d];
            if(value[x]) return 1;
        }
        value[x]=1;//这种是读完整串才能++
        return 0;
    }
    int search(char *s)
    {
        int sum=0,x=root;
        for(int i=0;s[i];i++)
        {
            int d=s[i]-'a';
            if(child[x][d]==-1)
                break;
            x=child[x][d];
            sum+=value[x];
        }
        return sum;
    }
}trie;



int cmp(node a,node b)
{
    return a.len<b.len;
}


int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        trie.init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",no[i].s);
            no[i].len=strlen(no[i].s);
        }
        sort(no+1,no+n+1,cmp);
        int flag=0;
        for(int i=1;i<=n;i++)
        {

            if(trie.insert(no[i].s))
            {
                puts("NO");
                flag=1;
                break;
            }
        }
        if(!flag) puts("YES");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值