码迷,mamicode.com
首页 > 其他好文 > 详细

Phone List

时间:2015-02-27 21:25:48      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

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
 
//排序比较相邻的字符串是否包含,  time:156 ms;
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <iomanip>
13 #include <cstdlib>
14 using namespace std;
15 const int INF=0x5fffffff;
16 const int MS=10005;
17 const double EXP=1e-8;
18 
19 char str[MS][11];
20 
21 int cmp(const void *a,const void *b)
22 {
23     return strcmp((char *)a,(char *)b);
24 }
25 
26 int main()
27 {
28     int T;
29     int n,i;
30     scanf("%d",&T);
31     while(T--)
32     {
33         scanf("%d",&n);
34         for(i=0;i<n;i++)
35             scanf("%s",str[i]);
36         //sort(str,str+n,cmp);
37         qsort(str,n,sizeof(str[0]),cmp);
38         int ok=1;
39         for(i=1;i<n&&ok;i++)
40         {
41             if(strncmp(str[i-1],str[i],strlen(str[i-1]))==0)
42                 ok=0;
43         }
44         if(ok)
45             puts("YES");
46         else
47             puts("NO");
48     }
49     return 0;
50 }

 

 //  静态 Trie  树法   time 78 ms
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <iomanip>
13 #include <cstdlib>
14 using namespace std;
15 const int INF=0x5fffffff;
16 const int MS=100005;
17 const double EXP=1e-8;
18 
19 struct node
20 {
21     bool have;
22     node * next[10];
23 }nodes[MS];
24 node *root;
25 bool flag;
26 int cnt;
27 node * add_node(int c)
28 {
29     node *p=&nodes[c];
30     for(int i=0;i<10;i++)
31         p->next[i]=NULL;
32     p->have=false;
33     return p;
34 }
35 void insert(char *str)
36 {
37     node *p=root,*q;
38     int len=strlen(str);
39     for(int i=0;i<len;i++)
40     {
41         int id=str[i]-0;
42         if(p->next[id]==NULL)
43         {
44             q=add_node(cnt++);
45             p->next[id]=q;
46         }
47        // if(p->have==true)
48        //     flag=true;
49         p=p->next[id];
50         if(p->have==true)
51             flag=true;
52     }
53     p->have=true;
54     for(int i=0;i<10;i++)
55     {
56         if(p->next[i]!=NULL)
57         {
58             flag=true;
59             break;
60         }
61     }
62 }
63 int main()
64 {
65     int i,n,t;
66     char str[15];
67     scanf("%d",&t);
68     while(t--)
69     {
70         cnt=0;
71         root=add_node(cnt++);
72         flag=false;
73         scanf("%d",&n);
74         for(i=0;i<n;i++)
75         {
76             scanf("%s",str);
77             if(!flag)
78             {
79                 insert(str);
80             }
81         }
82         if(flag==false)
83             printf("YES\n");
84         else
85             printf("NO\n");
86     }
87     return 0;
88 }

 

 
 

 

Phone List

标签:

原文地址:http://www.cnblogs.com/767355675hutaishi/p/4304351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!