nochnoikoshmar1

Untitled

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