1426. Phone List Constraints
Time limit:1 secs, Memory limit:64 MB
Description
given a list of phone numbers, determine if it's consistent in the sense this no number is the Pre?x of another. Let ' s say the phone catalogue listed these NUMBERS:
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? RST three digits of Bob ' s phone number. So the list would not being consistent.
Input
The. RST line of input gives a single integer, 1≤t≤40, the number of test cases. Each test case is starts with N, the number of the phone numbers, and 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, the output "YES" If the list is consistent, or "NO" otherwise.
Sample Input
2391197625999911254265113123401234401234598346
Sample Output
NOYES
Just started to think of the problem is very complex, and later found that the original dictionary order and then. Compare adjacent two phones will be OK, because after the order of the dictionary sequence, the two adjacent phones in the common shortest length of the similarity is the highest:
It's easier to use string, but slower: 0.12s:
problem#: 1426//submission#: 2771683//The source code is licensed under Creative Commons Attribution-noncommercial-sh Arealike 3.0 Unported license//uri:http://creativecommons.org/licenses/by-nc-sa/3.0///all Copyright reserved by Informatic Lab of Sun Yat-sen university#include <stdio.h> #include <algorithm> #include <string.h># Include <string> #include <iostream>using namespace std;string p[10005];bool cmp (const string& A, const string& b) {for (int i = 0; i < (int) a.size () && i < (int) b.size () i++) {if (a[i] = = B[i]) Continue else if (A[i] < B[i]) return true; else return false; } return true; BOOL Is_ok (int n) {int k, J; for (int i = 0; i < n-1; i++) {for (k = 0, J = i + 1; k < (int) p[i].size () && k < (int) P[j].siz E (); k++) {if (p[i][k]! = P[j][k]) {break; }} if ((K >=(int) p[i].size ()) | | (k >= (int) p[j].size ())) {//cout << i << j << K << Endl; return false; }} return true; int main () {Ios::sync_with_stdio (false); int n, Case_num; Cin >> Case_num; while (case_num--) {cin >> n; for (int i = 0; i < n; i++) {p[i].clear (); CIN >> P[i]; } sort (p, p + N); if (IS_OK (n)) {cout << "YES" << Endl; } else {cout << "NO" << Endl; }} return 0;}
Sicily 1426. Phone List