B - Phone List

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

题目大意
判断一组电话能否从头到尾拨打是唯一的,例如样例一中 911 和 91125426 ,当拨打911 时,无法确定911是已经输入完毕还是在继续输入。
输入:输入多组电话
输出:能否唯一,是输出yes否输出no。

Tip
可以建一颗字典树,如果是输入911再输入91125426,当录入91125426的第一个2时就会发现,前面有一个录入为到911,如果911后继续录入就不能保证唯一,故为No。
反过来,如果是先输入91125426再输入911,那么在录入911的第二个1结束后,会发现,前面的录入在911的1之后还有录入,故还是不能保证唯一。
以上两种情况一起考虑就能解决问题。

Others
以下为本人代码,其中用数组和指针代替动态申请内存过程。

B - Phone List_第1张图片
demo

你可能感兴趣的:(B - Phone List)