動物統計加強版
時間限制: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 <cstdio> #include <stdlib.h> #include <iostream> #include <cstring> using namespace std; char ch[4000100],ch1[4000100]; int a[4000100]; //RS哈希 unsigned int RSHash(char *str) { unsigned int b = 378551; unsigned int a = 63689; unsigned int Hash = 0; while (*str) { Hash = Hash * a + (*str++); a *= b; } return (Hash & 0x7FFFFFFF); } int main() { int n, i, j, Max; scanf("%d",&n); memset(a, 0, sizeof (a)); Max = 0; for (i = 0; i < n; i++) { scanf("%s",ch); a[RSHash(ch)%4000000]++; if (a[RSHash(ch)%4000000] > Max) { Max = a[RSHash(ch)%4000000]; strcpy(ch1,ch); } } printf("%s %d\n",ch1,Max); }