Advertisement
Tom_Clance

testPaste

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