HDOJ 1671 Phone List(字典树)

http://acm.hdu.edu.cn/showproblem.php?pid=1671

Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11407    Accepted Submission(s): 3911


Problem Description
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:
1. Emergency 911
2. Alice 97 625 999
3. 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.
 


Input
The first line of input gives a single integer, 1 <= t <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
 


Output
For each test case, output “YES” if the list is consistent, or “NO” otherwise.
 


Sample Input
  
  
2 3 911 97625999 91125426 5 113 12340 123440 12345 98346
 


Sample Output
  
  
NO YES
 

题目大意:输入几串数字,每串都是由0~9中的数字组成,每串数字的个数不超过10,判断其中某一串是否是另一串的前缀。


下面分别用三种方法。

一):先建立Trie树,然后对整个树进行遍历。建立Trie树时,如果从根到该节点的数字串存在,则标记该节点;遍历的过程中,只要有一个节点被标记过而且该节点下边还有数字,也就是该节点的指针指向不为空,则存在该数字串是某一数字串的前缀。


//用Trie树写的,方法是先构建Trie树,再遍历。
//在HDOJ用C++提交会超时,用G++提交AC 
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define NUM 10 
#define change(n) (n-('1'-1))//将字符'1'转变为数字1 
struct Trie
{
	bool judge;
	struct Trie *num[10];
};
bool find=false;//是否找到满足这个字符串是在前面出现过的
bool Erdogic(struct Trie *head)
{//递归遍历Trie树 
	if(head->judge&&(head->num[0]||head->num[1]||head->num[2]||head->num[3]||head->num[4]||head->num[5]||head->num[6]||head->num[7]||head->num[8]||head->num[9]))
    {//如果找到满足这个字符串是在前面出现过的 
		find=true;
    	return find;
    }
    else
    {
    	int i;
    	struct Trie *p;
    	p=head;
    	for(i=0;i<NUM;i++)
    	if(!(p->num[i])) continue;
    	else
        Erdogic(p->num[i]);
    }
    return find;
}
//-----销毁Trie树,释放内存-----//
void Delete(struct Trie *root)
{
     int i;
	 for(i=0;i<NUM;i++)
	 { 
	     if(root->num[i])
	     Delete(root->num[i]);
	 }
	 free(root);
	  
}
//----------// 
int main()
{
	int M,N,i,j,k;
	scanf("%d",&M);
	while(M--)
	{
		scanf("%d",&N);
		//-----构建并初始化Trie字典树根-----//
		struct Trie *head;//树的根 
		head=(struct Trie *)malloc(sizeof(struct Trie));
		head->judge=false;
		for(i=0;i<NUM;i++)
		head->num[i]=NULL; 
		//----------------------------------//
		
		struct Trie *q;
		while(N--)//构造Trie树 
		{
			char c[20];
			int n;
			scanf("%s",c);
			n=strlen(c);
			
			q=head;
			for(i=0;i<n;i++)
			{
				int m=change(c[i]);
				if(q->num[m])
				{
					q=q->num[m];
					continue;
				}
				struct Trie *s;//创建新节点
				s=(struct Trie *)malloc(sizeof(struct Trie));
				s->judge=false;
				for(j=0;j<NUM;j++)
		        s->num[j]=NULL;
				q->num[m]=s;
				
				q=q->num[m]; 
		    }
		    q->judge=true;
		}//创建Trie树成功
		Erdogic(head);

	    if(find) printf("NO\n");
	    else printf("YES\n");
	    
	    Delete(head);//释放空间,否则会超内存 
	    find=false;
	
	}
	return 0;
}
	 


二):一边建立Trie树,一边判断。判断到该节点的串是否是前边串的子串,或者前边是否存在某一串是否是到该节点的串的子串,或者该串在前边是否已经存在过。所以一般情况下,不需要建立完整的Trie树。


