7-20 奥运排行榜 (25分)(c++ STL)

整体代码是参考这位博主的(再次感谢这位博主,已经参考过他的好几篇代码了,再次推荐一下)
https://blog.csdn.net/xiang_6/article/details/78201197
但是在处理热门话题的时候 注意要将非字母和非数字的字符转化成空格
然后注意处理之后 话题的前面和末尾 可能还有空格
然后 两个分词之间只能有一个空格
map和set还有string太香了!

#include <iostream>
#include <cstdlib>
#include <string>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;
const int MAXNUM = 1e5 + 5;
int N;
//保证一个句子的热门话题
struct topic
{
    string name;
    int cnt;
}topics[MAXNUM];

set<string> cur_sentence_hot_topic;
set<string> all_hot_topic;
map <string, int> mp;

bool cmp(topic a, topic b)
{
    if (a.cnt == b.cnt) return a.name < b.name;
    return a.cnt > b.cnt;
}

void Handle(string sentence)
{
    //每次都要对句子的集合初始化
    cur_sentence_hot_topic.clear();
    int len = sentence.size();
    for (int i = 0; i < len; i++)
    {
        if (sentence[i] == '#')
        {
            int st = i + 1;
            int ed = st;
            //如果一个句子里面只有一个# —— 当作没有热点话题处理
            while (sentence[ed] != '#') ed++;
                //ed 保存的是第二个 #的前一个位置
            ed--;
            string handle_str;
            for (int j = st; j <= ed; j++)
            {
                if (isalpha(sentence[j]))
                    handle_str.push_back(tolower(sentence[j]));
                else if (isdigit(sentence[j]) || sentence[j] == ' ')
                    handle_str.push_back(sentence[j]);
                else
                    handle_str.push_back(' ');
            }
            //再次处理,应该是用来处理 空格的问题
            string res;
            bool flag = false;
            for (int j = 0; j < handle_str.size(); j++)
            {
                if (isalpha(handle_str[j]) || isdigit(handle_str[j]))
                {
                    flag = true;
                    res.push_back(handle_str[j]);
                }
                else if (handle_str[j] == ' ')
                {
                    if (flag == false) continue;
                    //若是第一个位置 则continue;
                    if (j == handle_str.size() - 1) continue;
                    else 
                    {
                        if (isalpha(handle_str[j + 1]) || isdigit(handle_str[j + 1]))
                            res.push_back(handle_str[j]);
                    }
                }
            }
            //cout << '[' << handle_str << ']' << endl;
            //cout << '{' << res << '}' << endl;
            cur_sentence_hot_topic.insert(res);
            //更新i读取的位置
            i = ed + 1;
        }//if
    }//for
    for (set<string>::iterator it = cur_sentence_hot_topic.begin(); it != cur_sentence_hot_topic.end(); it++)
    {
        //cout << "[" << *it << "]" << endl;
        mp[*it]++;
        all_hot_topic.insert(*it);
    }
}


void Input()
{
    string sentence;
    cin >> N; getchar();
    for (int i = 0; i < N; i++)
    {
        getline(cin, sentence);
        Handle(sentence);
    }
}

void Solve()
{
    int i = 0;
    for (set<string>::iterator it = all_hot_topic.begin(); it != all_hot_topic.end(); it++)
    {
        topics[i].name = *it;
        topics[i].cnt = mp[*it];
        i++;
    }
    sort(topics, topics + i, cmp);
    string ans1 = topics[0].name;
    int ans2 = topics[0].cnt;
    int ans3 = 0;
    for (int j = 1; j < i; j++)
    {
        if (topics[j].cnt == topics[0].cnt)
            ans3++;
        else break;
    }
    //ans1
    cout << (char)toupper(topics[0].name[0]);
    for (int i = 1; i < ans1.size(); i++)
        cout << topics[0].name[i];
    cout << endl;
    //ans2
    cout << ans2 << endl;
    //ans3
    if (ans3)
        printf("And %d more ...\n", ans3);

}
int main()
{
    //ios::sync_with_stdio(false);
    Input();
    Solve();
    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值