nyoj 290 动物统计加强版

                                            动物统计加强版

                                         时间限制: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

代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char str[12];
int max=0;
struct node
{
	int count;
	struct node *next[26];
};
struct node *head;
void init()
{
	int i;
	head=(struct node *)malloc(sizeof(struct node));
	head->count=0;
	for(i=0;i<26;i++)
		head->next[i]=NULL;
}
void create(char *a)
{
	int i,j,n;
	struct node *p,*q=head;
	n=strlen(a);
	for(i=0;i<n;i++)
	{
		if(q->next[a[i]-'a']==NULL)
		{
			p=(struct node *)malloc(sizeof(struct node));
			q->next[a[i]-'a']=p;
			q=q->next[a[i]-'a'];
			q->count=0;
			for(j=0;j<26;j++)
				q->next[j]=NULL;
		}
		else
		{
			q=q->next[a[i]-'a'];
		}
	}
	q->count++;
	if(max<q->count)
	{
		max=q->count;
		strcpy(str,a);
	}
}


int main()
{
	int n;
	char name[12];
	scanf("%d",&n);
	init();
	while(n--)
	{
        scanf("%s",&name);      
		create(name);
	}
	printf("%s %d\n",str,max);
	return 0;
}
		
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章