输入格式:
输入说明:输入首先给出一个正整数N(≤
输出格式:
第一行输出被最多条微博提到的话题,第二行输出其被提到的微博条数。如果这样的话题不唯一,则输出按字母序最小的话题,并在第三行输出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 ...
Ω
AC只是coding中的冰淇淋,debug才是coding的正餐。
——沃滋基硕德
本题满分30分,我已经拿到了20分,看来 大势已去,让我们看下一题 又是一些非常微小的细节所导致的错误。
通过仔细阅读题目,不难发现【注意】里面有一句非常显眼的话说要用 **一 个 空 格** 分隔单词。
So easy的啦~读到空格的时候把后面的分隔符全部读掉就完事了。
哦对,空格可能在开头 哦对,非英文字母和数字的符号也会分隔 哦对,空格后面还可能是结束符’#’
可以接受的啦,就差4分了呢~
。
。
。
Codeee
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<string, int> tc;
int main()
{
set<string> topics;
map<string, int> topics_cnt;
int n;
cin >> n;
getchar();
string str, topic;
for (int i = 0; i < n; ++i)
{
getline(cin, str);
for (int j = 0; j < str.size(); ++j)
if (str[j] == '#')
{
j += 1;
while (str[j] != '#')
{
if (isalnum(str[j]))
topic += str[j];
else //测试点2:非英文字母和数字符号也具有分词作用
{
while (!isalnum(str[++j]) && str[j] != '#');
if (str[j] != '#' && !topic.empty())
topic += ' ';
j -= 1;
}
++j;
}
transform(topic.begin(), topic.end(), topic.begin(), ::tolower);
topics.insert(topic);
topic = "";
}
for (auto &t: topics)
topics_cnt[t] += 1;
topics.clear();
}
vector<tc> cup(topics_cnt.begin(), topics_cnt.end());
sort(cup.begin(), cup.end(), [](tc &a, tc &b) { return a.second > b.second; });
int index = 0;
string hottest_topic = cup[0].first;
while (++index < cup.size() && cup[index].second == cup[0].second)
if (cup[index].first < hottest_topic)
hottest_topic = cup[index].first;
hottest_topic[0] = toupper(hottest_topic[0]);
cout << hottest_topic << endl << cup[0].second << endl;
if (index > 1) //测试点3
cout << "And " << index - 1 << " more ...";
}
∑ 几点说明
- 基本思路:一条微博中的话题用
set
存储可以避免重复计算,读完一条微博后将set
中的话题计数至map
中 sort
不能对map
进行排序,因此先将原map
放置于同类型pair
的vector
中,再进行排序,排序完找最小话题- 依然用
transform
函数将所有英文字母转换为小写,最后将首字母转换为大写 - 这里用到的
isalnum
函数用来判断字符是否为字母或数字
函数 | 作用 |
islower(char c) | 是否为小写字母 |
isupper(char c) | 是否为大写字母 |
isdigit(char c) | 是否为数字 |
isalpha(char c) | 是否为字母 |
isalnum(char c) | 是否为字母或者数字 |
toupper(char c) | 字母小写转大写 |
tolower(char c) | 字母大写转小写 |