• 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;
    }
  • 相关阅读:
    转:秋补可选用四种素食
    转:请您千万不要这样刷牙
    下拉菜单DropDownList无法插入项 Items.Insert unavailable Fred
    上班族加班漫画【转】 Fred
    jstree onselect回调方法,获取选中节点的值 Fred
    SQL Server 2005 Express 远程访问设置【转】 Fred
    提示ExecuteReader: CommandText 属性尚未初始化 Fred
    Convert Datetime to String in Sql Server (转) Fred
    Js获取Gridview中模板列控件ID,获取控件生成的HTML中的ID Fred
    asp.net ajax calendar控件中textbox禁止客户端输入 Fred
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5738414.html
Copyright © 2020-2023  润新知