题解 | 小红书推荐系统

小红书推荐系统

https://www.nowcoder.com/practice/e5b39c9034a84bf2a5e026b2b9b973d0

C++ 用到了unordered_map、pair、sort lambda表达式

#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
    unordered_map<string, int> keywords;
    string word;
    while (cin >> word) {
        keywords[word]++;
    }
    vector<pair<string, int>> words(keywords.begin(), keywords.end());
    sort(words.begin(), words.end(), [](const auto &a, const auto &b) {
        return a.second==b.second ? a.first<b.first : a.second>b.second;
    });
    for (auto i : words) {
        if (i.second<3) break;
        cout << i.first << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

UtopianYou...:这个简历排版真的不太行哦,去找免费的或者花点小钱,把排版弄整齐一点吧,看着舒服。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务