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 }
  • 相关阅读:
    set转成toarray()
    list和set的拉拉扯扯的关系
    【转载】VNC和远程桌面的区别
    笔记本最小安装centos7 连接WiFi的方法
    mysql 索引优化 性能调优 锁
    PageHelper 自动去掉排序参数问题
    抽奖算法 百万次抽奖 单线程环境下 约 3.5 秒
    gitlab 安装和使用
    sharding sphere 分表分库 读写分离
    mycat 安装 分表 分库 读写分离
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6854282.html
Copyright © 2011-2022 走看看