nochnoikoshmar1

Untitled

Mar 14th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 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.     bool isFirstStop = true;
  89.     for(const string &stop : r.stops){        
  90.        
  91.         if(!isFirstStop){
  92.             os << endl;
  93.         }
  94.         if(!stop.empty()){
  95.             isFirstStop = false;
  96.             os << "Stop "s << stop << ": "s;
  97.  
  98.             bool hasMoreThanOneBus = false;
  99.  
  100.             bool isFirstBus = true;
  101.             for(const string &bus : r.busesForStops.at(stop)){
  102.                 if(bus == r.busName){
  103.                     continue;
  104.                 }
  105.                 if(!isFirstBus){
  106.                     os << " ";
  107.                 }
  108.                 isFirstBus = false;
  109.                 hasMoreThanOneBus = true;
  110.                 os << bus;
  111.             }
  112.             if(!hasMoreThanOneBus){
  113.                 os << "no interchange";
  114.             }
  115.         }        
  116.     }
  117.     return os;
  118. }
  119. struct AllBusesResponse{
  120.     map<string, vector<string>> busesResponse;
  121. };
  122. ostream &operator<<(ostream &os, const AllBusesResponse &r){
  123.     if(!r.busesResponse.size()){
  124.         os << "No buses";
  125.         return os;
  126.     }
  127.     bool isFirstBus = true;
  128.     for(const auto &[bus, stops] : r.busesResponse){
  129.         if(!isFirstBus){
  130.             os << endl;
  131.         }
  132.         os << "Bus "s << bus << ": ";
  133.         for(int i = 0; i <= stops.size() - 1; i++){
  134.             if(stops.size() - 1 == i){
  135.                 os << stops[i];
  136.             } else{
  137.                 os << stops[i] << " ";
  138.             }
  139.         }
  140.         isFirstBus = false;
  141.     }
  142.     os << " ";
  143.     return os;
  144. }
  145. class BusManager{
  146. public:
  147.     void AddBus(const string &bus, const vector<string> &stops){        
  148.         for(const string &stop : stops){
  149.             buses_to_stops_[bus].push_back(stop);
  150.             stops_to_buses_[stop].push_back(bus);
  151.         }
  152.     }
  153.     // Реализуйте этот метод - список автобусов, проезжающих через остановку
  154.     BusesForStopResponse GetBusesForStop(const string &stop) const{        
  155.         BusesForStopResponse response;
  156.         if(!stops_to_buses_.count(stop)){
  157.             return response;
  158.         }
  159.         response.buses = stops_to_buses_.at(stop);
  160.         response.stopName = stop;        
  161.         response.found = true;
  162.         return response;
  163.     }
  164.     // Реализуйте этот метод - список остановок маршрута автобуса
  165.     StopsForBusResponse GetStopsForBus(const string &bus) const{        
  166.         StopsForBusResponse response;
  167.         if(!buses_to_stops_.count(bus)){
  168.             return response;
  169.         }
  170.         response.stops = buses_to_stops_.at(bus);        
  171.         response.busName = bus;
  172.         response.found = true;
  173.         for(const string &stop : response.stops){
  174.             response.busesForStops[stop] = stops_to_buses_.at(stop);
  175.         }
  176.         return response;
  177.     }
  178.  
  179.     AllBusesResponse GetAllBuses() const{
  180.         AllBusesResponse response;
  181.         for(const auto &[bus, stops] : buses_to_stops_){
  182.             response.busesResponse[bus] = stops;
  183.         }
  184.         return response;
  185.     }
  186. private:
  187.     map<string, vector<string>> stops_to_buses_, buses_to_stops_;
  188. };
  189. int main(){
  190.     int query_count;
  191.     Query q;
  192.     cin >> query_count;
  193.     BusManager bm;
  194.     for(int i = 0; i < query_count; ++i){
  195.         cin >> q;
  196.         switch(q.type){
  197.         case QueryType::NewBus:
  198.             bm.AddBus(q.bus, q.stops);
  199.             break;
  200.         case QueryType::BusesForStop:
  201.             cout << bm.GetBusesForStop(q.stop) << endl;
  202.             break;
  203.         case QueryType::StopsForBus:
  204.             cout << bm.GetStopsForBus(q.bus) << endl;
  205.             break;
  206.         case QueryType::AllBuses:
  207.             cout << bm.GetAllBuses() << endl;
  208.             break;
  209.         }
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment