/* Animal Statistics Enhanced Edition
Time limit: Ms | Memory Limit: 150000 KB
Difficulty: 4
Describe the existence of a large number of species in the pristine forest of the beautiful Daxing ' anling, with an unspecified number of primitive animals in the various animal materials brought by surveyors. Scientists want to determine which animal is the most in the forest, but because the data is too large, scientists can not bear it, want to be smart as your acmer to help.
Enter the first line to enter the number of animal names N (1<= n <= 4000000), and the next n lines to enter n strings to represent the animal's name (the length of the string is not more than 10, the string is all lowercase, and there is only one set of test data).
Output output the names and numbers of the most animals in these animals, separated by a space (the most data-guaranteed animals will not appear in more than two types).
Sample Input 10
Boar
Pig
Sheep
Gazelle
Sheep
Sheep
Alpaca
Alpaca
Marmot
Mole
Sample Output Sheep 3
SOURCE [Chenyu Zhang Yunzun] Original uploader Chenyu */
#include <stdio.h>
#include <string.h>
#include <malloc.h>
struct node{
int number;
struct node *next[26];
};//Structural Body
struct node *root;
Char ch[11];//globally valid
int max=0;//global variable
struct node *newset ()//struct pointer type function is similar to initialization
{
struct node *p;
int i;
p= (struct node *) malloc (sizeof (struct node));//Allocate space
for (I=0;i<26;++i)
{
p->next[i]=null;
}
p->number=0;
return p;
}
void Insert (char *s)//Insert character array
{
struct node *p;
int len,i;
P=root;
Len=strlen (s);
for (I=0;i<len;++i)
{
if (p->next[s[i]-' a ']==null)
p->next[s[i]-' a ']=newset ();//If it's a new letter, create a new one.
P=p->next[s[i]-' a '];
}
p->number++;//Records
if (P->number>max)
{
max=p->number;
strcpy (ch,s);//copy function
}
}
int main ()
{
int T;
Char a[11];
scanf ("%d", &t);
Root=newset ();//initialization
while (t--)
{
scanf ("%s", a);
Insert (a);
}
printf ("%s%d\n", Ch,max);
return 0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Nanyang OJ Nyoj Animal Statistics Enhanced Edition data Structure topic 290