PTA 7-18 新浪微博热门话题

来源:互联网 发布:香港电视台直播软件 编辑:程序博客网 时间:2024/06/05 00:11

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

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


MAP瞎暴

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <string>#include <map>using namespace std;map<string, int> vis;map<string, int> cnt;map<string, int>::iterator it;bool judge(char c) {    if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || '0' <= c && c <= '9' || c == ' ') return true;    return false;}int main() {    int n; scanf("%d", &n); getchar();    char str[1000];    string s;    for (int i = 1; i <= n; i++) {        gets(str);        bool flag = false;        vis.clear();        for (int i = 0; str[i]; i++) {            if (str[i] == '#' && flag == false) {                s.clear(); flag = true;            }            else if (str[i] != '#' && flag == true) {                if (!judge(str[i])) str[i] = ' ';                if ('A' <= str[i] && str[i] <= 'Z') str[i] += 32;                int len = s.size();                if (len > 0 && str[i] == ' ' && s[len - 1] == ' ') continue;                s.push_back(str[i]);            }            else if (str[i] == '#' && flag == true) {                string ss;                if (s[0] == ' ') for (int i = 1; i < s.size(); i++) ss.push_back(s[i]);                else if (s[s.size() - 1] == ' ') for (int i = 0; i < s.size() - 1; i++) ss.push_back(s[i]);                else ss = s;                //cout << ss << endl;                if (!vis[ss]) {                    vis[ss] = 1;                    cnt[ss]++;                }                flag = false;            }        }    }    int num = 0;    int res = 0;    string ans;    for (it = cnt.begin(); it != cnt.end(); it++) {        string x = it->first; int y = it->second;        if (y > res) {            num = 0;            res = y;            ans = x;        }        else if (y == res) num++;    }    ans[0] -= 32;    cout << ans << endl << res << endl;    if (num) cout << "And " << num << " more ..." << endl;}


原创粉丝点击