POJ 3630辞書ツリー


//   ,   
//  :     ,          :1.     911,   91125426   ,      is_word true        
//2.     91125426   911  ,            ,    ,      
//    :TLE   ,  :       ,new    
//  :        
//AC code
//10308326	c00h00g	3630	Accepted	2488K	157MS	C++	935B	2012-06-09 01:13:03
#include<stdio.h>
#include<string.h>

int root,loc;
bool is_prefix;
char ch[12];
struct trie_node{
	bool is_word;
	int child[10];
};
trie_node nd[100005];
//           
void trie_insert(char* str){
	int p=root;
	bool is_new_created=false;
	for(int i=0;i<strlen(str);i++){
		if(!nd[p].child[str[i]-'0']){
			is_new_created=true;
			nd[p].child[str[i]-'0']=++loc;
			nd[loc].is_word=false;
			memset(nd[loc].child,0,sizeof(nd[loc].child));
		}
		p=nd[p].child[str[i]-'0'];
		if(nd[p].is_word)
			is_prefix=true;
	}
	if(is_new_created==false)
		is_prefix=true;
	nd[p].is_word=true;
}


int main(){
	int N,n;
	scanf("%d",&N);
	while(N--){
		root=loc=1;
		memset(nd[1].child,0,sizeof(nd[1].child));
		is_prefix=false;
		scanf("%d",&n);
		while(n--){
			scanf("%s",ch);
			if(!is_prefix)
				trie_insert(ch);
		}
		if(is_prefix)
			printf("NO
"); else printf("YES
"); } return 0; }