动物统计加强版

来源:互联网 发布:时时彩单期计划软件 编辑:程序博客网 时间:2024/04/29 04:38

                               动物统计加强版

                   第一次学会使用字典树,调了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;}


原创粉丝点击