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();
}
}