nyoj290动物统计加强版(字典树)

题目链接:click here~

第一次接触字典树,写了一下午终于写出来了,因为一个很小的错误,调试了半天,最后还从写了一遍,看来还要多写代码才是硬道理!得意

代码如下:

#include<stdio.h>
02. #include<string.h>
03. #include<stdlib.h>
04. char str[15],ans[15];
05. int max;
06. struct node
07. {
08. struct node* next[26];
09. int count;
10. };
11. struct node* root;
12. struct node* newnode()        //建立新的结点
13. {
14. struct node* p=(struct node*)malloc(sizeof(struct node));
15. for(int i=0;i<26;i++)
16. p->next[i]=NULL;
17. p->count=0;
18. return p;
19. }
20. void insert()
21. {
22. struct node*p=root;
23. int len=strlen(str);
24. for(int i=0;i<len;i++)
25. {
26. if(p->next[str[i]-'a']==NULL)              //当一个结点为空的时候,就新建立一个节点
27. p->next[str[i]-'a']=newnode();
28. p=p->next[str[i]-'a'];
29. }
30. p->count++;                          //当一个单词输入结束的时候,就让最后结点中的count值加1
31. if(p->count>max)
32. {
33. max=p->count;
34. strcpy(ans,str);
35. }
36. }
37. int main()
38. {
39. int icase;
40. scanf("%d",&icase);
41. root=newnode();
42. max=0;
43. while(icase--)
44. {
45. scanf("%s",str);
46. insert();
47. }
48. printf("%s %d\n",ans,max);
49. return 0;
50. }




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值