poj3630

 

#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
struct node
{
	bool color;
	node *next[10];
	node()
	{
		color=false;
		memset(next,NULL,sizeof(next));
	}
};node tre[1000050];//动态创建会超时

int num;
bool insert(node *root,char *str)//可行时反回true
{
	int data,i;
	bool flg1;//假设可行
	bool flg2;//假设不可行
	node *p=root;
	flg1=true;flg2=false;
	for (i=0;i<strlen(str);i++)
	{
		data=str[i]-'0';
		if (p->next[data]==NULL)
		{
			p->next[data]=&tre[num++];flg2=true;
		}
		if (p->color!=true)
		{
			;
		} 
		else
		{
			flg1=false;
		}
		p=p->next[data];
	}
	p->color=true;
	return flg1&flg2;
}
int main()
{
	int i,t,n;
	char a[10];
	bool flg;
	cin>>t;
	while(t--)
	{
		flg=true;num=1;//假设可行
		cin>>n;
		for (i=0;i<n;i++)
		{
			scanf("%s",a);
			if (flg&&insert(&tre[0],a))
			{
				;
			} 
			else
			{
				flg=false;
			}
		}
		if (flg)
		{
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}

		for (i=0;i<=num;i++)
		{
			tre[i].color=false;
			memset(tre[i].next,NULL,sizeof(tre[i].next));
		}
	}
	return 0;
}
posted @ 2011-09-19 20:23  qijinbiao1  阅读(406)  评论(0编辑  收藏  举报