Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <map>
- #include <string>
- #include <algorithm>
- #include <iostream>
- #include <set>
- #include <utility>
- #include <vector>
- #include <numeric>
- #include <chrono>
- #include <type_traits>
- #include <cassert>
- #include <sstream>
- using namespace std;
- enum class QueryType{
- NewBus,
- BusesForStop,
- StopsForBus,
- AllBuses,
- };
- struct Query{
- QueryType type = QueryType::NewBus;
- string bus;
- string stop;
- vector<string> stops;
- };
- istream &operator>>(istream &is, Query &q){
- string operation_code;
- cin >> operation_code;
- if(operation_code == "NEW_BUS"s){
- q.stops.clear();
- q.type = QueryType::NewBus;
- is >> q.bus;
- int stop_count = 0;
- is >> stop_count;
- vector<string> stops;
- stops.resize(stop_count);
- for(int i = 0; i < stop_count; ++i){
- string stopName;
- is >> stopName;
- q.stops.push_back(stopName);
- }
- } else if(operation_code == "BUSES_FOR_STOP"s){
- q.type = QueryType::BusesForStop;
- is >> q.stop;
- } else if(operation_code == "STOPS_FOR_BUS"s){
- q.type = QueryType::StopsForBus;
- is >> q.bus;
- } else if(operation_code == "ALL_BUSES"s){
- q.type = QueryType::AllBuses;
- }
- return is;
- }
- struct BusesForStopResponse{
- vector<string> buses;
- string stopName;
- bool found = false;
- };
- ostream &operator<<(ostream &os, const BusesForStopResponse &r){
- if(!r.found){
- os << "No stop";
- return os;
- } else{
- string result;
- bool isFirst = true;
- for(const auto &element : r.buses){
- if(!isFirst){
- result += " ";
- }
- os << result + element;
- isFirst = false;
- }
- return os;
- }
- }
- struct StopsForBusResponse{
- vector<string> stops;
- string busName;
- bool found = false;
- map<string, vector<string>> busesForStops;
- };
- ostream &operator<<(ostream &os, const StopsForBusResponse &r){
- if(!r.found){
- os << "No bus";
- return os;
- }
- bool isFirstStop = true;
- for(const string &stop : r.stops){
- if(!isFirstStop){
- os << endl;
- }
- if(!stop.empty()){
- isFirstStop = false;
- os << "Stop "s << stop << ": "s;
- bool hasMoreThanOneBus = false;
- vector<string> busesForStop = r.busesForStops.at(stop);
- bool isFirstBus = true;
- for(const string &bus : busesForStop){
- if(bus == r.busName){
- continue;
- }
- if(!isFirstBus){
- os << " ";
- }
- isFirstBus = false;
- hasMoreThanOneBus = true;
- os << bus;
- }
- if(!hasMoreThanOneBus){
- os << "no interchange";
- }
- }
- }
- return os;
- }
- struct AllBusesResponse{
- map<string, vector<string>> busesResponse;
- };
- ostream &operator<<(ostream &os, const AllBusesResponse &r){
- map<string, vector<string>> response = r.busesResponse;
- if(!r.busesResponse.size()){
- os << "No buses";
- return os;
- }
- bool isFirstBus = true;
- for(const auto &[bus, stops] : response){
- if(!isFirstBus){
- os << endl;
- }
- os << "Bus "s << bus << ": ";
- bool isFirstStop = true;
- for(const auto &stop : stops){
- if(!isFirstStop){
- os << " ";
- }
- os << stop;
- isFirstStop = false;
- }
- isFirstBus = false;
- }
- return os;
- }
- class BusManager{
- public:
- void AddBus(const string &bus, const vector<string> &stops){
- for(const string &stop : stops){
- buses_to_stops_[bus].push_back(stop);
- stops_to_buses_[stop].push_back(bus);
- }
- }
- // Реализуйте этот метод - список автобусов, проезжающих через остановку
- BusesForStopResponse GetBusesForStop(const string &stop) const{
- BusesForStopResponse response;
- if(!stops_to_buses_.count(stop)){
- return response;
- }
- response.buses = stops_to_buses_.at(stop);
- response.stopName = stop;
- response.found = true;
- return response;
- }
- // Реализуйте этот метод - список остановок маршрута автобуса
- StopsForBusResponse GetStopsForBus(const string &bus) const{
- StopsForBusResponse response;
- if(!buses_to_stops_.count(bus)){
- return response;
- }
- response.stops = buses_to_stops_.at(bus);
- response.busName = bus;
- response.found = true;
- for(const string &stop : response.stops){
- response.busesForStops[stop] = stops_to_buses_.at(stop);
- }
- return response;
- }
- AllBusesResponse GetAllBuses() const{
- AllBusesResponse response;
- for(const auto &[bus, stops] : buses_to_stops_){
- response.busesResponse[bus] = stops;
- }
- return response;
- }
- private:
- map<string, vector<string>> stops_to_buses_, buses_to_stops_;
- };
- int main(){
- int query_count;
- Query q;
- cin >> query_count;
- BusManager bm;
- for(int i = 0; i < query_count; ++i){
- cin >> q;
- switch(q.type){
- case QueryType::NewBus:
- bm.AddBus(q.bus, q.stops);
- break;
- case QueryType::BusesForStop:
- cout << bm.GetBusesForStop(q.stop) << endl;
- break;
- case QueryType::StopsForBus:
- cout << bm.GetStopsForBus(q.bus) << endl;
- break;
- case QueryType::AllBuses:
- cout << bm.GetAllBuses() << endl;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment