首页 > 其他 > 详细

HDU1671 Phone List

时间:2018-05-21 10:55:15      阅读:138      评论:0      收藏:0      [点我收藏+]

Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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 1247 1800 1677 1298 
 


Statistic | Submit | Discuss | Note

 

http://acm.hdu.edu.cn/showproblem.php?pid=1671

 

技术分享图片
 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int n, t;
 6 bool ans;
 7 const int MAXN = 110000;
 8 int g[MAXN][15], f[MAXN], gx, w[MAXN];
 9 
10 bool add (int u, string s, int x) {
11     if (x >= s.length()) return 0;
12     w[u] = 1;
13     if (f[g[u][s[x] - 0]]) {
14         return 0;
15     }
16     else {
17         if (!g[u][s[x] - 0]) g[u][s[x] - 0] = ++gx;
18         if (x == s.length() - 1) {
19             if (w[g[u][s[x] - 0]]) return 0;
20             f[g[u][s[x] - 0]] = 1;
21             w[g[u][s[x] - 0]] = 1;
22             return 1;
23         }
24         else {
25             return add(g[u][s[x] - 0], s, x + 1);
26         }
27     }
28 }
29 
30 int main() {
31     cin >> t;
32     while (t--) {
33         ans = 1;
34         cin >> n;
35         memset(f, 0, sizeof(f));
36         memset(g, 0, sizeof(g));
37         memset(w, 0, sizeof(w));        
38         gx = 1;
39         while (n--) {
40             string s;
41             cin >> s;
42             ans = min(ans, add(1, s, 0));
43         }
44         if (ans) {
45             cout << "YES\n";
46         }
47         else {
48             cout << "NO\n";
49         }
50     }
51     return 0;
52 }
View Code

 

技术分享图片
 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 string s[10001];
 6 
 7 int main() {
 8     int t;
 9     cin >> t;
10     while (t--) {
11         int n;
12         cin >> n;
13         for (int i = 1; i <= n; ++i) cin >> s[i];
14         sort(s + 1, s + 1 + n);
15         bool tf = 1;
16         for (int i = 2; i <= n && tf; ++i) {
17             if (s[i].length() > s[i - 1].length()) {
18                 bool x = 1;
19                 for (int j = 0; j < s[i - 1].length(); ++j) {
20                     if (s[i - 1][j] != s[i][j]) {
21                         x = 0;
22                         break;
23                     }
24                 }
25                 if (x) tf = 0;
26             }
27         }
28         if (tf) cout << "YES\n";
29         else cout << "NO\n";
30     }
31     return 0; 
32 } 
View Code

 

HDU1671 Phone List

原文:https://www.cnblogs.com/m-m-m/p/9066091.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!