nyoj-290-动物统计加强版

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<math.h>
 5 using namespace std;
 6 struct mu
 7 {
 8     int num;
 9     struct mu *next[26];
10 };
11 struct mu *root;
12 struct mu *bulid();
13 int save(char a[]);
14 int main()
15 {
16     int n,max=0;
17     char anm[15],a[15];
18     root=bulid();
19     scanf("%d",&n);
20     getchar();
21     while(n--)
22     {
23         gets(a);
24         int temp=save(a);
25         if(temp>max)
26         {
27             max=temp;
28             strcpy(anm,a);
29         }
30     }
31     printf("%s %d
",anm,max);
32     return 0;
33 }
34 struct mu *bulid()
35 {
36     struct mu *p;
37     p=(struct mu*)malloc(sizeof(struct mu));
38     for(int i=0;i<26;i++)
39         p->next[i]=NULL;
40     p->num=0;
41     return p;
42 }
43 int save(char a[])
44 {
45     struct mu *p;
46     p=root;
47     int len=strlen(a);
48     for(int i=0;i<len;i++)
49     {
50         if(p->next[a[i]-'a']!=NULL)
51             p=p->next[a[i]-'a'];
52         else
53         {
54             p->next[a[i]-'a']=bulid();
55             p=p->next[a[i]-'a'];
56         }
57     }
58     p->num++;
59     return p->num;
60 }
原文地址:https://www.cnblogs.com/nylg-haozi/p/3196248.html