查找舆情热词

#华为机试2020.04.06第一题
查找舆情热词
参考代码:

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

unordered_map<string, int> frequency_in_title;
unordered_map<string, int> frequency_in_text;
unordered_map<string, int> first_in_title;
unordered_map<string, int> first_in_text;
int index_title = 1, index_article = 1;


struct word_info
{
    word_info(pair<string, int> f, int fre_in_text, int t, int a) : name(f.first), fre_in_title(f.second){
        fre_in_all = fre_in_title * 3 + fre_in_text;
        if (t == 0)  first_in_tit = INT32_MAX;
        if (a == 0)  first_in_text = INT32_MAX;
    };
    string name;
    int fre_in_title;
    int fre_in_all;
    int first_in_tit;
    int first_in_text;

};
bool cmp(word_info* a, word_info* b)
{

    if (a->fre_in_all != b->fre_in_all) return a->fre_in_all > b->fre_in_all;
    if (a->fre_in_title != b->fre_in_title) return a->fre_in_title > b->fre_in_title;
    if (a->first_in_tit != b->first_in_tit) return a->first_in_tit < b->first_in_tit;
    return a->first_in_text < b->first_in_text;
}
int main()
{
    int topN, M;
    cin >> topN >> M;
    string lineStr;
    // 换行
    getline(cin, lineStr);

    while (M--)
    {
        // 处理标题
        getline(cin, lineStr);
        stringstream ss(lineStr);
        string str;
        while (getline(ss, str, ' '))
        {
            frequency_in_title[str]++;
            if (first_in_title.count(str) == 0) first_in_title[str] = index_title++;
        }
        // 处理正文
        getline(cin, lineStr);
        ss = stringstream(lineStr);
        while (getline(ss, str, ' '))
        {
            frequency_in_text[str]++;
            if (first_in_title.count(str) == 0) first_in_title[str] = index_title++;
        }

    }
    vector<word_info*> words;
    // 存入结构体;
    for (auto word : frequency_in_title)
    {
        word_info* temp = new word_info(word, frequency_in_text[word.first], first_in_title[word.first], first_in_text[word.first]);
        words.push_back(temp);
    }
    //sort
    sort(words.begin(), words.end(), cmp);
    for (int i = 0; i < topN; i++) cout << words[i]->name << ' ';
    puts("");
    getchar();
    return 0;
}`
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值