PTA新浪微博热门话题(题面毒瘤)

这道题的题目描述非常容易让人误解

测试点1容易过掉 一般没有问题

测试点2会出现k等于0的情况 这个时候最后一行是不输出的(题目没有写清楚)

测试点3容易出现PE 实际上是中间的处理过程存在问题 这里的有相同分词举例来说明

Hello World == Hello+World == Hello++World

简单来说就是所有非英文、数字的字符都要被替换成空格 并且相邻空格要合并成一个空格

同时应当把末位的空格忽略掉 即样例中的

This is a #test of topic#.
Another #Test of topic.#

是相同的两个话题

其他的细节处理与本题的解题方法在代码注释里写的比较清楚了~~

总结一下这题就是题目描述比较搞心态

附上代码(C++):

#include <bits/stdc++.h>

int n;
std::map<std::string, int> mp;

bool check(char x) // 检验是否为英文字母
{
    if((x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z'))
        return true;
    return false;
}
int main() //本题主要对字符串进行处理和统计 题目描述很不清楚容易误解题意导致WA了很多次
{
    std::cin >> n;
    getchar(); //读掉末尾的空格
    while(n -- ) // 统计出所有话题的次数
    {
        std::string str, t, res;
        getline(std::cin, str);
        int cnt = 0;
        int l = -1, r = -1; //用来存话题的两端
        std::unordered_map<std::string, int> st; //用于查询本条微博是否已经记录过该话题 如果已经记录过就不可以重复添加了
        for(int i = 0; i < str.size(); i ++ )
        {
            if(str[i] == '#' && !(cnt & 1)) //记录读到第几个# 偶数存左端点 奇数存右端点
                l = i + 1, cnt ++;
            else if(str[i] == '#' && (cnt & 1))
            {
                r = i - 1, cnt ++;
                t = str.substr(l, r - l + 1); // 将原字符串复制给t进行处理
                std::string temp;
                for(int i = 0; i < t.size(); i ++ ) //处理字符串保留英文字母和数字 同时将其他字符都替换成空格 Hello World 等价于 Hello+World
                     if(check(t[i]))
                         temp += tolower(t[i]); //全部变为小写 方便后续判断与处理
                     else   if(isdigit(t[i])) temp += t[i];
                     else   temp += ' ';
                for(int i = 0; i < temp.size(); i ++ ) //这一步进行二次处理 把原来的多个空格合并成一个空格放入到mp中
                    if(temp[i] == ' ' && (res[res.size() - 1] == ' ' || i == temp.size() - 1))
                        continue;
                    else    res += temp[i];
                if(!st.count(res)) //st用处见22行
                    mp[res] ++; //如果当前微博还没有记录该话题 那么次数就加一
                st[res] ++;
                res = ""; // 每次读完一个话题都要清空一下res字符串 因为一条微博可能会包含多个话题
            }
        }
    }
    std::string res;
    int maxv = 0;
    for(auto item : mp) //遍历mp 找出被引用次数最多的字符串及其次数
    {
        std::string word = item.first;
        int tot = item.second;  
        if(tot > maxv)
            res = word, maxv = tot;
    }
    res[0] = toupper(res[0]); // 题目要求话题开头字母大写 其他均为小写
    std::cout << res << "\n" << maxv << "\n";
    int ans = 0;
    for(auto item : mp) //其他话题中与所输出的热门话题引用次数相同的个数 
    {
        std::string word = item.first;
        int tot = item.second;  
        if(tot == maxv)
            ans ++;
    }
    ans --; // 减掉1 因为上面运算出来的ans包含自身
    if(ans) //测试数据毒瘤 若ans为0则不输出最后一行
        std::cout << "And " << ans << " more ...";
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值