【题解】UVA11362 Phone List

Tags :

​ 排序,字典树

​ 从短到长排序,逐个插入字典树。若与已有的重复,返回错误信息。

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#define re register
#define GC getchar()
#define Clean(X,K) memset(X,K,sizeof(X))
using namespace std ;
const int Maxn = 10005 , MaxD = 10 , Base = 10 ;
int Times , N , T[Maxn * MaxD][Base] , End[Maxn * MaxD] , Tot = 0;
string S[Maxn] ;
bool Add (string &S) {
	int L = S.length() , P = 0 ;
	for (re int i = 0 ; i < L; ++ i) {
		if (!T[P][S[i] - '0']) T[P][S[i] - '0'] = ++ Tot ;
		P = T[P][S[i] - '0'] ;
		if (End[P]) return true ;
	}
	++ End[P] ;
	return false ;
}
bool C (const string &X , const string &Y) {
	return X.length() < Y.length() ;
}
int main () {
//	freopen ("POJ3630.in" , "r" , stdin) ;
	scanf ("%d" , &Times) ;
	while (Times -- ) {
		scanf ("%d\n" , &N) ;
		Clean (T , 0) , Clean (End , 0);
		Tot = 0 ;
		bool Fl = true ;
		for (re int i = 0 ; i < N; ++ i) getline (cin , S[i]) ;
		sort (S , S + N , C) ;
		for (re int i = 0 ; i < N; ++ i) {
			if (Add (S[i])) {
				printf("NO\n") ;
				Fl = false;
				break ;
			}
		}
		if (Fl ) printf ("YES\n") ;
	}
	fclose (stdin) , fclose (stdout) ;
	return 0 ;
}
posted @ 2019-03-29 11:32  Betulaceae  阅读(176)  评论(0编辑  收藏  举报