概述
#华为机试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;
}`
最后
以上就是温婉绿茶为你收集整理的查找舆情热词的全部内容,希望文章能够帮你解决查找舆情热词所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复