4675: 【GESP2503三级】词频统计

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

题目描述

在⽂本处理中,统计单词出现的频率是一个常见的任务。现在,给定 个单词,你需要找出其中出现次数最多的单
词。在本题中,忽略单词中字母的大小写(即 AppleappleAPPLEaPPle 等均视为同一个单词)。
请你编写一个程序,输入$n$ 个单词,输出其中出现次数最多的单词。

输入

第一行,一个整数$n$ ,表示单词的个数;
接下来$n$ 行,每行包含一个单词,单词由大小写英文字母组成。
输入保证,出现次数最多的单词只会有一个。

输出

输出一行,包含出现次数最多的单词(输出单词为小写形式)。

样例输入 复制

6
Apple
banana
apple
Orange
banana
apple

样例输出 复制

apple

提示

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n; 
	cin >> n; 
    map<string, int> cnt;
    int mx = -1;
    for (int i = 1; i <= n; i ++) {
        string s; 
		cin >> s;
        for(int j=0;j<s.length();j++)
        	if(s[j]>='A'&&s[j]<='Z')
        	    s[j]+=32;
        if (! cnt.count(s))
            cnt [ s ] = 0;
        mx = max(mx, ++cnt[ s ]);
    }
    int mx_num = 0;
    map<string, int>::iterator it;
    for (it = cnt.begin(); it != cnt.end(); it++)
        if ((it->second) == mx) {
            cout << (it->first) << '\n';
            mx_num ++;
        }
    return 0;
}