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;
}
		
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章