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
Source
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)
题目类型:Trie树
算法分析:直接将所有的字符串插入到Trie树中,然后对于每个字符串,判断到终止节点时下一层是否还有next不为NULL的情况即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 1e9 + 7; const int maxn = 10; char s[10000+66][maxn]; struct Trie { Trie *next[maxn]; Trie () { memset (next, 0, sizeof (next)); } }; Trie *root; void Init () { root = new Trie (); } void Insert (char *s) { if (!s[0]) return ; Trie *p = root; for (int i = 0; s[i]; i++) { if (!p -> next[s[i]-'0']) { p -> next[s[i]-'0'] = new Trie; } p = p -> next[s[i]-'0']; } } bool Query (char *s) { if (!s[0]) return false; Trie *p = root; for (int i = 0; s[i]; i++) { if (!p -> next[s[i]-'0']) return false; p = p -> next[s[i]-'0']; } for (int i = 0; i < 10; i++) if (p -> next[i]) return true; return false; } void DelTrie (Trie *t) { if (!t) return ; for (int i = 0; i < maxn; i++) { if (t -> next[i]) DelTrie (t -> next[i]); } delete t; } int main() { // freopen("aaa.txt", "r", stdin); int t; scanf ("%d", &t); while (t--) { int n; scanf ("%d", &n); Init (); for (int i = 1; i <= n; i++) { scanf ("%s", s[i]); Insert (s[i]); } bool is_valid = true; for (int i = 1; i <= n; i++) { if (Query (s[i])) { is_valid = false; break; } } if (is_valid) puts ("YES"); else puts ("NO"); DelTrie (root); } return 0; } |
- « 上一篇:hdu1576
- hdu1698:下一篇 »