zoukankan      html  css  js  c++  java
  • Phone List

    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.数据量比较大,申请后要释放。
    2.静态的更好一点
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 using namespace std;
     6 
     7 bool flag;
     8 
     9 struct node{
    10     bool exist;
    11     node* next[10];
    12     node(){
    13         exist=0;
    14         memset(next,0,sizeof(next));
    15     }
    16 };
    17 
    18 node* rt;
    19 node* root=NULL;
    20 
    21 void build(char *a){
    22     rt=root;
    23     bool f=0;
    24     int t=0,len=strlen(a);
    25     for(int i=0;i<len;i++){
    26         int id=a[i]-'0';
    27         if(rt->next[id]) t++;
    28         if(rt->exist==1) f=1;
    29         if(!rt->next[id]) rt->next[id]=new node();
    30         rt=rt->next[id];
    31     }
    32     rt->exist=1;
    33     if(t==len||f) flag=1;
    34 }
    35 
    36 void del(node* root){
    37     for(int i=0;i<10;i++){
    38         if(root->next[i])
    39            del(root->next[i]);
    40     }
    41     delete(root);
    42 }
    43 int main()
    44 {   int n;scanf("%d",&n);
    45     while(n--){
    46         int m;scanf("%d",&m);
    47         flag=0;
    48         root=new node();
    49         for(int i=0;i<m;i++){
    50             char s[20];
    51             scanf("%s",s);
    52             build(s);
    53         }
    54         if(flag) cout<<"NO"<<endl;
    55         else cout<<"YES"<<endl;
    56         del(root);
    57     }
    58 }
  • 相关阅读:
    bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
    CentOS 7下MySQL安装配置
    CentOS 7下设置DNS服务器
    MySQL Table is marked as crashed 解决方法
    supervisor使用
    Linux更改服务器Hostname
    在Linux中让打印带颜色的字
    php安装gearman扩展实现异步分步式任务
    GitLab的Gravatar头像服务不可用
    Nginx + tornado + supervisor部署
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6854282.html
Copyright © 2011-2022 走看看