动物统计加强版

时间限制:3000 ms  |  内存限制:150000 KB
难度:4
 
描述
在美丽大兴安岭原始森林中存在数量繁多的物种,在勘察员带来的各种动物资料中有未统计数量的原始动物的名单。科学家想判断这片森林中哪种动物的数量最多,但是由于数据太过庞大,科学家终于忍受不了,想请聪明如你的ACMer来帮忙。
 
输入
第一行输入动物名字的数量N(1<= N <= 4000000),接下来的N行输入N个字符串表示动物的名字(字符串的长度不超过10,字符串全为小写字母,并且只有一组测试数据)。
输出
输出这些动物中最多的动物的名字与数量,并用空格隔开(数据保证最多的动物不会出现两种以上)。
样例输入
10
boar
pig
sheep
gazelle
sheep
sheep
alpaca
alpaca
marmot
mole
样例输出
sheep 3
 1 /* 功能Function Description:     字典树
 2    开发环境Environment:          DEV C++ 4.9.9.1
 3    技术特点Technique:
 4    版本Version:
 5    作者Author:                   可笑痴狂
 6    日期Date:                      20120810
 7    备注Notes:
 8 */
 9 #include<stdio.h>
10 #include<string.h>
11 #include<malloc.h>
12 
13 typedef struct node
14 {
15     int sum;
16     struct node *next[26];
17 }node;
18 //node memory[1000000];
19 //int k;
20 
21 
22 int search (char *s,node *T)
23 {
24     int i,j,id;
25     node *p,*q;
26     p=T;
27     i=0;
28     while(s[i])
29     {
30         id=s[i]-'a';
31         if(p->next[id]==NULL)
32         {
33         //    q=&memory[k++];
34             q=(node *)malloc(sizeof(node));
35             q->sum=0;
36             for(j=0;j<26;++j)
37                 q->next[j]=NULL;
38             p->next[id]=q;
39         }
40         p=p->next[id];
41         ++i;
42     }
43     (p->sum)++;
44     return p->sum;
45 }
46 
47 int main()
48 {
49     int n,i,num,max;
50     char temp[50];
51     char name[50];
52     while(scanf("%d",&n)!=EOF)
53     {
54         node *T;
55     //    k=0;
56     //    T=&memory[k++];
57         T=(node *)malloc(sizeof(node));
58         T->sum=0;
59         for(i=0;i<26;++i)
60             T->next[i]=NULL;
61         max=0;
62         while(n--)
63         {
64             scanf("%s",temp);
65             num=search(temp,T);
66             if(num>max)
67             {
68                 max=num;
69                 strcpy(name,temp);
70             }
71         }
72         printf("%s %d\n",name,max);
73     }
74     return 0;
75 }

 


posted on 2012-08-10 09:28  可笑痴狂  阅读(532)  评论(0编辑  收藏  举报