-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus_stops_3.cpp
169 lines (150 loc) · 3.48 KB
/
bus_stops_3.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <string>
#include <iostream>
#include <cassert>
#include <vector>
#include <map>
using namespace std;
enum class QueryType {
NewBus,
BusesForStop,
StopsForBus,
AllBuses
};
struct Query {
QueryType type;
string bus;
string stop;
vector<string> stops;
};
istream& operator >> (istream& is, Query& q) {
q.stops.clear();
q.stop.erase();
string command;
is >> command;
if (command == "NEW_BUS") {
size_t stop_count;
q.type = QueryType::NewBus;
is >> q.bus >> stop_count;
for (size_t i = 0; i < stop_count; i++) {
is >> q.stop;
q.stops.push_back(q.stop);
}
}
else if (command == "BUSES_FOR_STOP") {
q.type = QueryType::BusesForStop;
is >> q.stop;
}
else if (command == "STOPS_FOR_BUS") {
q.type = QueryType::StopsForBus;
is >> q.bus;
}
else if (command == "ALL_BUSES") {
q.type = QueryType::AllBuses;
}
else {
throw runtime_error("Bad command");
}
return is;
}
struct BusesForStopResponse {
string reply;
};
ostream& operator << (ostream& os, const BusesForStopResponse& r) {
os << r.reply;
return os;
}
struct StopsForBusResponse {
string reply;
};
ostream& operator << (ostream& os, const StopsForBusResponse& r) {
os << r.reply;
return os;
}
struct AllBusesResponse {
string reply;
};
ostream& operator << (ostream& os, const AllBusesResponse& r) {
os << r.reply;
return os;
}
class BusManager {
public:
void AddBus(const string& bus, const vector<string>& stops) {
buses_to_stops[bus] = stops;
for (const auto& stop : buses_to_stops[bus]) {
stops_to_buses[stop].push_back(bus);
}
}
BusesForStopResponse GetBusesForStop(const string& stop) const {
if (stops_to_buses.count(stop) == 0) {
return {"No stop"};
}
BusesForStopResponse result;
for (const auto& bus : stops_to_buses.at(stop)) {
result.reply += (bus + ' ');
}
return result;
}
StopsForBusResponse GetStopsForBus(const string& bus) const {
if (buses_to_stops.count(bus) == 0) {
return {"No bus"};
}
StopsForBusResponse result;
for (const auto& stop : buses_to_stops.at(bus)) {
result.reply += "Stop " + stop + ": ";
if (stops_to_buses.at(stop).size() == 1) {
result.reply += "no interchange";
} else {
for (const auto& other_bus : stops_to_buses.at(stop)) {
if (bus != other_bus) {
result.reply += (other_bus + ' ');
}
}
}
result.reply += '\n';
}
return result;
}
AllBusesResponse GetAllBuses() const {
if (buses_to_stops.empty()) {
return {"No buses"};
}
AllBusesResponse result;
for (const auto& bus_item : buses_to_stops) {
result.reply += "Bus " + bus_item.first + ": ";
for (const auto& stop : bus_item.second) {
result.reply += (stop + ' ');
}
result.reply += '\n';
}
return result;
}
private:
map<string, vector<string>> buses_to_stops;
map<string, vector<string>> stops_to_buses;
};
// Íå ìåíÿÿ òåëà ôóíêöèè main, ðåàëèçóéòå ôóíêöèè è êëàññû âûøå
int main() {
int query_count;
Query q;
cin >> query_count;
BusManager bm;
for (int i = 0; i < query_count; ++i) {
cin >> q;
switch (q.type) {
case QueryType::NewBus:
bm.AddBus(q.bus, q.stops);
break;
case QueryType::BusesForStop:
cout << bm.GetBusesForStop(q.stop) << endl;
break;
case QueryType::StopsForBus:
cout << bm.GetStopsForBus(q.bus) << endl;
break;
case QueryType::AllBuses:
cout << bm.GetAllBuses() << endl;
break;
}
}
return 0;
}