Phone List
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 34805 | Accepted: 9980 |
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:
- 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
Sample Output
NO YES
Source
题意:
给n个最多是10位的电话号码,问存不存在某个电话号码是其他电话号码的前缀的情况。存在输出NO,否则输出YES
思路:
最开始的思路是先给所有的字符按照字符串的长度从短到长排个序,一次将字符串添加到Trie树中。添加过程中如果发现某个节点已经被标记为字符串结束,那么说明要输出NO。这样的思路TLE。大概是因为对于每一个字符串都要先求一下长度然后排序的话就变成了O(n^2logn)?
其实我们并不需要排序。只需要在建立Trie树的时候对节点的出现次数进行统计。并且,一条路径上靠近根节点的节点的次数一定是大于等于远离根节点的。建立完成后我们对于每一个字符串,去找他在树上的这个路径是不是全部都是次数大于1的节点。只要找到一个路径全部都是大于1的节点的路径说明这个字符串是某个字符串的前缀。输出NO
1 #include <iostream> 2 #include <set> 3 #include <cmath> 4 #include <stdio.h> 5 #include <cstring> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 using namespace std; 11 typedef long long LL; 12 #define inf 0x7f7f7f7f 13 14 int t, n; 15 const int maxn = 1e5 + 5; 16 int ed[maxn]; 17 int trie[maxn][10], tot = 1; 18 //vector<string>s; 19 //string s[maxn]; 20 char s[maxn][10]; 21 22 void insertt(char* str) 23 { 24 int len = strlen(str), p = 1; 25 for(int k = 0; k < len; k++){ 26 int ch = str[k] - ‘0‘; 27 if(trie[p][ch] == 0){ 28 trie[p][ch] = tot++; 29 } 30 p = trie[p][ch]; 31 ed[p]++; 32 } 33 //ed[p] = true; 34 //ed[p] = true; 35 } 36 37 bool searchh(char *str) 38 { 39 int len = strlen(str), p = 19; 40 for(int k = 0; k < len; k++){ 41 p = trie[p][str[k] - ‘0‘]; 42 if(ed[p] == 1){ 43 return false; 44 } 45 } 46 return true; 47 } 48 49 bool cmp(string a, string b) 50 { 51 return a.size() > b.size(); 52 //return strlen(a) > strlen(b); 53 } 54 55 int main() 56 { 57 scanf("%d", &t); 58 while(t--){ 59 tot = 1; 60 memset(trie, 0, sizeof(trie)); 61 memset(ed, 0, sizeof(ed)); 62 scanf("%d", &n); 63 for(int i = 0; i < n; i++){ 64 /*char c[10]; 65 string ch; 66 scanf("%s", c); 67 ch = c; 68 s.push_back(ch); 69 //cin>>s[i];*/ 70 scanf("%s", &s[i]); 71 insertt(s[i]); 72 } 73 //sort(s.begin(), s.end(), cmp); 74 /*for(int i = 0; i < n; i++){ 75 cout<<s[i]<<endl; 76 }*/ 77 bool flag = true; 78 for(int i = 0; i < n; i++){ 79 if(searchh(s[i])){ 80 flag = false; 81 break; 82 } 83 /*if(searchh((char*)s[i].data())){ 84 printf("NO\n"); 85 flag = false; 86 break; 87 } 88 else{ 89 insertt((char*)s[i].data()); 90 }*/ 91 } 92 if(flag){ 93 printf("YES\n"); 94 } 95 else{ 96 printf("NO\n"); 97 } 98 } 99 return 0; 100 }
原文地址:https://www.cnblogs.com/wyboooo/p/9838235.html