标签:sicily
Time Limit: 1 secs, Memory Limit: 64 MB
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pre?x 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 ?rst three digits of Bob’s phone number. So this list would not be consistent.
The ?rst 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.
For each test case, output “YES” if the list is consistent, or “NO” otherwise.
2 3 911 97625999 91125426 5 113 12340 123440 12345 98346
NO YES
刚刚开始把这道题想的很复杂,后来发现原来按照字典序排序然后。。比较相邻两个电话就ok了,因为按照字典序排完序之后,相邻两个电话在公共最短长度内的相似度是最高的:
用string排方便些,不过时间上要慢些:0.12s:
// Problem#: 1426 // Submission#: 2771683 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 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].size(); 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
原文地址:http://blog.csdn.net/u012925008/article/details/44738759