## Phone List

Time Limit: 5 Seconds Memory Limit: 32768 KB

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:

- Emergency 911

- Alice 97 625 999

- 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```

Output for Sample Input

```NO YES```

Source: The 2007 Nordic Collegiate Programming Contest

CODE

```#include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<algorithm> #include<vector> using namespace std;   int main() { int i, j, k, n; vector <string> v; char s[15]; scanf("%d", &n); while (n--) { scanf("%d", &k); v.clear(); j = k; while (j--) { scanf("%s", s); v.push_back(s); } sort(v.begin(), v.end()); --k; for(i = 0; i < k; ++i) { if(v[i+1].find(v[i]) == 0) { puts("NO"); goto out; } } puts("YES"); out:; } return 0; }```

```#include<stdio.h> #include<string.h>   struct trie { trie *node[10]; bool end; trie() { end = false; for(int i = 0; i < 10; ++i) node[i] = NULL; } } *root;   bool insert(char *s) { bool T = true; trie *p = root; int i, c; for(i = 0; s[i]; ++i) { c = s[i] - '0'; if(p -> node[c] == NULL) p -> node[c] = new trie(); p = p -> node[c]; if(p -> end) { T = false; break; } } p -> end = true; return T; }   void release(trie *p) { for(int i = 0; i < 10; ++i) if(p -> node[i]) release(p -> node[i]); delete(p); }   int main() { int i, j, k, t; char s[15]; scanf("%d", &t); while (t--) { root = new trie(); scanf("%d", &k); bool T = false; while(k--) { scanf("%s", s); if(T == false && insert(s) == false) { printf("NO\n"); T = true; } } if(T == false) printf("YES\n"); release(root); } return 0; }```
```#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std;   int main() { long long D[10005], tmp; int i, j, k, n; scanf("%d", &n); while(n--) { scanf("%d", &k); for(i = 0; i < k; ++i) { scanf("%lld", &tmp); D[i] = tmp; } sort(D,D+k); --k; for(i = 0; i < k; ++i) { tmp = D[i+1]; while(tmp > D[i]) tmp /= 10; if(tmp == D[i]) { puts("NO"); goto out; } } puts("YES"); out:; } return 0; }```

trie写的那个代码如果有人能帮我找出原因,真是感激不尽!

» 本博客采用署名 2.5 中国大陆许可协议进行许可，本文版权归作者所有，欢迎转载，但必须在明显位置给出原文连接。
anyShare分享到：