zoukankan      html  css  js  c++  java
  • HDU 1617 Phone List (排序||字典树)

    Phone List

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


    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
     
    Source
     
    Recommend
    lcy
     

    字符串排序再扫描一遍即可

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<stdlib.h>
     6 #include<algorithm>
     7 using namespace std;
     8 const int MAXN=10000+5;
     9 int T,n;
    10 struct node
    11 {
    12     char str[10+5];
    13 }a[MAXN];
    14 
    15 int cmp(const node A,const node B)
    16 {
    17     return strcmp(A.str,B.str)<0;
    18 }
    19 
    20 int judge(char a[],char b[])
    21 {
    22     int len=strlen(a);
    23     for(int i=0;i<len;i++)
    24         if(a[i]!=b[i])
    25             return 0;
    26     return 1;
    27 }
    28 int main()
    29 {
    30     //freopen("in.txt","r",stdin);
    31     scanf("%d",&T);
    32     while(T--)
    33     {
    34         scanf("%d",&n);
    35         for(int i=0;i<n;i++)
    36             scanf("%s",a[i].str);
    37 
    38         sort(a,a+n,cmp);//利用结构体对字符串排序
    39 
    40         int ok=0;
    41         for(int i=0;i<n-1;i++)
    42         {
    43             if(judge(a[i].str,a[i+1].str))
    44             {
    45                 ok=1;
    46                 break;
    47             }
    48         }
    49         
    50         if(ok)
    51             printf("NO
    ");
    52         else
    53             printf("YES
    ");
    54     }
    55     return 0;
    56 }
    View Code
  • 相关阅读:
    28、数组中出现次数超过一半的数字
    27、字符串的排列
    26、二叉搜索树与双向链表
    21、栈的压入、弹出序列
    22、从上往下打印二叉树
    23、二叉搜索树的后序遍历序列
    24、二叉树中和为某一值的路径
    25、复杂链表的复制
    4、简单工厂模式,工厂方法模式,
    基数排序
  • 原文地址:https://www.cnblogs.com/clliff/p/3965269.html
Copyright © 2011-2022 走看看