Sicily 1426. Phone List

1426. Phone ListConstraints

Time Limit: 1 secs, Memory Limit: 64 MB

Description

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 26In 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.

Input

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.

Output

For each test case, output “YES” if the list is consistent, or “NO” otherwise.

Sample Input2391197625999911254265113123401234401234598346Sample OutputNOYES刚刚开始把这道题想的很复杂,后来发现原来按照字典序排序然后。。比较相邻两个电话就ok了,,因为按照字典序排完序之后,相邻两个电话在公共最短长度内的相似度是最高的:

用string排方便些,不过时间上要慢些:0.12s:

// Problem#: 1426// Submission#: 2771683// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: // 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;elsereturn 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 1426. Phone List

相关文章:

你感兴趣的文章:

标签云: