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 }
  • 相关阅读:
    关于局域网内IIS部署网站,本机可访问,而网内其他用户无法访问问题的解决方法
    spark出现task不能序列化错误的解决方法
    Ganglia安装
    Hadoop自定义JobTracker和NameNode管理页面
    如何编写自定义hive UDF函数
    HighChart利用servlet导出中文PNG图片乱码问题解决
    sparkR介绍及安装
    在Linux中安装redmine
    在Ubuntu14.10中部署Hadoop2.6.0单节点伪分布集群
    【转】Spark on Yarn遇到的几个问题
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6854282.html
Copyright © 2011-2022 走看看