Advertisement
Wow_Rasl

Untitled

Nov 17th, 2021
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. // ответ на запрос BUSES_FOR_STOP
  9. void PrintBusesForStop(map<string, vector<string>>& stops_to_buses,
  10.                        const string& stop) {
  11.   if (stops_to_buses.count(stop) == 0) {
  12.     cout << "No stop" << endl;
  13.   } else {
  14.     for (const string& bus : stops_to_buses[stop]) {
  15.       cout << bus << " ";
  16.     }
  17.     cout << endl;
  18.   }                
  19. }
  20.  
  21. // ответ на запрос STOPS_FOR_BUS
  22. void PrintStopsForBus(map<string, vector<string>>& buses_to_stops,
  23.                       map<string, vector<string>>& stops_to_buses,
  24.                       const string& bus) {
  25.   if (buses_to_stops.count(bus) == 0) {
  26.     cout << "No bus" << endl;
  27.   } else {
  28.     for (const string& stop : buses_to_stops[bus]) {
  29.       cout << "Stop " << stop << ": ";
  30.      
  31.       // если через остановку проходит ровно один автобус,
  32.       // то это наш автобус bus, следовательно, пересадки тут нет
  33.       if (stops_to_buses[stop].size() == 1) {
  34.         cout << "no interchange";
  35.       } else {
  36.         for (const string& other_bus : stops_to_buses[stop]) {
  37.           if (bus != other_bus) {
  38.             cout << other_bus << " ";
  39.           }
  40.         }
  41.       }
  42.       cout << endl;
  43.     }
  44.   }            
  45. }
  46.  
  47. // ответ на запрос ALL_BUSES
  48. void PrintAllBuses(const map<string, vector<string>>& buses_to_stops) {
  49.   if (buses_to_stops.empty()) {
  50.     cout << "No buses" << endl;
  51.   } else {
  52.     for (const auto& bus_item : buses_to_stops) {
  53.       cout << "Bus " << bus_item.first << ": ";
  54.       for (const string& stop : bus_item.second) {
  55.         cout << stop << " ";
  56.       }
  57.       cout << endl;
  58.     }
  59.   }            
  60. }
  61.  
  62. int main() {
  63.   int q;
  64.   cin >> q;
  65.  
  66.   map<string, vector<string>> buses_to_stops, stops_to_buses;
  67.  
  68.   for (int i = 0; i < q; ++i) {
  69.     string operation_code;
  70.     cin >> operation_code;
  71.  
  72.     if (operation_code == "NEW_BUS") {
  73.  
  74.       string bus;
  75.       cin >> bus;
  76.       int stop_count;
  77.       cin >> stop_count;
  78.      
  79.       // с помощью ссылки дадим короткое название вектору
  80.       // со списком остановок данного автобуса;
  81.       // ключ bus изначально отсутствовал в словаре, поэтому он автоматически
  82.       // добавится туда с пустым вектором в качестве значения
  83.       vector<string>& stops = buses_to_stops[bus];
  84.       stops.resize(stop_count);
  85.      
  86.       for (string& stop : stops) {
  87.         cin >> stop;
  88.         // не забудем обновить словарь stops_to_buses
  89.         stops_to_buses[stop].push_back(bus);
  90.       }
  91.  
  92.     } else if (operation_code == "BUSES_FOR_STOP") {
  93.  
  94.       string stop;
  95.       cin >> stop;
  96.       PrintBusesForStop(stops_to_buses, stop);
  97.  
  98.     } else if (operation_code == "STOPS_FOR_BUS") {
  99.  
  100.       string bus;
  101.       cin >> bus;
  102.       PrintStopsForBus(buses_to_stops, stops_to_buses, bus);
  103.  
  104.     } else if (operation_code == "ALL_BUSES") {
  105.  
  106.       PrintAllBuses(buses_to_stops);
  107.  
  108.     }
  109.   }
  110.  
  111.   return 0;
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement