Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <iostream>
- #include <map>
- #include <string>
- #include <vector>
- #include <algorithm>
- 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.bus.clear();
- q.stops.clear();
- q.stop.clear();
- string operation_code;
- is >> operation_code;
- if (operation_code == "NEW_BUS") {
- int stop_count;
- q.type = QueryType::NewBus;
- is >> q.bus >> stop_count;
- for (int i = 0; i < stop_count; ++i) {
- is >> q.stop;
- q.stops.push_back(q.stop);
- }
- }
- else if (operation_code == "BUSES_FOR_STOP") {
- q.type = QueryType::BusesForStop;
- is >> q.stop;
- }
- else if (operation_code == "STOPS_FOR_BUS") {
- q.type = QueryType::StopsForBus;
- is >> q.bus;
- }
- else if (operation_code == "ALL_BUSES") {
- q.type = QueryType::AllBuses;
- }
- return is;
- }
- struct BusesForStopResponse {
- vector<string> bus_list;
- bool no_stop = false;
- };
- ostream& operator<<(ostream& os, const BusesForStopResponse& r) {
- if (r.no_stop) {
- for (const auto& stop: r.bus_list) {
- os << stop << " "s;
- }
- }
- else {
- os << "No stop"s;
- }
- return os;
- }
- struct StopsForBusResponse {
- vector<string> stop_list;
- bool no_bus = false;
- string bus;
- };
- ostream& operator<<(ostream& os, const StopsForBusResponse& r) {
- if (r.no_bus) {
- for (const auto& stop : r.stop_list) {
- os << stop << " "s;
- }
- }
- else {
- os << "No interchange"s;
- }
- return os;
- }
- struct AllBusesResponse {
- map<string, vector<string>> all_buses;
- bool no_buses = false;
- };
- ostream& operator<<(ostream& os, const AllBusesResponse& r) {
- // Реализуйте эту функцию
- if (r.all_buses.empty())
- {
- os << "No buses"s;
- }
- else
- {
- for (const auto& bus_item : r.all_buses)
- {
- os << "Bus "s << bus_item.first << ": "s;
- for (const auto& stop : bus_item.second)
- {
- os << stop << " "s;
- }
- os << endl;
- }
- }
- return os;
- }
- class BusManager {
- public:
- void AddBus(const string& bus, const vector<string>& stops) {
- buses_to_stops_.at(bus).clear();
- for (const string& stop : stops) {
- buses_to_stops_.at(bus).push_back(stop);
- stops_to_buses_.at(stop).push_back(bus);
- }
- }
- BusesForStopResponse GetBusesForStop(const string& stop) const {
- // Реализуйте этот метод
- BusesForStopResponse response;
- if (stops_to_buses_.count(stop) != 0) {
- for (const auto& bus : stops_to_buses_.at(stop)) {
- response.bus_list.push_back(bus);
- }
- }
- else {
- response.no_stop = true;
- }
- return response;
- }
- StopsForBusResponse GetStopsForBus(const string& bus) const {
- // Реализуйте этот метод
- StopsForBusResponse response;
- if (buses_to_stops_.count(bus) != 0) {
- response.bus = bus;
- for (const auto& stop : buses_to_stops_.at(bus)) {
- response.stop_list.push_back(stop);
- }
- }
- else {
- response.no_bus = true;
- }
- return response;
- }
- AllBusesResponse GetAllBuses() const {
- // Реализуйте этот метод
- AllBusesResponse response;
- if (!buses_to_stops_.empty()) {
- //sort(buses_to_stops_.begin(), buses_to_stops_.end(), 0);
- response.all_buses = buses_to_stops_;
- }
- else {
- response.no_buses = true;
- }
- return response;
- }
- private:
- map<string, vector<string>> buses_to_stops_, // автобус (список остановки)
- 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment