题解 | 小红书推荐系统

小红书推荐系统

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

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <unordered_map>
#include <algorithm>
using namespace std;
int main()
{
    string input;
    getline(cin,input);

    istringstream iss(input);
    string word;
    unordered_map<string , int> FreqMap;
    while(iss >> word)
    {
        FreqMap[word]++;
    }
    vector<pair<string,int>> keywords;
    for(auto& entry : FreqMap)
    {
        if(entry.second >= 3) 
        {
            keywords.push_back(entry);
        }
    }
    sort(keywords.begin(),keywords.end(),[](const pair<string,int>& a , const pair<string,int>& b ){
        if(a.second != b.second)
        {
            return a.second > b.second;
        }
        else
        {
            return a.first < b.first ;
        }
    });

    for(auto& keyword : keywords)
    {
        cout << keyword.first << endl;
    }

    return 0;

}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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