提问:
输入输出怎么写,希望大家帮忙补充。
C++词汇统计
把所有单词/短语汇总在一起,找出出现次数最多的那个。
输入是一行,为所有单词,由空格隔开。最多有100000个单词。
输出一个出现次数最多的单词,如有相同次数,输出字典序靠前的。
输入样例:
dangran nage nage haode dangran qiche nage youdaoli youyisi nage nihao henhao henxianran verygood
输出样例:
#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; }
解答:
请参考下面代码,谢谢:
#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; }
原文链接: https://www.cnblogs.com/dituirenwu/p/17087153.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/315525
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!