hdu1671

/*
分析:
    昨天才自学了字典树,晚上还在想这种数据结构的推广,想到的就有
数字的,没想到今儿就碰上了,轻松搞定~O(∩_∩)O~
    建立字典树,每次测试完要释放。


                                  2012-07-05
*/






#include"stdio.h"
#include"string.h"
#include"stdlib.h"


struct dic
{
	struct dic *child[10];
};
struct dic *root;


void insert(char *source)
{
	struct dic *now,*newnode;
	int i,j;
	int len;


	len=strlen(source);
	now=root;


	for(i=0;i<len;i++)
	{
		if(now->child[source[i]-'0'])	now=now->child[source[i]-'0'];
		else
		{
			newnode=(struct dic *)malloc(sizeof(struct dic));
			for(j=0;j<10;j++)	newnode->child[j]=0;
			now->child[source[i]-'0']=newnode;
			now=newnode;
		}
	}
}


int find(char *source)
{
	struct dic *now;
	int i;
	int len;
	int flag;


	len=strlen(source);
	now=root;


	for(i=0;i<len;i++)	now=now->child[source[i]-'0'];


	flag=0;
	for(i=0;i<10;i++)	if(now->child[i])	{flag=1;break;}


	return flag;
}


void release(struct dic * now)
{
	int i;
	for(i=0;i<10;i++)
		if(now->child[i])	release(now->child[i]);
	free(now);
}


int main()
{
	int T;
	int n;
	int i,l,j;
	int ans;
	char str[10011][12];
	char temp[12];


	scanf("%d",&T);
	while(T--)
	{
		root=(struct dic *)malloc(sizeof(struct dic));
		for(j=0;j<10;j++)	root->child[j]=0;

		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			scanf("%s",str[i]);
			insert(str[i]);
		}

		ans=0;
		for(i=0;i<n;i++)
		{
			ans=find(str[i]);
			if(ans)	break;
		}

		if(ans)	printf("NO\n");
		else	printf("YES\n");


		release(root);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值