您的位置:首页 > 其它

NYOJ290,动物统计加强版HashMap

2014-11-04 12:07 232 查看


动物统计加强版

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


来源[陈玉 张云聪]原创

//package NYOJ;

import java.util.HashMap;
import java.util.Scanner;

public class Main{

public static void main(String[] args) {

Scanner cin = new Scanner(System.in);
HashMap<String, Integer> d = new HashMap<String, Integer>();
int n = cin.nextInt();
String smax = null;
String s = null;
int max = 1;
while (n-- > 0) {
s = cin.next();
if (d.get(s) == null) {
d.put(s, 1);
} else {
int temp = d.get(s) + 1;
d.put(s, temp);
if (max < temp) {
max = temp;
smax = s;
}
}
}
if (smax==null) System.out.println(s+" "+max);
else System.out.println(smax+" "+max);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: