shizaero

Untitled

Jan 29th, 2023
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.04 KB | None | 0 0
  1. #include <cassert>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. enum class QueryType {
  11.     NewBus,
  12.     BusesForStop,
  13.     StopsForBus,
  14.     AllBuses,
  15. };
  16.  
  17. struct Query {
  18.     QueryType type;
  19.     string bus;
  20.     string stop;
  21.     vector<string> stops;
  22. };
  23.  
  24. istream& operator>>(istream& is, Query& q) {
  25.     q.bus.clear();
  26.     q.stops.clear();
  27.     q.stop.clear();
  28.    
  29.     string operation_code;
  30.     is >> operation_code;
  31.  
  32.     if (operation_code == "NEW_BUS") {
  33.         int stop_count;
  34.         q.type = QueryType::NewBus;
  35.         is >> q.bus >> stop_count;
  36.         for (int i = 0; i < stop_count; ++i) {
  37.             is >> q.stop;
  38.             q.stops.push_back(q.stop);
  39.         }
  40.     }
  41.     else if (operation_code == "BUSES_FOR_STOP") {
  42.         q.type = QueryType::BusesForStop;
  43.         is >> q.stop;
  44.     }
  45.     else if (operation_code == "STOPS_FOR_BUS") {
  46.         q.type = QueryType::StopsForBus;
  47.         is >> q.bus;
  48.     }
  49.     else if (operation_code == "ALL_BUSES") {
  50.         q.type = QueryType::AllBuses;
  51.     }
  52.     return is;
  53. }
  54.  
  55. struct BusesForStopResponse {
  56.     vector<string> bus_list;
  57.     bool no_stop = false;
  58. };
  59.  
  60. ostream& operator<<(ostream& os, const BusesForStopResponse& r) {
  61.     if (r.no_stop) {
  62.         for (const auto& stop: r.bus_list) {
  63.             os << stop << " "s;
  64.         }
  65.     }
  66.     else {
  67.         os << "No stop"s;
  68.     }
  69.     return os;
  70. }
  71.  
  72. struct StopsForBusResponse {
  73.     vector<string> stop_list;
  74.     bool no_bus = false;
  75.     string bus;
  76. };
  77.  
  78. ostream& operator<<(ostream& os, const StopsForBusResponse& r) {
  79.     if (r.no_bus) {
  80.         for (const auto& stop : r.stop_list) {
  81.             os << stop << " "s;
  82.         }
  83.     }
  84.     else {
  85.         os << "No interchange"s;
  86.     }
  87.     return os;
  88. }
  89.  
  90. struct AllBusesResponse {
  91.     map<string, vector<string>> all_buses;
  92.     bool no_buses = false;
  93. };
  94.  
  95. ostream& operator<<(ostream& os, const AllBusesResponse& r) {
  96.     // Реализуйте эту функцию
  97.     if (r.all_buses.empty())
  98.     {
  99.         os << "No buses"s;
  100.     }
  101.     else
  102.     {
  103.         for (const auto& bus_item : r.all_buses)
  104.         {
  105.             os << "Bus "s << bus_item.first << ": "s;
  106.             for (const auto& stop : bus_item.second)
  107.             {
  108.                 os << stop << " "s;
  109.             }
  110.             os << endl;
  111.         }
  112.     }
  113.     return os;
  114. }
  115.  
  116. class BusManager {
  117. public:
  118.     void AddBus(const string& bus, const vector<string>& stops) {
  119.         buses_to_stops_.at(bus).clear();
  120.         for (const string& stop : stops) {
  121.             buses_to_stops_.at(bus).push_back(stop);
  122.             stops_to_buses_.at(stop).push_back(bus);
  123.         }
  124.     }
  125.  
  126.     BusesForStopResponse GetBusesForStop(const string& stop) const {
  127.         // Реализуйте этот метод
  128.         BusesForStopResponse response;
  129.         if (stops_to_buses_.count(stop) != 0) {
  130.             for (const auto& bus : stops_to_buses_.at(stop)) {
  131.                 response.bus_list.push_back(bus);
  132.             }
  133.         }
  134.         else {
  135.             response.no_stop = true;
  136.         }
  137.         return response;
  138.     }
  139.  
  140.     StopsForBusResponse GetStopsForBus(const string& bus) const {
  141.         // Реализуйте этот метод
  142.         StopsForBusResponse response;
  143.         if (buses_to_stops_.count(bus) != 0) {
  144.             response.bus = bus;
  145.             for (const auto& stop : buses_to_stops_.at(bus)) {
  146.                 response.stop_list.push_back(stop);
  147.             }
  148.         }
  149.         else {
  150.             response.no_bus = true;
  151.         }
  152.         return response;
  153.     }
  154.  
  155.     AllBusesResponse GetAllBuses() const {
  156.         // Реализуйте этот метод
  157.         AllBusesResponse response;
  158.         if (!buses_to_stops_.empty()) {
  159.             //sort(buses_to_stops_.begin(), buses_to_stops_.end(), 0);
  160.             response.all_buses = buses_to_stops_;
  161.         }
  162.         else {
  163.             response.no_buses = true;
  164.         }
  165.         return response;
  166.     }
  167. private:
  168.     map<string, vector<string>> buses_to_stops_, // автобус (список остановки)
  169.         stops_to_buses_; //остановки (список автобусов)
  170. };
  171.  
  172. // Реализуйте функции и классы, объявленные выше, чтобы эта функция main
  173. // решала задачу "Автобусные остановки"
  174.  
  175. int main() {
  176.     int query_count;
  177.     Query q;
  178.  
  179.     cin >> query_count;
  180.  
  181.     BusManager bm;
  182.     for (int i = 0; i < query_count; ++i) {
  183.         cin >> q;
  184.         switch (q.type) {
  185.         case QueryType::NewBus:
  186.             bm.AddBus(q.bus, q.stops);
  187.             break;
  188.         case QueryType::BusesForStop:
  189.             cout << bm.GetBusesForStop(q.stop) << endl;
  190.             break;
  191.         case QueryType::StopsForBus:
  192.             cout << bm.GetStopsForBus(q.bus) << endl;
  193.             break;
  194.         case QueryType::AllBuses:
  195.             cout << bm.GetAllBuses() << endl;
  196.             break;
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment