• HDOJ1671 Phone List 字典树的应用判断一组字符串中是否有一个字符串是另一个字符串的前缀(字典树第二类应用)。


    Phone List

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 5081    Accepted Submission(s): 1714


    Problem 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
     1 /* 功能Function Description:     HDOJ-1671   字典树问题
     2    开发环境Environment:          DEV C++ 4.9.9.1
     3    技术特点Technique:
     4    版本Version:
     5    作者Author:                   可笑痴狂
     6    日期Date:                      20120809
     7    备注Notes:
     8         这道题就是判断一组字符串中是否有一个字符串是另一个字符串的前缀
     9         注意有两种情况
    10 */
    11 #include<stdio.h>
    12 #include<string.h>
    13 
    14 typedef struct node
    15 {
    16     int num;    //标记该字符是否是某一字符串的结尾
    17     struct node *next[10];
    18 }node;
    19 
    20 node memory[1000000];
    21 int k;
    22 
    23 int insert(char *s,node *T)
    24 {
    25     int i,len,id,j;
    26     node *p,*q;
    27     p=T;
    28     len=strlen(s);
    29     for(i=0;i<len;++i)
    30     {
    31         id=s[i]-'0';
    32         if(p->num==1)   //说明存在先前字符可以作为s的前缀----(先短后长)
    33             return 1;
    34         if(p->next[id]==NULL)
    35         {
    36             q=&memory[k++];
    37             q->num=0;
    38             for(j=0;j<10;++j)
    39                 q->next[j]=NULL;
    40             p->next[id]=q;
    41         }
    42         p=p->next[id];
    43     }
    44     for(i=0;i<10;++i)      //如果p的后继结点不为空的话说明s时先前字符的前缀----(先长后短)
    45         if(p->next[i]!=NULL)
    46             return 1;
    47     p->num=1;
    48     return 0;
    49 }
    50 
    51 int main()
    52 {
    53     int m,n,flag,i;
    54     node *T;
    55     char s[15];
    56     scanf("%d",&m);
    57     while(m--)
    58     {
    59         k=0;          //每次都从数组下标为0的地方开始分配内存,可以使内存循环利用,从而不会造成内存超限
    60         T=&memory[k++];
    61         T->num=0;
    62         for(i=0;i<10;++i)
    63             T->next[i]=NULL;
    64         flag=0;
    65         scanf("%d",&n);
    66         while(n--)
    67         {
    68             scanf("%s",s);
    69             if(flag)
    70                 continue;
    71             if(insert(s,T))
    72                 flag=1;
    73         }
    74         if(flag)
    75             printf("NO\n");
    76         else
    77             printf("YES\n");
    78     }
    79     return 0;
    80 }
     
    功不成,身已退
  • 相关阅读:
    allocator类
    智能指针shared_ptr
    字面值常量类
    转换构造函数
    委托构造函数
    访问说明符&封装
    const成员函数
    函数指针
    constexper和常量表达式
    函数返回数组指针
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2630724.html
Copyright © 2020-2023  润新知