4321: 【基础】整理抽屉(1760)

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:8 解决:6

题目描述

期末考试即将来临,小T由于同时肩负了学习、竞赛、班团活动等多方面的任务,一直没有时间好好整理他的课桌抽屉,为了更好地复习,小T首先要把课桌抽屉里的书分类整理好。

小T的抽屉里堆着 lns="http://www.w3.org/1998/Math/MathML"> 本书,每本书的封面上都印有学科名称,学科名称用一个字符串表示,如语文学科的书封面上都印有“chinese”。现在,你的任务是帮助小T找出哪个学科的书最多?

输入

第一行包含一个自然数 lns="http://www.w3.org/1998/Math/MathML">lns="http://www.w3.org/1998/Math/MathML">01000)表示抽屉中书的总数。

接下来 lns="http://www.w3.org/1998/Math/MathML"> 行每行包含一本书的学科名称,学科名称是一个长度不超过 lns="http://www.w3.org/1998/Math/MathML">15 的由小写英文字母组成的字符串。

输出

仅有一行包含一个字符串,表示最多的那种书的学科名称。

数据保证答案一定是唯一的。

样例输入 复制

5
english
chinese
physics
chinese
chinese

样例输出 复制

chinese

提示

【样例解释】

小T课桌抽屉里共有 lns="http://www.w3.org/1998/Math/MathML">5 本书,其中有 lns="http://www.w3.org/1998/Math/MathML">3 本是语文学科的,英语学科和物理学科各有 lns="http://www.w3.org/1998/Math/MathML">1 本,所以最多的是语文学科的书,应输出“chinese”。

【数据范围】

lns="http://www.w3.org/1998/Math/MathML">30% 的数据满足:lns="http://www.w3.org/1998/Math/MathML">110,学科名称为长度不超过 lns="http://www.w3.org/1998/Math/MathML">2 的仅包含英文小写字母的字符串;

lns="http://www.w3.org/1998/Math/MathML">100% 的数据满足:lns="http://www.w3.org/1998/Math/MathML">11000,学科名称为长度不超过 lns="http://www.w3.org/1998/Math/MathML">15 的仅包含英文小写字母的字符串;

#include<bits/stdc++.h>
using namespace std;
struct node{
    string name;
    int count;
};
bool cmp(node x,node y) {
    if(x.count!=y.count) return x.count>y.count;
    else return x.name>y.name;
}
node a[1010];
map<string,int> m;
int n; 
int main() {
    cin>>n;
    string t;
    for(int i=1;i<=n;i++) {
        cin>>t;
        m[t]++;
    }
    map<string,int>::iterator it;
    int k=1;
    for(it=m.begin();it!=m.end();it++){
    	a[k].name=it->first;
    	a[k].count=it->second;
    	k++;
	}
    sort(a+1,a+k,cmp);
    cout<<a[1].name<<endl; 
    return 0;
}