//方法为一边建Trie树,一边判断
//在HDOJ用c++提交超时,用G++提交AC 
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define change(i) (i-('1'-1))
#define NUM 10
struct Trie
{
	bool judge;
	struct Trie *num[NUM];
};
bool prime=false;
void Build(struct Trie *root,char *list)
{
	int i,j,k,n,m;
	n=strlen(list);
	struct Trie *p=root;
	
	for(i=0;i<n;i++)
	{
		m=change(list[i]);
		if(p->num[m])
		{	
			if(i<n-1&&p->num[m]->judge)
			{
				//-----判断是否出现前缀的情况{1}-----//
		        //{1}:前面已经出现过123,这里出现12345
		        prime=true;
		        return;//直接返回main函数 
		    }
		    else
		    {
		    	p=p->num[m];
			    continue;
		    }
		}
		else
		{
			//-----创建新节点-----// 
			struct Trie *s;
			s=(struct Trie *)malloc(sizeof(struct Trie));
			s->judge=false;
			for(j=0;j<NUM;j++)
			s->num[j]=NULL;
			
			p->num[m]=s;
			//--------------------//
			p=p->num[m];
	    }
	}
	
	if(p->judge)
	{
		//-----判断是否出现前缀的情况{2}-----//
	    //{2}:前面已经出现123,这里出现123 
		prime=true;
		return;
	}
	else
	p->judge=true;
		
	
	//-----判断是否出现前缀的情况{3}-----//
	//{3}:前面已经出现12345,这里出现123 
	if(p->judge&&(p->num[0]||p->num[1]||p->num[2]||p->num[3]||p->num[4]||p->num[5]||p->num[6]||p->num[7]||p->num[8]||p->num[9]))
	{
	    prime=true;
		return;//返回main函数
	}	 
}
void Delete(struct Trie *root)
{//释放空间 
	int i;
	for(i=0;i<NUM;i++)
	{
	   if(root->num[i])
	   Delete(root->num[i]);
    }
    free(root); 
}
int main()
{
	int M,N,i,j,k;
	scanf("%d",&M);
	while(M--)
	{
		//-----创建根节点-----// 
		struct Trie *root;
		root=(struct Trie *)malloc(sizeof(struct Trie));
		root->judge=false;
		for(i=0;i<NUM;i++)
		root->num[i]=NULL;
		//--------------------//
		scanf("%d",&N);
		char s[20];
		for(i=0;i<N;i++)
		{
			memset(s,'\0',sizeof(s));
			scanf("%s",s);
			if(!prime)
			Build(root,s);
		}
		
		if(prime)
		printf("NO\n");
		else
		printf("YES\n");
		
		prime=false;
		Delete(root);
	}
	return 0;
}

说明:以上Trie树的两种方法在杭电oj提交时,用C++会超时,用G++可以AC。不知为何。


三):由于此题都是数字,且判断的是前缀,所以可以先排序,然后再按排序依次判断。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10000
struct List
{
	char s[10];
}num[MAX+10];
int cmp(const void *a,const void *b)
{
	struct List *c=(List *)a;
	struct List *d=(List *)b;
	if(strcmp(c->s,d->s)!=0)
	return strcmp(c->s,d->s);
}
int main()
{
	int M,N,i,j,k;
	scanf("%d",&M);
	while(M--)
	{
		scanf("%d",&N);
		for(i=0;i<N;i++)
		scanf("%s",num[i].s);
		qsort(num,N,sizeof(num[0]),cmp);
		
		bool judge=false;
		for(i=0;i<N-1;i++)
		{
			int n=strlen(num[i].s);
			if(strncmp(num[i].s,num[i+1].s,n)==0)
			{
				judge=true;
				break;
			}
			//从if的判断可以知道,前缀只可能是:123是12345的前缀,而不能说234是12345的前缀		 
		}
		if(judge)
		printf("NO\n");
		else
		printf("YES\n");
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值