新浪微博热门话题 (30 分)
新浪微博可以在发言中嵌入“话题”,即将发言中的话题文字写在一对“#”之间,就可以生成话题链接,点击链接可以看到有多少人在跟自己讨论相同或者相似的话题。新浪微博还会随时更新热门话题列表,并将最热门的话题放在醒目的位置推荐大家关注。
本题目要求实现一个简化的热门话题推荐功能,从大量英文(因为中文分词处理比较麻烦)微博中解析出话题,找出被最多条微博提到的话题。
输入格式:
输入说明:输入首先给出一个正整数N(≤105 ),随后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 ...
我的代码:
#include <iostream>
#include <map>
using namespace std;
int main(){
int n;
cin>>n;
map<string,int> m;
getchar();
int max=0;
while(n--){
string str;
getline(cin,str);
string hot[100];int hoti=0;
for(int i=0;i<str.size();i++){
if(str[i]=='#'&&i<str.size()){
string a="";
i++;
while(str[i]!='#'&&i<str.size()){
if(str[i]>='A'&&str[i]<='Z'){
str[i]='a'+(str[i]-'A');
}
if((str[i]>='a'&&str[i]<='z')||(str[i]>='0'&&str[i]<='9')||str[i]==' '){
a+=str[i];
}
else{
str[i]=' ';
a+=str[i];
}//其它字符变为空格实现
i++;
}
int k=a.size()-1;
while(a[k]==' '){
a=a.substr(0,a.size()-1);//如果最后一个字符是空格 则删除
k--;
}
int flag=0;
for(int j=0;j<hoti;j++){
if(a==hot[j]){
flag=1;
}
}//判断是否是同一条微博里多次出现的热词
if(flag==0){
m[a]++;
hot[hoti++]=a;
}
if(m[a]>max)
max=m[a];
}
}
}
int num=0;
for(auto it=m.begin();it!=m.end();it++){
if(max==it->second){
int first=0;
string str=it->first;
for(int i=0;i<str.size();i++){
if(first==0&&str[i]>='a'&&str[i]<='z'){
cout<<char(str[i]-32);
first=1;
continue;
}
else if(str[i]==' '){
cout<<" ";
while(str[i]==' '){
i++;
}
}
cout<<str[i];
}
cout<<endl<<max<<endl;
it++;
while(it!=m.end()){
if(it->second==max)
num++;
it++;
}
if(num!=0)
cout<<"And "<<num<<" more ..."<<endl;
break;
}
}
return 0;
}