1975: 動物統計加強版
時間限制: 3 Sec 內存限制: 128 MB
提交: 71 解決: 28
您該題的狀態:已完成
[提交][狀態][討論版]
題目描述
在美麗大興安嶺原始森林中存在數量繁多的物種,在勘察員帶來的各種動物資料中有未統計數量的原始動物的名單。科學家想判斷這片森林中哪種動物的數量最多,但是由於數據太過龐大,科學家終於忍受不了,想請聰明如你的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>
struct tTree {
int cnt;
struct tTree* next[26];
tTree() {
this->cnt = 0;
for( int i=0 ; i<26 ; i++ )
this->next[i] = NULL;
}
void clear(){
this->cnt = 0;
for( int i=0 ; i<26 ; i++ )
this->next[i] = NULL;
}
};
char name[11];
char ans[11];
int Max;
tTree *root = new tTree();
void insert( char *s ) {
int i,k;
tTree *T = root;
for( i=0 ; s[i] ; i++ ){
k = s[i] - 'a';
if( T->next[k]==NULL ){
T->next[k] = new tTree();
}
T = T->next[k];
}
T->cnt++;
if( T->cnt>Max ){
Max = T->cnt;
strcpy( ans,s );
}
}
int main() {
int n;
while( ~scanf( "%d",&n ) ){
Max = 0;
while( n-- ){
scanf( "%s",&name );
insert( name );
}
printf( "%s %d\n",ans,Max );
root->clear();
}
}