Nanyang OJ Nyoj Animal Statistics Enhanced Edition data Structure topic 290

Source: Internet
Author: User



/* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.