_纵拥千千晚星 2023-02-02 17:58 采纳率: 100%
浏览 71
已结题

关于C++的词汇统计

输入输出怎么写,希望大家帮忙补充。
C++词汇统计
把所有单词/短语汇总在一起,找出出现次数最多的那个。
输入是一行,为所有单词,由空格隔开。最多有100000个单词。
输出一个出现次数最多的单词,如有相同次数,输出字典序靠前的。
输入样例:
dangran nage nage haode dangran qiche nage youdaoli youyisi nage nihao henhao henxianran verygood
输出样例:
nage

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int big=0;
    map<string,int>::iterator mit;
    for(mit=d.begin();mit!=d.end();mit++)
    {
        int cnt=mit->second;
        if(cnt>big)
        {
            big=cnt
            ans=mit->first;
        }
    } 
    return 0;
}

  • 写回答

3条回答 默认 最新

  • CodeBytes 2023-02-02 18:51
    关注

    “该回答引用ChatGPT”
    请参考下面代码,谢谢:

    #include <iostream>
    #include <map>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    int main() {
        map<string, int> words;
        string word;
        while (cin >> word) {
            ++words[word];
        }
    
        string res;
        int max_count = 0;
        for (auto item : words) {
            if (item.second > max_count) {
                max_count = item.second;
                res = item.first;
            } else if (item.second == max_count) {
                if (item.first < res) res = item.first;
            }
        }
    
        cout << res << endl;
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月6日
  • 已采纳回答 2月2日
  • 修改了问题 2月2日
  • 创建了问题 2月2日

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测