• POJ 3630


    Phone List
    Time Limit: 1000MSMemory Limit: 65536K
    Total Submissions: 20894Accepted: 6532

    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:

    • Emergency 911
    • Alice 97 625 999
    • 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
    题意:判断每组串是不是某个串的前缀。
    sl:字典树轻松解决,沿途判断是不是经过单词节点,如果当前单词判断完了那么判断当前节点是不是有后继节点
    ps:差点爆内存,最好用左儿子右兄弟。
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int MAX = 20;
     6 const int A =  1000000;
     7 char num[MAX];
     8 int flag;
     9 int ch[A][11];
    10 struct Trie
    11 {
    12     int val[A]; int sz;
    13     Trie(){sz=1; memset(ch[0],0,sizeof(ch[0])); }
    14     int index(char a) {return a-'0';}
    15     void insert(char *s,int v)
    16     {
    17         int u=0,c; int n=strlen(s);
    18         for(int i=0;i<n;i++)
    19         {
    20             c=index(s[i]);
    21             if(!ch[u][c])
    22             {
    23                 memset(ch[sz],0,sizeof(ch[sz]));
    24                 val[sz]=0;
    25                 ch[u][c]=sz++;
    26             }
    27             u=ch[u][c];
    28             if(val[u]!=0) flag=1;
    29         }
    30         val[u]=v;
    31         for(int i=0;i<10;i++) if(ch[u][i]) flag=1;
    32     }
    33 };
    34 int main()
    35 {
    36     int cas,n;
    37     scanf("%d",&cas);
    38     while(cas--)
    39     {
    40         Trie trie;
    41         scanf("%d",&n); flag=0;
    42         for(int i=0;i<n;i++)
    43         {
    44             scanf("%s",num);
    45             trie.insert(num,i+1);
    46         }
    47         if(flag) printf("NO ");
    48         else printf("YES ");
    49     }
    50 }
    51 /*
    52 5 5
    53 123456
    54 6656
    55 3123
    56 13646
    57 1564
    58 */
  • 相关阅读:
    [转]按照HashTable动态设定类的属性和字段
    Enterprise Library 3.0 January 2007 CTP 数据访问程序块 _ 图解配置
    用Ad和windsor重构Portal
    ActiveRecordBase借助NHibernate的条件获取实体类对象
    Enterprise Library 3.0 January 2007 CTP 数据访问程序块 _ 系统自带链接字符串加密
    VS2005 SP1时因为安全策略而安装失败!
    [转]ASP.NET 2.0 的内部变化
    VB.NET 实现动态数组
    Making plain binary files using a C compiler (i386+)
    迭代器的使用方法简要介绍(摘自C++Primer)
  • 原文地址:https://www.cnblogs.com/acvc/p/3583320.html
Copyright © 2020-2023  润新知