poj3630/hdu1671 Phone List

本文介绍了一种通过构建字典树来判断电话号码簿中是否存在号码前缀冲突的方法。使用两种不同的字典树实现方式——指针建树与数组建树进行解决。文章通过具体的代码示例展示了如何有效地避免电话号码之间的冲突。

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

                                                                     Phone List

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

想法:暑假回炉重造第一天,无限TLE and MLE,菜到不行。简单的字典树模板题,一开始以为是hdu的题,动态指针建树后忘记销毁指针一直MLE;后面改了一直TLE,才发现是poj的3630,也是醉了。要用数组来建树才行。不会这玩意只能百度去学了,真的是菜。

CODE:

hdu1671版(指针建树)

#include<stdio.h>
#include<string.h>
struct trie{
        trie *next[10];
        int v;
        trie(){
                v=1;
                for(int i=0;i<10;i++) next[i]=NULL;
        }
}*root;
void create_trie(char *str){
        int len=strlen(str);
        trie *p=root,*q;
        for(int i=0;i<len;i++){
                int id=str[i]-'0';
                if(p->next[id]==NULL){
                        q=new trie;
                        p->next[id]=q;
                        p=p->next[id];
                }
                else{
                        p->next[id]->v++;
                        p=p->next[id];
                }
        }
        p->v=-1;
}
int find_trie(char *s){
        int len=strlen(s);
        trie *q=root;
        for(int i=0;i<len;i++){
                int id=s[i]-'0';
                q=q->next[id];
                if(q==NULL) return 0;
                if(q->v==-1) return -1;
        }
        return -1;
}
void delete_trie(trie* q){
        if(q==NULL) return ;
        for(int i=0;i<10;i++) delete_trie(q->next[i]);
        delete q;
}
int main()
{
        int t,n,i;
        char s[15];
        scanf("%d",&t);
        while(t--){
                scanf("%d",&n);
                root=new trie;
                int flag=0;
                for(i=0;i<n;i++){
                        scanf("%s",s);
                        if(flag) continue;
                        if(find_trie(s)==-1) flag=1;
                        if(flag) continue;
                        create_trie(s);
                }
                if(flag) puts("NO");
                else puts("YES");
                delete_trie(root);
        }
}

poj3630(数组建树)

#include<cstring>
#include<cstdio>
using namespace std;
const int N=1e4+5;
char a[N][11];
struct node
{
    bool flag;
    node *next[11];
    node(){
        flag=0;
        memset(next,0,sizeof(next));
    }
}trie[100000];
int num;
void Insert(node *root,char *s)
{
    int i=0;
    node *p=root;
    while(s[i]){
        int k=s[i++]-'0';
        if(p->next[k]==NULL) p->next[k]=&trie[num++];
        p=p->next[k];
    }
    p->flag=1;
}
bool Search(node *root,char *s)
{
    int i=0;
    node *p=root;
    while(s[i]){
        int k=s[i++]-'0';
        p=p->next[k];
        if(p->flag&&s[i]) return 1;
    }
    return 0;
}
int main()
{
    int t,n,i;
    scanf("%d",&t);
    while(t--){
        memset(trie,0,sizeof(trie));
        node *root=&trie[0];
        num=1;
        scanf("%d",&n);
        for(i=0;i<n;i++){
            scanf("%s",a[i]);
            Insert(root,a[i]);
        }
        int flag=0;
        for(i=0;i<n;i++){
            if(Search(root,a[i])){
                flag=1;
                break;
            }
        }
        if(!flag) puts("YES");
        else puts("NO");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值