Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <iostream>
- #include <map>
- #include <string>
- #include <vector>
- using namespace std;
- enum class QueryType {
- NewBus,
- BusesForStop,
- StopsForBus,
- AllBuses,
- };
- struct Query {
- QueryType type;
- string bus;
- string stop;
- vector<string> stops;
- };
- istream& operator>>(istream& is, vector<string>& stops) {
- string stop;
- for (int i = 0; i < stops.size(); ++i)
- {
- is >> stop;
- stops[i] = stop;
- stop = "";
- }
- return is;
- }
- istream& operator>>(istream& is, Query& q) {
- string quer;
- int counter;
- is >> quer;
- if (quer == "NEW_BUS") {
- q.type = QueryType::NewBus;
- is >> q.bus;
- is >> counter;
- q.stops.resize(counter);
- is >> q.stops;
- }
- else if (quer == "BUSES_FOR_STOP") {
- q.type = QueryType::BusesForStop;
- is >> q.stop;
- }
- else if (quer == "STOPS_FOR_BUS") {
- q.type = QueryType::StopsForBus;
- is >> q.bus;
- }
- else {
- q.type = QueryType::AllBuses;
- }
- return is;
- }
- struct BusesForStopResponse {
- vector<string> busesforstop;
- };
- ostream& operator<<(ostream& os, const BusesForStopResponse& r) {
- if (r.busesforstop.size() != 0) {
- int i = r.busesforstop.size();
- for (string stop : r.busesforstop) {
- os << stop;
- --i;
- if (i != 0)
- {
- os << " ";
- }
- }
- }
- else os << "No stop";
- return os;
- }
- struct StopsForBusResponse {
- vector<string> stopsforbus;
- map<string, vector<string>> buses_to_inter;
- };
- ostream& operator<<(ostream& os, const StopsForBusResponse& r) {
- if (r.stopsforbus.size() != 0) {
- bool isnotfirst = false;
- for (string stop : r.stopsforbus) {
- if (isnotfirst) os << endl;
- isnotfirst = true;
- os << "Stop ";
- os << stop << ":";
- if (r.buses_to_inter.find(stop) != r.buses_to_inter.end()) {
- for (string int_bus : r.buses_to_inter.at(stop))
- {
- os << " " << int_bus;
- }
- }
- else os << " no interchange";
- }
- }
- else os << "No bus";
- return os;
- }
- struct AllBusesResponse {
- map <string, vector<string>> allbuses;
- };
- ostream& operator<<(ostream& os, const AllBusesResponse& r) {
- if (r.allbuses.size() > 0) {
- bool isnotfirst = false;
- for (auto& bus : r.allbuses)
- {
- if (isnotfirst) os << endl;
- isnotfirst = true;
- os << "Bus " << bus.first << ":";
- for (string stop : bus.second)
- {
- os << " ";
- os << stop;
- }
- }
- }
- else os << "No buses";
- return os;
- }
- class BusManager {
- public:
- AllBusesResponse buses_;
- void AddBus(const string& bus, const vector<string>& stops) {
- for (string stop : stops)
- {
- buses_.allbuses[bus].push_back(stop);
- }
- }
- BusesForStopResponse GetBusesForStop(const string& stop) const {
- BusesForStopResponse stop_and_buses;
- for (auto bus : buses_.allbuses)
- {
- for (string stopv : bus.second)
- {
- if (stopv == stop)
- {
- stop_and_buses.busesforstop.push_back(bus.first);
- }
- }
- }
- return stop_and_buses;
- }
- StopsForBusResponse GetStopsForBus(const string& bus) const {
- StopsForBusResponse bus_and_stops;
- if (buses_.allbuses.find(bus) != buses_.allbuses.end()) {
- for (auto stop : buses_.allbuses.at(bus))
- {
- bus_and_stops.stopsforbus.push_back(stop);
- BusesForStopResponse int_buses = GetBusesForStop(stop);
- for (string int_bus : int_buses.busesforstop)
- {
- if (int_bus != bus)
- {
- bus_and_stops.buses_to_inter[stop].push_back(int_bus);
- }
- }
- }
- }
- return bus_and_stops;
- }
- AllBusesResponse GetAllBuses() const {
- return buses_;
- }
- };
- 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
Advertisement