南邮 OJ 1522 Phone List

Phone List

时间限制(普通/Java) :  1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
总提交 : 126            测试通过 : 47 

比赛描述

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.



输入

The first line of input gives a single integer, 1 ≤ ≤ 40, the number of test cases. Each
test case starts with n, the number of phone numbers, on a separate line, 1 ≤ ≤ 10000.
Then follows 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

提示

undefined

题目来源

Nordic Collegiate Programming Contest 2007




#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char phone[10000][11];

int comp(const void *c1, const void *c2){
	return strcmp((char*)c1,(char*)c2);
}
int main(){
	int t,n,i;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(i=0; i<n; i++){
			scanf("%s",phone[i]);
		}
		qsort(phone,n,sizeof(phone[0]),comp);
		bool consistent = 1;
		for(i=0; i<n-1; i++){
			if( strncmp(phone[i],phone[i+1],strlen(phone[i])) == 0 ){
				consistent = 0;
				break;
			}
		}
		if(consistent){
			printf("YES\n");
		}else{
			printf("NO\n");
		}
	}
}




/* 1000 MS
#include<stdio.h>

bool consistent = 1;

struct tNode{
	bool visited;
	tNode *n[10];
};

void delTree(tNode *root){
	for(int i=0; i<10; i++){
		if(root->n[i]){
			delTree(root->n[i]);
		}
	}
	delete root;
}

void insert(int i, const char *c, tNode *pNode){
	int num=c[i]-'0';
	if(pNode->visited){
		consistent = 0;				//某一数字串的最后节点  被访问到了, 不是consistent
		return;						//就不用做其他动作了
	}
	if(!pNode->n[num]){
		pNode->n[num] = new tNode();
	}else if(!c[i+1]){				//WA1 先出现长的,再出现短的
		consistent = 0;
		return;
	}
	i++;
	if(c[i]){
		insert(i, c, pNode->n[num]);
	}else{								//最后一站
		pNode->n[num]->visited = 1;
	}
}

int main(){
	int t, n;
	char c[21];
	tNode *root;
	while(scanf("%d",&t)==1){
		while(t--){
			scanf("%d",&n);
			getchar();
			consistent = 1;
			root = new tNode();
			while(n--){
				scanf("%s",c);
				if(consistent){
					insert(0,c,root);
				}
			}
			if(consistent){
				printf("YES\n");
			}else{
				printf("NO\n");
			}
			delTree(root);
		}
	}
}
*/




/*Time Limit Exceed at Test 1
#include<iostream>
#include<string>
#include<set>
using namespace std;

int main(){
	int t,n,len;
	string s;
	set<string> sSet;
	set<string>::iterator it1,it2;
	cin>>t;
	while(t--){
		cin>>n;
		sSet.clear();
		while(n--){
			cin>>s;
			sSet.insert(s);
		}
		it1 = it2 = sSet.begin();
		for(++it2; it2!=sSet.end(); ++it1,++it2){
			len = (int)(*it1).length();
			if(len<=(int)(*it2).length() && (*it1)==(*it2).substr(0,len)){
				break;
			}
		}
		if(it2!=sSet.end()){
			cout<<"NO"<<endl;
		}else{
			cout<<"YES"<<endl;
		}
	}
}
*/





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值