动物统计加强版

                               动物统计加强版

                   第一次学会使用字典树,调了4个小时~~,还有待提高。

http://acm.nyist.net/JudgeOnline/problem.php?pid=290

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
int lmax=0;
struct dictree
{
  struct dictree *child[26];
   int sum;
};
struct dictree *root;

int insert(char *temp)//动态创建树
{
    int len,i,j;
    struct dictree *now,*newnode=NULL;
	now=root;
    len=strlen(temp);   
   for(i=0;i<len;i++)
   { 	   	 
         if(now->child[temp[i]-'a']!=NULL)		   			
		      now=now->child[temp[i]-'a'];             			  		  		 
         else
		 {		 		     				     
			 newnode=(struct dictree *)malloc(sizeof(struct dictree));
			  for(j=0;j<26;j++)//注意,这个j还得我很惨,我之前写的是用变量i来进行赋值的。4个小时,不要再犯这种低级的错误.
				 newnode->child[j]=NULL;
                  newnode->sum=0;   				  		         
			 now->child[temp[i]-'a']=newnode;			
				now=newnode;
		 }                   
   }                          
            now->sum++;
               if(now->sum>lmax)
			   {
				  lmax=now->sum;
				  return 1;//表示换了
			   }       
   return 0;//表示没有换
}

int main()
{
    int i,n,t,len;
	char temp[27],aid[27];
	root=(struct dictree *)malloc(sizeof(struct dictree));    
	for(i=0;i<26;i++)
		root->child[i]=NULL;
	root->sum=0;		
       	scanf("%d",&n);
	while(n--)
	{
	     scanf("%s",temp);		 
		 t=insert(temp);     
		 if(t==1)			 
		 {
		   len=strlen(temp);
		   for(i=0;i<len;i++)
			   aid[i]=temp[i];
		   aid[i]='\0';		 		 
		 }		 	
	}
  printf("%s %d\n",aid,lmax);
return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值