7-46 新浪微博热门话题 (30分)

新浪微博可以在发言中嵌入“话题”,即将发言中的话题文字写在一对“#”之间,就可以生成话题链接,点击链接可以看到有多少人在跟自己讨论相同或者相似的话题。新浪微博还会随时更新热门话题列表,并将最热门的话题放在醒目的位置推荐大家关注。

本题目要求实现一个简化的热门话题推荐功能,从大量英文(因为中文分词处理比较麻烦)微博中解析出话题,找出被最多条微博提到的话题。

输入格式:

输入说明:输入首先给出一个正整数N(≤),随后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 ...

flag标记#出现,只记录每对#之间的字符,对于非英文数字字符换成空格,开头结尾都不能有空格且词间空格只有一个。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <algorithm>

using namespace std;
char s[150],ans[150],ch;
int n,num,mnum;
map<string,int> mp;
int main() {
    scanf("%d",&n);
    getchar();
    for(int i = 0;i < n;i ++) {
        int c = 0,flag = 0;
        map<string,bool> tnum;
        while((ch = getchar()) != '\n') {
            if(flag == 1) {
                if(ch == '#') {
                    flag = 0;
                    while(c && s[c - 1] == ' ') c --;
                    s[c] = 0;
                    if(!c) continue;
                    c = 0;
                    if(!tnum[s]) {
                        mp[s] ++;
                        if(mp[s] > mnum) {
                            num = 1;
                            mnum = mp[s];
                            strcpy(ans,s);
                        }
                        else if(mp[s] == mnum) {
                            if(strcmp(s,ans) < 0) strcpy(ans,s);
                            num ++;
                        }
                    }
                    tnum[s] = true;
                }
                else if(isalnum(ch)) s[c ++] = tolower(ch);
                else if(c && s[c - 1] != ' ') s[c ++] = ' ';
            }
            else if(ch == '#') flag = 1;
        }
    }
    ans[0] = toupper(ans[0]);
    printf("%s\n%d\n",ans,mnum);
    if(num > 1) printf("And %d more ...",num - 1);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/8023spz/p/12298900.html