Animal statistics enhanced version time limit: 3000 MS | memory limit: 150000 KB difficulty: 4
-
Description
-
There are a large number of species in the virgin forest of the beautiful daxing'anling district. The list of unrecorded primitive animals is included in the animal data provided by the surveyors. Scientists want to determine which animal in the forest has the largest number, but because the data is too large, scientists can't stand it at last. They want to ask your ACMer for help.
-
Input
-
In the first line, enter the number of animal names N (1 <= N <= 4000000). In the next N lines, enter N strings to indicate the name of the animal (the length of the string cannot exceed 10, all strings are lowercase letters and only one group of test data is available ).
-
Output
-
Output the names and quantities of the most animals among them, and separate them with spaces (data ensures that there are no more than two types of animals ).
-
Sample Input
-
10boarpigsheepgazellesheepsheepalpacaalpacamarmotmole
-
Sample output
-
sheep 3
#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct node{int count;struct node *next[26];}tree;tree *head;char ch[12];int num;void init(){int i;head=(tree*)malloc(sizeof(tree));for(i=0; i<26; i++){head->next[i]=NULL;}}void insert(char *s){int i,j;tree *q,*p=head;int len=strlen(s);for(i=0; i<len; i++){if(p->next[s[i]-'a']==NULL){q=(tree*)malloc(sizeof(tree));p->next[s[i]-'a']=q;p=p->next[s[i]-'a'];p->count=0;for(j=0; j<26; j++){p->next[j]=NULL;}}else{p=p->next[s[i]-'a'];}}p->count++;if(num<p->count){num=p->count;strcpy(ch,s); }}int main(){int n;char s[12];scanf("%d",&n);num=0;init();while(n--){scanf("%s",s);insert(s);}printf("%s %d\n",ch,num);return 0;}