Skip to content

Commit 395def7

Browse files
committed
update 7-48, add 7-46
1 parent 285b220 commit 395def7

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <unordered_map>
4+
#include <unordered_set>
5+
#include <algorithm>
6+
#include <vector>
7+
using namespace std;
8+
9+
string &Trim(string &s) {
10+
if (s.empty()) return s;
11+
s.erase(0, s.find_first_not_of(" "));
12+
s.erase(s.find_last_not_of(" ") + 1);
13+
return s;
14+
}
15+
16+
string &RemoveExtraSpace(string &s) {
17+
string result = "";
18+
for (int i = 0; s[i] != '\0'; i++) {
19+
if (s[i] != ' ') result += s[i];
20+
else {
21+
if (s[i + 1] != ' ')
22+
result += s[i];
23+
}
24+
}
25+
26+
s = result;
27+
return s;
28+
}
29+
30+
int main() {
31+
int n;
32+
string weibo;
33+
unordered_map<string, unordered_set<int>> topicMapWeiboIndexSet;
34+
cin >> n;
35+
cin.ignore();
36+
for (int i = 0; i < n; i++) {
37+
getline(cin, weibo);
38+
39+
string topic = "";
40+
bool recording = false;
41+
42+
for (const char &c : weibo) {
43+
if (c == '#') {
44+
recording = !recording;
45+
if (!recording) {
46+
if (topic != "") {
47+
string processedTopic = RemoveExtraSpace(Trim(topic));
48+
topicMapWeiboIndexSet[processedTopic].insert(i);
49+
topic = "";
50+
}
51+
}
52+
continue;
53+
}
54+
55+
if (!recording) continue;
56+
57+
// only keep a-z, A-Z, 0-9, replace others with space
58+
char target;
59+
if (c >= 'A' && c <= 'Z') target = c - 'A' + 'a';
60+
else if (c >= 'a' && c <= 'z') target = c;
61+
else if (c >= '0' && c <= '9') target = c;
62+
else target = ' ';
63+
topic += target;
64+
}
65+
}
66+
67+
// post process, push <topic, count> to vector, sort by count, topic order
68+
vector<pair<string, size_t>> topicCount;
69+
for (auto &elem : topicMapWeiboIndexSet) {
70+
topicCount.push_back({elem.first, elem.second.size()});
71+
}
72+
73+
sort(topicCount.begin(), topicCount.end(), [&](const pair<string, size_t> a, const pair<string, size_t> b) {
74+
if (a.second == b.second) return a.first < b.first;
75+
else return a.second > b.second;
76+
});
77+
78+
// output for hottest topic
79+
pair<string, size_t> &hottest = topicCount[0];
80+
if (hottest.first[0] >= 'a' && hottest.first[0] <= 'z') {
81+
cout << (char)(hottest.first[0] - 'a' + 'A');
82+
} else {
83+
cout << hottest.first[0];
84+
}
85+
for (int i = 1; i < hottest.first.size(); i++) {
86+
cout << hottest.first[i];
87+
}
88+
cout << endl;
89+
cout << hottest.second << endl;
90+
91+
// check same count with hottest topic
92+
int sameCount = 0;
93+
for (int i = 1; i < topicCount.size(); i++) {
94+
if (topicCount[i].second == hottest.second)
95+
sameCount++;
96+
}
97+
if (sameCount > 0) cout << "And " << sameCount << " more ..." << endl;
98+
99+
return 0;
100+
}

数据结构与算法题目集(中文)/7-48_银行排队问题之单窗口“夹塞”版 (30).cpp

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Customer {
1414
};
1515

1616
int main() {
17+
ios::sync_with_stdio(false);
1718
string name;
1819
int n, m, k;
1920
cin >> n >> m;

0 commit comments

Comments
 (0)