新浪微博可以在发言中嵌入“话题”,即将发言中的话题文字写在一对“#”之间,就可以生成话题链接,点击链接可以看到有多少人在跟自己讨论相同或者相似的话题。新浪微博还会随时更新热门话题列表,并将最热门的话题放在醒目的位置推荐大家关注。
本题目要求实现一个简化的热门话题推荐功能,从大量英文(因为中文分词处理比较麻烦)微博中解析出话题,找出被最多条微博提到的话题。
输入格式:
输入说明:输入首先给出一个正整数N(≤10
5
),随后N行,每行给出一条英文微博,其长度不超过140个字符。任何包含在一对最近的#中的内容均被认为是一个话题,输入保证#成对出现。
输出格式:
第一行输出被最多条微博提到的话题,第二行输出其被提到的微博条数。如果这样的话题不唯一,则输出按字母序最小的话题,并在第三行输出And k more …,其中k是另外几条热门话题的条数。输入保证至少存在一条话题。
注意:两条话题被认为是相同的,如果在去掉所有非英文字母和数字的符号、并忽略大小写区别后,它们是相同的字符串;同时它们有完全相同的分词。输出时除首字母大写外,只保留小写英文字母和数字,并用一个空格分隔原文中的单词。
输入样例:
4
This is a #test of topic#.
Another #Test of topic.#
This is a #Hot# #Hot# topic
Another #hot!# #Hot# topic
输出样例:
Hot
2
And 1 more …
参考了某佬的做法%%%
传送门:https:///qq_48508278/article/details/119637352
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#pragma warning(disable:4996)
using namespace std;
const int N = 1e5 + 10;
typedef long long LL;
int a[N];
map<string, int>mp;
set<string>st[N];
void duru(string s,int x) {
int flag = 0;
int count = 0;
string word = "";
string s1="";
s += ".";
for (int i = 0; i < s.size(); i++) {
if (s[i] == '#') {
count++;
flag = 1;
if (i < s.size() - 1)
i++;
}
if(isalpha(s[i])&&flag||isdigit(s[i])&&flag){
if (isdigit(s[i]))
word += s[i];
if (isalpha(s[i]))
word += tolower(s[i]);
}
else if(flag){
if (word != " ")
s1 += word;
word = " ";
}
if (count == 2) {
flag = 0;
count = 0;
st[x].insert(s1);
s1 = "";
word = "";
}
}
}
int main() {
int n;
cin >> n;
getchar();
for (int i = 0; i < n; i++) {
char s[150];
cin.getline(s,150 );
duru(s, i);
for (auto it:st[i]) {
mp[it] += 1;
}
}
int maxnum = 0;
for (auto it : mp) {
if (it.second > maxnum)maxnum = it.second;
}
int res = 0;
for (map<string, int>::iterator it = mp.begin(); it != mp.end();it++) {
if (it->second == maxnum) {
string str = it->first;
str[0] = str[0] - 32;
cout << str << endl;
cout << maxnum << endl;
break;
}
}
for (auto i : mp) {
if (i.second == maxnum)res++;
}
if (res > 1)
cout << "And " << res-1 << " more ...";
}