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

    http://acm.hdu.edu.cn/showproblem.php?pid=1671

    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 #include<iostream>
     2 using namespace std;
     3 typedef struct node
     4 {
     5     node * next[10];
     6     int num;
     7     node()
     8     {
     9         num=0;
    10         memset(next,NULL,sizeof(next));
    11     }
    12 }Nod;
    13 void insert_tree(Nod * head,char *str)
    14 {
    15     Nod *h=head;
    16     int len=strlen(str);
    17     int i;
    18     for(i=0;i<len;i++)
    19     {
    20         int id=str[i]-'0';
    21         if(h->next[id]==NULL)
    22             h->next[id]=new node;
    23         h=h->next[id];
    24     }
    25     h->num=1;   //这个标记结尾
    26 }
    27 int find_tree(Nod *head,char *str)
    28 {
    29     int i;
    30     Nod *h=head;
    31     int len=strlen(str);
    32     for(i=0;i<len;i++)
    33     {
    34         int id=str[i]-'0';
    35         if(h->next[id]==NULL)
    36             return h->num;     //这句重要
    37         h=h->next[id];
    38     }
    39     return 1;  //这句也重要
    40 }
    41 void freedom(Nod *h)
    42 {
    43     int i;
    44     for(i=0;i<10;i++)
    45     {
    46         if(h->next[i]!=NULL)
    47             freedom(h->next[i]);
    48     }
    49     delete h;
    50 }
    51 int main()
    52 {
    53     char str[20];
    54     int t;
    55     scanf("%d",&t);
    56     while(t--)
    57     {
    58         int i,n;
    59         scanf("%d",&n);
    60         getchar();    
    61         Nod *head;
    62         head=new node;
    63         int flag=0;
    64         for(i=0;i<n;i++)
    65         {
    66             gets(str);
    67             if(flag||find_tree(head,str))
    68                 flag=1;
    69             else
    70                 insert_tree(head,str);    
    71         }
    72         if(!flag)
    73             puts("YES");
    74         else
    75             puts("NO");
    76         freedom(head);
    77     }
    78     return 0;
    79 }
  • 相关阅读:
    友盟统计,监听事件次数。
    webView 加载网页
    Springboot 启动时Bean初始化,启动异常-Assert.isTrue(condition,message) 报错
    Springboot使用@ConfigurationProperties注解 配置读不进去
    2018即将结束,给寒假李哥flag
    大精度求和,给任意两个数 m,n 甚至m,n->∞ 计算x+y
    第二章JavaScript 函数和对象
    第三章JavaScript 内置对象
    响应式网页设计
    新的页面布局方式
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2658249.html
Copyright © 2011-2022 走看看