山东省第一届ACM大学生程序设计竞赛 Phone Number 字典树

本文介绍了一种使用字典树解决电话号码前缀检查问题的方法,并提供了两种实现方案:一种利用字典树进行高效查找,另一种采用简单的暴力匹配方法。通过对输入电话号码的排序和比较,确保了没有一个号码是另一个号码的前缀。

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

Phone Number

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

We know that if a phone number A is another phone number B’s prefix, B is not able to be called. For an example, A is 123 while B is 12345, after pressing 123, we call A, and not able to call B.
Given N phone numbers, your task is to find whether there exits two numbers A and B that A is B’s prefix.

输入

 The input consists of several test cases.
 The first line of input in each test case contains one integerN (0<N<1001), represent the number of phone numbers.
 The next line containsN integers, describing the phone numbers.
 The last case is followed by a line containing one zero.

输出

 For each test case, if there exits a phone number that cannot be called, print “NO”, otherwise print “YES” instead.

示例输入

2
012
012345
2
12
012345
0

示例输出

NO
YES

提示

 

来源

 2010年山东省第一届ACM大学生程序设计竞赛


水题字典树,判断是否有前缀号码出现

ACcode:

#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define MAX 10
#define maxn 1005
using namespace std;
struct Trie{
    Trie* next[MAX];
    int v;
};
struct STR{
    char str[maxn];
    int len;
    bool operator<(const STR &a)const{
        return (this->len<a.len);
    }
}my[maxn];
Trie *root;
int n,ans,num;
void createTire(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=(Trie *)malloc(sizeof(Trie));
            q->v=1;
            for(int j=0;j<MAX;++j)
                q->next[j]=NULL;
            p->next[id]=q;
            p=p->next[id];
        }
        else{
            p->next[id]->v++;
            p=p->next[id];
        }
    }
    p->v=-1;
}
int findTrie(char *str){
    int len=strlen(str);
    Trie *p=root;
    for(int i=0;i<len;++i){
        int id=str[i]-'0';
        p=p->next[id];
        if(p==NULL)
            return 0;
        if(p->v==-1)
            return 0;
    }
    return 2;
}
void init(){
    root=(Trie *)malloc(sizeof(Trie));
    for(int i=0;i<MAX;++i)
        root->next[i]=NULL;
    ans=num=0;
    memset(my,0,sizeof(my));
}
int main(){
    while(~scanf("%d",&n)&&n){
        init();
        for(int i=0;i<n;++i){
            scanf("%s",my[i].str);
            my[i].len=strlen(my[i].str);
        }
        sort(my,my+n);
        for(int i=0;i<n;++i)createTire(my[i].str);
     //   for(int i=0;i<n;i++)cout<<my[i].str<<'\12';
        for(int i=0;i<n;++i)
            ans+=findTrie(my[i].str);
            //cout<<"ans: "<<ans<<'\12';
        for(int i=0;i<n&&!ans;++i)
            for(int j=i+1;j<n;++j)
                if(!strcmp(my[i].str,my[j].str))
                    ans++;
        printf(ans>0?"NO\n":"YES\n");
    }
    return 0;
}
/*
2
012
012345
2
12
012345
0
*/

暴力做法

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#define maxn 1005
#define inf 0x7fffffffff
#define ll long long
using namespace std;
int n,m,loop;
struct N {
    char s[maxn];
    int len;
}my[maxn];
bool cmp(N a,N b){return a.len<b.len;}
int len[maxn];
int main(){
    while(scanf("%d",&n)&&n){
        for(int i=1;i<=n;++i){
            scanf("%s",my[i].s);
            my[i].len=strlen(my[i].s);
        }
        sort(my+1,my+1+n,cmp);
        bool flag=true;
        for(int i=1;flag&&i<=n;++i)
            for(int j=i+1;flag&&j<=n;++j){
                int ok=0;
                for(int z=0;z<my[i].len;++z)
                        if(my[i].s[z]==my[j].s[z])ok++;
                if(ok==my[i].len)flag=false;
            }
        if(flag)puts("YES");
        else puts("NO");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值