题解 | 小红书推荐系统

小红书推荐系统

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

#include<bits/stdc++.h>
using namespace std;

using ll=long long;
using ull=unsigned long long;
using i128=__int128_t;
using u128=__uint128_t;
using ld=long double;

map<string,int>m;

bool cmp(const pair<string,int>& a, const pair<string,int>& b)
{
    if(a.second == b.second) return a.first < b.first;
    return a.second > b.second;
}

void solve()
{
    string s;
    getline(cin,s);
    // 处理输入字符串
    stringstream ss(s);
    string word;
    while(ss >> word)
    {
        m[word]++;
    }
    // 将map内容复制到vector中排序
    vector<pair<string,int>> vec(m.begin(), m.end());
    sort(vec.begin(), vec.end(), cmp);
    
    // 输出出现次数>=3的单词
    for(auto& p : vec)
    {
        if(p.second >= 3) 
        cout << p.first << '\n';
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t=1;
    //cin >> t;
    
    while(t--)
    {
        solve();
    }
    return 0;
}

全部评论

相关推荐

03-04 07:14
门头沟学院 C++
黑皮白袜臭脚体育生:老板:都给工作机会了还想要工资,哪来这么多好事
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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