题解 | 小红书推荐系统
小红书推荐系统
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;
}
