chevengur

СПРИНТ № 3 | Введение в тестирование | Урок 6: Декомпозиция и отладка кода

Nov 8th, 2023 (edited)
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     int q;
  10.     cin >> q;
  11.  
  12.     map<string, vector<string>> buses_to_stops, stops_to_buses;
  13.  
  14.     for (int i = 0; i < q; ++i) {
  15.         string operation_code;
  16.         cin >> operation_code;
  17.  
  18.         if (operation_code == "NEW_BUS"s) {
  19.             string bus;
  20.             cin >> bus;
  21.             int stop_count;
  22.             cin >> stop_count;
  23.             vector<string>& stops = buses_to_stops[bus];
  24.             stops.resize(stop_count);
  25.             for (string& stop : stops) {
  26.                 cin >> stop;
  27.                 stops_to_buses[stop].push_back(bus);
  28.             }
  29.  
  30.         } else if (operation_code == "BUSES_FOR_STOP"s) {
  31.             string stop;
  32.             cin >> stop;
  33.             if (stops_to_buses.count(stop) == 0) {
  34.                 cout << "No stop"s << endl;
  35.             } else {
  36.                 bool is_first = true;
  37.                 for (const string& bus : stops_to_buses[stop]) {
  38.                     if (!is_first) {
  39.                         cout << " "s;
  40.                     }
  41.                     is_first = false;
  42.                     cout << bus;
  43.                 }
  44.                 cout << endl;
  45.             }
  46.  
  47.         } else if (operation_code == "STOPS_FOR_BUS"s) {
  48.             string bus;
  49.             cin >> bus;
  50.             if (buses_to_stops.count(bus) == 0) {
  51.                 cout << "No bus"s << endl;
  52.             } else {
  53.                 for (const string& stop : buses_to_stops[bus]) {
  54.                     cout << "Stop "s << stop << ":"s;
  55.                     if (stops_to_buses[stop].size() == 1) {
  56.                         cout << " no interchange"s;
  57.                     } else {
  58.                         for (const string& other_bus : stops_to_buses[stop]) {
  59.                             if (bus != other_bus) {
  60.                                 cout << " "s << other_bus;
  61.                             }
  62.                         }
  63.                     }
  64.                     cout << endl;
  65.                 }
  66.             }
  67.  
  68.         } else if (operation_code == "ALL_BUSES"s) {
  69.             if (buses_to_stops.empty()) {
  70.                 cout << "No buses"s << endl;
  71.             } else {
  72.                 for (const auto& bus_item : buses_to_stops) {
  73.                     cout << "Bus "s << bus_item.first << ":"s;
  74.                     for (const string& stop : bus_item.second) {
  75.                         cout << " "s << stop;
  76.                     }
  77.                     cout << endl;
  78.                 }
  79.             }
  80.         }
  81.     }
  82.  
  83.     return 0;
  84. }
Add Comment
Please, Sign In to add comment