Advertisement
Alaricy

декомпоз

Nov 24th, 2022 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.93 KB | None | 0 0
  1. #include <cassert>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. enum class QueryType {
  10.     NewBus,
  11.     BusesForStop,
  12.     StopsForBus,
  13.     AllBuses,
  14. };
  15.  
  16. struct Query {
  17.     QueryType type;
  18.     string bus;
  19.     string stop;
  20.     vector<string> stops;
  21. };
  22.  
  23. istream& operator>>(istream& is, vector<string>& stops) {
  24.     string stop;
  25.     for (int i = 0; i < stops.size(); ++i)
  26.     {
  27.         is >> stop;
  28.         stops[i] = stop;
  29.         stop = "";
  30.     }
  31.     return is;
  32. }
  33.  
  34. istream& operator>>(istream& is, Query& q) {
  35.     string quer;
  36.     int counter;
  37.     is >> quer;
  38.     if (quer == "NEW_BUS") {
  39.         q.type = QueryType::NewBus;
  40.         is >> q.bus;
  41.         is >> counter;
  42.         q.stops.resize(counter);
  43.         is >> q.stops;
  44.     }
  45.     else if (quer == "BUSES_FOR_STOP") {
  46.         q.type = QueryType::BusesForStop;
  47.         is >> q.stop;
  48.     }
  49.     else if (quer == "STOPS_FOR_BUS") {
  50.         q.type = QueryType::StopsForBus;
  51.         is >> q.bus;
  52.     }
  53.     else {
  54.         q.type = QueryType::AllBuses;
  55.     }
  56.     return is;
  57. }
  58.  
  59. struct BusesForStopResponse {
  60.     vector<string> busesforstop;
  61. };
  62.  
  63. ostream& operator<<(ostream& os, const BusesForStopResponse& r) {
  64.     if (r.busesforstop.size() != 0) {
  65.         int i = r.busesforstop.size();
  66.         for (string stop : r.busesforstop) {
  67.             os << stop;
  68.             --i;
  69.             if (i != 0)
  70.             {
  71.                 os << " ";
  72.             }
  73.         }
  74.     }
  75.     else os << "No stop";
  76.     return os;
  77. }
  78.  
  79. struct StopsForBusResponse {
  80.     vector<string> stopsforbus;
  81.     map<string, vector<string>> buses_to_inter;
  82. };
  83.  
  84. ostream& operator<<(ostream& os, const StopsForBusResponse& r) {
  85.     if (r.stopsforbus.size() != 0) {
  86.         bool isnotfirst = false;
  87.         for (string stop : r.stopsforbus) {
  88.             if (isnotfirst) os << endl;
  89.             isnotfirst = true;
  90.             os << "Stop ";
  91.             os << stop << ":";
  92.             if (r.buses_to_inter.find(stop) != r.buses_to_inter.end()) {
  93.                 for (string int_bus : r.buses_to_inter.at(stop))
  94.                 {
  95.                     os << " " << int_bus;
  96.                 }
  97.             }
  98.             else os << " no interchange";
  99.  
  100.         }
  101.     }
  102.     else os << "No bus";
  103.     return os;
  104. }
  105.  
  106. struct AllBusesResponse {
  107.     map <string, vector<string>> allbuses;
  108. };
  109.  
  110. ostream& operator<<(ostream& os, const AllBusesResponse& r) {
  111.     if (r.allbuses.size() > 0) {
  112.         bool isnotfirst = false;
  113.         for (auto& bus : r.allbuses)
  114.         {
  115.             if (isnotfirst) os << endl;
  116.             isnotfirst = true;
  117.             os << "Bus " << bus.first << ":";
  118.             for (string stop : bus.second)
  119.             {
  120.                 os << " ";
  121.                 os << stop;
  122.             }
  123.         }
  124.     }
  125.     else os << "No buses";
  126.     return os;
  127. }
  128.  
  129. class BusManager {
  130. public:
  131.     AllBusesResponse buses_;
  132.     void AddBus(const string& bus, const vector<string>& stops) {
  133.         for (string stop : stops)
  134.         {
  135.             buses_.allbuses[bus].push_back(stop);
  136.         }
  137.     }
  138.  
  139.     BusesForStopResponse GetBusesForStop(const string& stop) const {
  140.         BusesForStopResponse stop_and_buses;
  141.         for (auto bus : buses_.allbuses)
  142.         {
  143.             for (string stopv : bus.second)
  144.             {
  145.                 if (stopv == stop)
  146.                 {
  147.                     stop_and_buses.busesforstop.push_back(bus.first);
  148.                 }
  149.             }
  150.         }
  151.         return stop_and_buses;
  152.     }
  153.  
  154.     StopsForBusResponse GetStopsForBus(const string& bus) const {
  155.         StopsForBusResponse bus_and_stops;
  156.         if (buses_.allbuses.find(bus) != buses_.allbuses.end()) {
  157.             for (auto stop : buses_.allbuses.at(bus))
  158.             {
  159.                 bus_and_stops.stopsforbus.push_back(stop);
  160.                 BusesForStopResponse int_buses = GetBusesForStop(stop);
  161.                 for (string int_bus : int_buses.busesforstop)
  162.                 {
  163.                     if (int_bus != bus)
  164.                     {
  165.                         bus_and_stops.buses_to_inter[stop].push_back(int_bus);
  166.                     }
  167.                 }
  168.             }
  169.         }
  170.         return bus_and_stops;
  171.     }
  172.  
  173.     AllBusesResponse GetAllBuses() const {
  174.         return buses_;
  175.     }
  176. };
  177.  
  178.  
  179. int main() {
  180.     int query_count;
  181.     Query q;
  182.  
  183.     cin >> query_count;
  184.  
  185.     BusManager bm;
  186.     for (int i = 0; i < query_count; ++i) {
  187.         cin >> q;
  188.         switch (q.type) {
  189.         case QueryType::NewBus:
  190.             bm.AddBus(q.bus, q.stops);
  191.             break;
  192.         case QueryType::BusesForStop:
  193.             cout << bm.GetBusesForStop(q.stop) << endl;
  194.             break;
  195.         case QueryType::StopsForBus:
  196.             cout << bm.GetStopsForBus(q.bus) << endl;
  197.             break;
  198.         case QueryType::AllBuses:
  199.             cout << bm.GetAllBuses() << endl;
  200.             break;
  201.         }
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement