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

hdu1671 Phone List [字典树 hash]

时间:2015-02-12 20:06:02      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

传送门

Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11633    Accepted Submission(s): 3965


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
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1075 1251 1247 1677 1298 
 

 

 

 

另一种解法:转自:http://fszxwfy.blog.163.com/blog/static/44019308201282484625551/

大致题意:多组电话号码,如果存在某组是其他组的前缀则输出NO

 

看了解题报告都说用字典树做,个人看了下题目数据突然想到一种巧法。

首先将所有的号码当做 long long 型存入数组,读完后进行排序,再从小到大读出每个数,并用map进行标记以示这个数出现过,然后在对这个数不断尽心除10操作,判断除10后的数有没有被标记,bingo..

不过WA了,后来想想看应该是有些号码前面是0,这样就不行了,不过后来一想可以将所有的数前面都加个1不就解决了。。。。

 

然后然后就这样ac了。。。。。  同学也在做  他都受不了  哈哈哈哈

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cstdio>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<map>
 9 #include<set>
10 #include<stack>
11 #include<string>
12 
13 #define N 20005
14 #define M 205
15 #define mod 10000007
16 //#define p 10000007
17 #define mod2 1000000000
18 #define ll long long
19 #define LL long long
20 #define eps 1e-6
21 #define inf 100000000
22 #define maxi(a,b) (a)>(b)? (a) : (b)
23 #define mini(a,b) (a)<(b)? (a) : (b)
24 
25 using namespace std;
26 
27 int n;
28 int T;
29 map<ll,bool>mp;
30 int flag;
31 
32 void ini()
33 {
34     flag=1;
35     scanf("%d",&n);
36     mp.clear();
37     char s[105];
38     int l;
39     int i;
40     queue<ll>q;
41     ll tt=1;
42     while(n--){
43         scanf("%s",s);
44         l=strlen(s);
45         tt=1;
46         for(i=0;i<l;i++){
47             tt=tt*10+s[i]-0;
48         }
49         q.push(tt);
50         mp[tt]=true;
51     }
52     ll te;
53     while(q.size()>=1){
54         te=q.front();
55         q.pop();
56         while(te>0){
57             te=te/10;
58             if(mp[te]==true){
59                 flag=0;return;
60             }
61         }
62         //q.push(nt);
63     }
64 
65 }
66 
67 void solve()
68 {
69 
70 }
71 
72 void out()
73 {
74     if(flag==0){
75         printf("NO\n");
76     }
77     else{
78         printf("YES\n");
79     }
80 }
81 
82 int main()
83 {
84     //freopen("data.in","r",stdin);
85     //freopen("data.out","w",stdout);
86     scanf("%d",&T);
87     //for(int ccnt=1;ccnt<=T;ccnt++)
88     while(T--)
89     //scanf("%d%d",&n,&m);
90     //while(scanf("%d",&n)!=EOF)
91     {
92         ini();
93         solve();
94         out();
95     }
96     return 0;
97 }

 

hdu1671 Phone List [字典树 hash]

标签:

原文地址:http://www.cnblogs.com/njczy2010/p/4288800.html

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