poj3630 Phone List Dictionary Tree

Source: Internet
Author: User

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:emergency 911 Alice 625 999 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≤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

2
3
911
97625999
91125426
5
113
12340
123440
12345
98346

Sample Output

NO
YES
Try the dictionary tree, the first time with a dictionary tree, there are several places to pay attention to, are in the comments inside ...
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace
Std
	struct trie{int cnt;
struct trie* erza[10];
}node[100005];//open a little bit int num=0;
	Char number[100005][11];//open a bit trie* build () {trie* p=&node[num++];
	p->cnt=0;
for (int k=0;k<10;k++) p->erza[k]=null;//do not forget to initialize return p; 
} void Insert (Trie *&root,char *number)//must add & symbol .....
	{int i=0;
	if (root==null) root=build ();
	Trie* P=root;
		while (Number[i]) {if (!p->erza[number[i]-' 0 ']) p->erza[number[i]-' 0 ']=build ();
		p=p->erza[number[i]-' 0 '];
		p->cnt++;
	i++;
	}} bool PD (trie* root,char* number) {int i=0;
	if (root==null) return true;
	Trie* P=root;
		while (Number[i]) {p=p->erza[number[i]-' 0 '];
		if (p->cnt==1) return false;
	i++;
} return true;
} int n;
	int main () {int t,i,j;
	cin>>t;
	Trie *root;
		while (t--) {bool flag=true;
		cin>>n;
		Root=null;
			for (i=0;i<n;i++) {cin>>number[i]; InsertRoot,number[i]);
				} for (i=0;i<n;i++) {if (PD (Root,number[i))) {flag=false;
			Break
		}} if (flag) cout<< "yes\n";
		else cout<< "no\n";
	num=0; }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.