查找文章的热词(出现最多的次数)
**描述:**输入文章由标题和正文组成,并使用“”作为分隔符,标题出现的单词,系数为3,正文出现的单词系数为1。如果词语出现的词频系数相同,在标题中出现的频率次数高的排在前面,如果仍然相同,则按照词语在标题中出现的先后顺序进行排序,如果还相同,则按照词语在正文中的先后顺序进行排序。
输入:
第一行输入为正整数topN,和文章数M。每篇文章被处理为标题和正文两行。
3 2
标题1
正文1
标题2
正文2
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
//新建结构体
struct Node
{
string s;
int times;
int titleTimes;
int articleTimes;
};
//将整个字符串拆分成字符串单词数组
vector<string> getwords(string str) {
vector<string>res;
stringstream ss(str);
string temp;
while (ss >> temp) {
res.push_back(temp);
}
return res;
}
//**运算符重载**
bool operator < (const Node& s1, const Node& s2) {
if (s1.times == s2.times && s1.titleTimes == s2.titleTimes) {
return s1.articleTimes > s2.articleTimes;
}
if (s1.times == s2.times) {
return s1.titleTimes > s2.titleTimes;
}
return s1.times > s2.times;
}
int main() {
int topN, m;
cin >> topN >> m;
string str1;
string str2;
unordered_map<string, Node>mp;
//清除缓存区的字符
getchar();
int flag1 = 1;
int flag2 = 1;
while (m--) {
//读取标题中的字符串
getline(cin, str1);
vector<string>words;
words = getwords(str1);
for (string s : words) {
if (mp.find(s) != mp.end()) {
mp[s].times = mp[s].times + 3;
}
else {
mp[s].s = s;
mp[s].times = 3;
mp[s].articleTimes = flag1++;
}
}
//读取正文中的字符串
getline(cin, str2);
words.clear();
words = getwords(str2);
for (string s : words) {
if (mp.find(s) != mp.end()) {
mp[s].times = mp[s].times + 1;
}
else {
mp[s].s = s;
mp[s].times = 1;
mp[s].articleTimes = flag2++;
}
}
}
vector<Node>sortNode;
for (auto iter = mp.begin(); iter != mp.end(); iter++) {
sortNode.push_back(iter->second);
}
//排序
sort(sortNode.begin(), sortNode.end());
for (int i = 0; i < topN-1; i++) {
cout << sortNode[i].s << ' ';
}
cout << sortNode[topN - 1].s;
return 0;
}