洛谷 B4262:[GESP202503 三级] 词频统计 ← STL map

【题目来源】
https://www.luogu.com.cn/problem/B4262

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

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

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

【输入样例】
6
Apple
banana
apple
Orange
banana
apple

【输出样例】
apple

【说明/提示】
对于所有测试点,
1≤n≤100,每个单词的长度不超过 30,且仅由大小写字母组成。

【算法分析】
●​​​​​​​ 小写字母的 ASCII 码值,比对应的大写字母的 ASCII 值大 32。
●​​​​​​​ 本代码用 STL map 实现:
https://blog.csdn.net/hnjzsyjyj/article/details/146118701

【算法代码:STL map

#include
using namespace std;

map mp;
int cnt=INT_MIN;
string ans;
int n;

int main() {
    cin>>n;
    while(n--) {
        string s;
        cin>>s;
        for(int i=0; i='A' && s[i]<='Z') s[i]+=32;
        }

        mp[s]++;
        if(mp[s]>cnt) {
            cnt=mp[s];
            ans=s;
        }
    }

    cout<





【参考文献】
https://www.luogu.com.cn/problem/solution/B4262
https://blog.csdn.net/m0_65608962/article/details/146569264
https://www.bilibili.com/opus/1047539319500177428
 

你可能感兴趣的:(信息学竞赛,#,STL标准库,STL,map)