HotTopicOfWeiBo

本文最后更新于:2021年1月2日 凌晨

Hot Topic Of Wei Bo

emm! 这是一篇水文,主要是debug太难受了,所以我决定记录下这个错误!

原题–PTA 新浪微博热门话题(30分)

其实我主要是感觉题目说的有点不明不白的!

最后给几个大佬的测试用例,就明白了!

1
2
3
4
5
6
7
8
9
10
11
Input
4
This is a #test of topic#.
Another #Test of topic.#
This is a #Hot# #Hot# topic
Another #hot!# #Hot# topic

Output
Hot
2
And 1 more ...
1
2
3
4
5
6
7
8
9
10
Input
4
This is a #test of 1 topic#.
Another #Test of (1)topic.#
This is a #Hot# topic
This is a test of 1 topic

Output
Test of 1 topic
2
1
2
3
4
5
6
7
8
9
10
Input
3
Test #for@ diff words#
Test #ford iff words#
#more than# one #topics.#

Output
For diff words
1
And 3 more…

最后贴个通过代码!✔️

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# include <bits/stdc++.h>
using namespace std;

bool cmp(string str1, string str2)
{
for (int i = 0; i <= str1.length() - 1 && i <= str2.length() - 1; ++i) {
if (str1[i] < str2[i]) {
return true;
} else if (str1[i] > str2[i]) {
return false;
}
}
if (str1.length() < str2.length()) {
return true;
}
return false;
}

int main()
{
int n;
scanf("%d\n", &n);
//cin >> n;
map<string, int> HashMap;
string str;
while (n > 0) {
-- n;
string forStr = ".";
string curStr;
bool start = false;
bool end = false;
bool blankEnd = false;
while (cin.peek() != '\n') {
char ch = cin.get();
if (ch == '#') {
if (!start) {
start = true;
} else {
end = true;
start = false;
}
} else if (start && isalnum(ch)) {
curStr += tolower(ch);
blankEnd = false;
} else if (start) {
if (!blankEnd && curStr.length() > 0) {
curStr += ' ';
blankEnd = true;
}
}
if (end && isalpha(curStr[0])) {
curStr[0] = toupper(curStr[0]);
}
if (end && curStr != forStr) {
if (blankEnd) {
curStr = curStr.substr(0, curStr.length() - 1);
}
end = false;
auto search = HashMap.find(curStr);
if (search != HashMap.end()) {
search->second += 1;
} else {
HashMap.insert(make_pair(curStr, 1));
}
forStr = curStr;
curStr = "";
}
}
cin.get();// 读取最后一个换行符
}

auto begin = HashMap.begin();
string res = begin->first;
int cnt = begin->second;
int nums = 0;
++ begin;
while (begin != HashMap.end()) {
if (begin->second > cnt) {
res = begin->first;
cnt = begin->second;
nums = 0;
} else if (begin->second == cnt) {
if (cmp(begin->first, res)) {
res = begin->first;
}
nums += 1;
}
++ begin;
}
cout << res << endl;
cout << cnt << endl;
if (nums != 0) {
cout << "And " << nums << " more ...";
}
return 0;
}

睡了,晚安!


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!