Phone List
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 11655 Accepted Submission (s): 3970
Problem Description Given A list of phone numbers, determine if it is consistent in the sense this 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-to-the-emergency line as soon as Y OU had dialled the first three digits of Bob ' s phone number. So the list would not being consistent.
Input the first line of input gives a single integer, 1 <= t <=, the number of test cases. Each test case is starts with N, the number of the phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with a unique phone number on each line. A phone number is a sequence of at the 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 "Insigma International Cup" Zhejiang Collegiate Programming Contest-warm up (3)
is a simple dictionary tree insert operation, in the insert operation is judged whether there will be a prefix phenomenon. You need to destroy the memory, otherwise it will be mle. My Code C + + timed out, g++234ms, heart plug.
.............................. If you know the reason for the time-out, you might as well teach the weak and weak .... .......
AC Code:
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> # Include <map> #include <queue> #include <vector> #include <cstdlib> #include <algorithm> # Define LS u << 1 #define RS u << 1 | 1 #define Lson L, Mid, U << 1 #define Rson mid + 1, R, u << 1 |
1 #define INF 0x3f3f3f3f #define MAX using namespace std;
typedef long Long LL;
const int M = 1e4 + 100;
const int mod = 2147483647;
struct trie{Trie *next[max];
int index;
Trie () {index = 1;
memset (Next,0,sizeof (next));
}
};
Char s[20];
BOOL Flag;
void Trie_insert (Trie *tr,int len) {if (!flag) return;
if (S[len]) {int u = S[len]-' 0 ';
if (tr->next[u] = = 0) {Tr->next[u] = new Trie;
} else {if (Tr->next[u]->index = =-1 | | S[len + 1] = = ' + ') {flag = false;
return;
} } trie_insert (Tr->next[u],len + 1);
} else Tr->index =-1;
} void Deal_trie (Trie *tr) {if (tr = = NULL) return;
for (int i = 0; i < MAX; i++) {if (Tr->next[i]) Deal_trie (Tr->next[i]);
} free (TR);
} int main () {int t,n;
cin>>t;
while (t--) {scanf ("%d", &n);
Flag = true;
Trie *root = new Trie;
while (n--) {scanf ("%s", s);
if (flag) Trie_insert (root,0);
} if (flag) puts ("YES");
Else puts ("NO");
Deal_trie (root);
} return 0;
}
Hash grazing, need a small pruning. Consider a leading 0, so add 1 before each number, without affecting the result.
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> # Include <map> #include <queue> #include <vector> #include <cstdlib> #include <algorithm> # Define LS u << 1 #define RS u << 1 | 1 #define Lson L, Mid, U << 1 #define Rson mid + 1, R, u << 1 |
1 #define INF 0x3f3f3f3f #define MAX using namespace std;
typedef long Long LL;
const int M = 1e4 + 100;
const int mod = 2147483647;
map<ll,int>mp;
ll D[m];
bool Solve (int n) {mp[d[0]] = 1;
for (int i = 1; i < n; i++) {ll res = d[i];
while (res >= 100) {//pruning, no timeout is added.
Res/= 10;
if (Mp[res]) return 1;
} Mp[d[i]] = 1;
} return 0;
} int main () {int t,n;
Char s[20];
scanf ("%d", &t);
while (t--) {mp.clear ();
scanf ("%d", &n);
for (int i = 0; i < n; i++) {scanf ("%s", s); ll res = 1;
for (int j = 0; s[j]; j + +) Res = res * + s[j]-' 0 ';
D[i] = res;
} sort (d,d + N);
if (Solve (n)) puts ("NO");
Else puts ("YES");
} return 0; }