【小红书笔试题】小红书有一个推推荐系统

小红书有一个推荐系统,可以根据用户搜索的关键词推荐用户希望获取的内容。
现在给定小红的搜索记录(记录为分词后的结果),我们认为当一个单词出现的次数不少于3次时,该单词为“用户期望搜索的单词”,即称为关键词。请你根据小红的记录,输出小红的用户画像对应的所有关键词。
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M

输入描述:
一行字符串,仅由小写字母和空格组成。代表小红的搜索记录。
字符串长度不超过100000。

输出描述:
小红所有的关键词。每行输入一个。你需要按照搜索频次从高到低输出。频次相同的,你需要按字典序升序输出。

示例1
输入例子:
kou red game red ok who game red karaoke yukari kou red red nani kou can koukou ongakugame game
输出例子:
red
game
kou

#include <iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
struct mapcomparator {
    bool operator()(const std::pair<string, int>& a, const std::pair<string, int>& b) {
        if (a.second == b.second) {
            return a.first < b.first;
        }
        return a.second > b.second;
    }
};
int main() {
    string a;
    map<string, int> wmap;
    while (cin >> a) { // 注意 while 处理多个 case
        wmap[a] ++;
    }

    vector<std::pair<string,int>> sortMap(wmap.begin(), wmap.end());
    std::sort(sortMap.begin(), sortMap.end(), mapcomparator());
    auto it = sortMap.begin();
    while(it!=sortMap.end()){
        if(it->second > 2){
            cout<< it->first <<endl;
        }
        it++;
    }

}
// 64 位输出请用 printf("%lld")
全部评论

相关推荐

牛客907956396号:3-4k简称3块
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务