zoukankan      html  css  js  c++  java
  • HDU 1671 Phone List (字典树)

    Phone List

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    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:
    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”。
    分析:用字典树,边插入边检查就行了。
    #include<stdio.h>
    #include<string.h>
    
    const int MAXN = 10;
    const int MAXM = 1000000;
    typedef struct TrieNode
    {
        bool flag;
        struct TrieNode *next[MAXN];
    }TrieNode;
    
    TrieNode Memory[MAXM];
    int allocp=0;
    bool flag;
    
    void InitTrieRoot(TrieNode **pRoot)
    {
        *pRoot=NULL;
    }
    TrieNode *CreateTrieNode()
    {
        TrieNode *p;
        p=&Memory[allocp++];
        p->flag=false;
        for(int i=0;i<MAXN;i++)
            p->next[i]=NULL;
        return p;
    }
    void InsertTrie(TrieNode **pRoot,char *s)
    {
        TrieNode *p;
        if(!(p=*pRoot)) p=*pRoot=CreateTrieNode();
        int i=0,k;
        while(s[i])
        {
            k=s[i++]-'0';
            if(p->next[k])
            {
                if(p->next[k]->flag||s[i]=='')
                {
                    flag=false;
                    return;
                }
            }
            else p->next[k]=CreateTrieNode();
            p=p->next[k];
        }
        p->flag=true;
    }
    char str[50];
    
    int main()
    {
        int T,n;
        TrieNode *Root=NULL;
        scanf("%d",&T);
        while(T--)
        {
            flag=true;
            allocp=0;
            scanf("%d",&n);
            InitTrieRoot(&Root);
            for(int i=1;i<=n;i++)
            {
                scanf("%s",str);
                if(flag) InsertTrie(&Root,str);
            }
            if(flag) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
  • 相关阅读:
    MD5算法--网盘秒传
    无线网络定位算法综述
    android学习---异步任务(AsyncTask)
    python中局部变量的定义
    python3里函数怎么样使用元组或字典作为参数调用(复制他人博客)
    调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之一
    Linux入门之——安装虚拟机软件
    Linux学习方法之以始为终—Linux工作分类
    Linux基础系列—Linux内核源码目录结构
    Linux基础系列—Linux体系结构和Linux内核结构
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5738414.html
Copyright © 2011-2022 走看看