NYOJ 290 动物统计加强版

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<malloc.h>
 4 #define N 26
 5 char s[11];
 6 int max;
 7 
 8 typedef struct Trie{  
 9     Trie *next[N];  //含有26个结点指针(a,b,c.......z)
10     int count;
11 }*Node;
12 
13 Node root;
14 
15 Node creat()
16 {
17     Node p = (struct Trie*) malloc(sizeof(struct Trie));
18     for(int i = 0; i < N; ++i)
19         p->next[i] = NULL;
20     p->count = 0;
21     return p;
22 }
23 
24 void insertNode(char *str)
25 {
26     Node p = root;
27     int i, len = strlen(str);
28     for(i = 0; i < len; ++i)
29     {
30         if(p->next[ str[i] - 'a' ] == NULL)
31             p->next[ str[i] - 'a' ] = creat();
32         p = p->next[ str[i] - 'a' ];
33     }
34     ++p->count;  //记录该单词出现的个数
35     if(max < p->count)
36     {
37         max = p->count;
38         strcpy(s,str);
39     }
40 }
41 
42 int main()
43 {
44 //    freopen("in.txt","r",stdin);
45     int n;
46     char a[11];
47     root = creat();
48     max = 0;
49     scanf("%d",&n);
50     while(n--)
51     {
52         scanf("%s",a);
53         insertNode(a);
54     }
55     printf("%s %d\n",s,max);
56     return 0;
57 }
原文地址:https://www.cnblogs.com/yaling/p/3015650.html