shizaero

Untitled

Feb 3rd, 2023
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.46 KB | None | 0 0
  1. #include <cassert>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5. #include <vector>
  6. #include <sstream>
  7. #include <set>
  8.  
  9. using namespace std;
  10.  
  11. enum class QueryType {
  12.     NewBus,
  13.     BusesForStop,
  14.     StopsForBus,
  15.     AllBuses,
  16. };
  17.  
  18. struct Query {
  19.     QueryType type;
  20.     string bus;
  21.     string stop;
  22.     vector<string> stops;
  23. };
  24.  
  25. istream& operator>>(istream& is, Query& q) {
  26.     // Реализуйте эту функцию
  27.     string operation_code;
  28.     is >> operation_code;
  29.  
  30.     if (operation_code == "NEW_BUS"s) {
  31.         q.type = QueryType::NewBus;
  32.         is >> q.bus;
  33.         int stop_count;
  34.         is >> stop_count;
  35.         q.stops.clear();
  36.         for (int i = 0; i < stop_count; ++i) {
  37.             string stop_word;
  38.             is >> stop_word;
  39.             q.stops.push_back(stop_word);
  40.         }
  41.     }
  42.     else if (operation_code == "BUSES_FOR_STOP"s) {
  43.         is >> q.stop;
  44.         q.type = QueryType::BusesForStop;
  45.     }
  46.     else if (operation_code == "STOPS_FOR_BUS"s) {
  47.         is >> q.bus;
  48.         q.type = QueryType::StopsForBus;
  49.     }
  50.     else if (operation_code == "ALL_BUSES"s) {
  51.         q.type = QueryType::AllBuses;
  52.     }
  53.  
  54.     return is;
  55. }
  56.  
  57. struct BusesForStopResponse {
  58.     // Наполните полями эту структуру
  59.     vector<string> buses;
  60.     bool no_stop;
  61. };
  62.  
  63. ostream& operator<<(ostream& os, const BusesForStopResponse& r) {
  64.     // Реализуйте эту функцию
  65.     if (r.no_stop == true) {
  66.         os << "No stop"s;
  67.     }
  68.     else {
  69.         bool is_first = true;
  70.         for (const auto& a : r.buses) {
  71.             if (is_first) {
  72.                 os << a;
  73.                 is_first = false;
  74.             }
  75.             else {
  76.                 os << " "s << a;
  77.             }
  78.         }
  79.     }
  80.     return os;
  81. }
  82.  
  83. struct StopsForBusResponse {
  84.     //Наполните полями эту структуру
  85.     //set<string> interchange;
  86.     vector<string> stops;
  87.     map<string, vector<string>> stop_to_buses;
  88.     bool no_stop = false;
  89. };
  90.  
  91. ostream& operator<<(ostream& os, const StopsForBusResponse& r) {
  92.     // Реализуйте эту функцию
  93.     if (r.no_stop == true) {
  94.         os << "No bus"s;
  95.     }
  96.     else {
  97.         int count = 0;
  98.         for (string stop : r.stops) {
  99.             bool is_first = true;
  100.             os << "Stop "s << stop << ": "s;
  101.             for (auto a : r.stop_to_buses.at(stop)) {
  102.                 if (is_first) {
  103.                     os << a;
  104.                     is_first = false;
  105.                 }
  106.                 else {
  107.                     os << " "s << a;
  108.                 }
  109.             }
  110.             ++count;
  111.             if (count != r.stops.size()) {
  112.                 os << endl;
  113.             }
  114.            
  115.         }
  116.     }
  117.     return os;
  118. }
  119.  
  120. struct AllBusesResponse {
  121.     // Наполните полями эту структуру
  122.     map<string, vector<string>> bus_to_stops;
  123.     bool no_stop;
  124. };
  125.  
  126. ostream& operator<<(ostream& os, const AllBusesResponse& r) {
  127.     // Реализуйте эту функцию
  128.     if (r.no_stop == true) {
  129.         os << "No buses"s;
  130.     }
  131.     else {
  132.         for (const auto& [bus, stops] : r.bus_to_stops) {
  133.             os << "Bus "s << bus << ": "s;
  134.             bool is_first = true;
  135.             for (const string& stop : stops) {
  136.                 if (is_first) {
  137.                     os << stop;
  138.                     is_first = false;
  139.                 }
  140.                 else {
  141.                     os << " "s << stop;
  142.                 }
  143.             }
  144.             os << endl;
  145.         }
  146.     }
  147.     return os;
  148. }
  149.  
  150. class BusManager {
  151. public:
  152.  
  153.     void AddBus(const string& bus, const vector<string>& stops) {
  154.         // Реализуйте этот метод
  155.         for (const string& stop : stops) {
  156.             bus_stops_[bus].push_back(stop);
  157.             stop_bases_[stop].push_back(bus);
  158.         }
  159.     }
  160.  
  161.     BusesForStopResponse GetBusesForStop(const string& stop) const {
  162.         // Реализуйте этот метод
  163.         BusesForStopResponse finish;
  164.         if (stop_bases_.count(stop) == 0) {
  165.             finish.no_stop = true;
  166.         }
  167.         else {
  168.             finish.no_stop = false;
  169.             for (const auto& a : stop_bases_.at(stop)) {
  170.                 finish.buses.push_back(a);
  171.             }
  172.         }
  173.         return finish;
  174.     }
  175.  
  176.     StopsForBusResponse GetStopsForBus(const string& bus) const {
  177.         StopsForBusResponse finish;
  178.         vector <string> interchange = { "no interchange"s };
  179.         if (bus_stops_.count(bus) == 0) {
  180.             finish.no_stop = true;
  181.         }
  182.         else {
  183.             for (const string& stop : bus_stops_.at(bus)) {
  184.                 finish.stops.push_back(stop);
  185.                 if (stop_bases_.at(stop).size() == 1) {
  186.                     finish.stop_to_buses[stop] = interchange;
  187.                 }
  188.                 else {
  189.                     for (const string& other_bus : stop_bases_.at(stop)) {
  190.                         if (bus != other_bus) {
  191.                             finish.stop_to_buses[stop].push_back(other_bus);
  192.                         }
  193.                     }
  194.                 }
  195.             }
  196.         }
  197.         return finish;
  198.     }
  199.  
  200.     AllBusesResponse GetAllBuses() const {
  201.         // Реализуйте этот метод
  202.         AllBusesResponse finish;
  203.         if (bus_stops_.empty()) {
  204.             finish.no_stop = true;
  205.         }
  206.         else {
  207.             finish.no_stop = false;
  208.             finish.bus_to_stops = bus_stops_;
  209.             /*for (const auto& [bus, stops] : bus_stops_) {
  210.                 for (const string& stop : stops) {
  211.                     finish.bus_to_stops[bus].push_back(stop);
  212.                 }
  213.             }*/
  214.         }
  215.         return finish;
  216.     }
  217. private:
  218.     map<string, vector<string>> bus_stops_, stop_bases_;
  219. };
  220.  
  221. int main() {
  222.    
  223.     int query_count;
  224.     Query q;
  225.  
  226.     cin >> query_count;
  227.  
  228.     BusManager bm;
  229.     for (int i = 0; i < query_count; ++i) {
  230.         cin >> q;
  231.         switch (q.type) {
  232.         case QueryType::NewBus:
  233.             bm.AddBus(q.bus, q.stops);
  234.             break;
  235.         case QueryType::BusesForStop:
  236.             cout << bm.GetBusesForStop(q.stop) << endl;
  237.             break;
  238.         case QueryType::StopsForBus:
  239.             cout << bm.GetStopsForBus(q.bus) << endl;
  240.             break;
  241.         case QueryType::AllBuses:
  242.             cout << bm.GetAllBuses() << endl;
  243.             break;
  244.         }
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment