Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <cassert>
- #include <vector>
- #include <map>
- 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, Query& q) {
- string tString;
- is >> tString;
- if (tString == "NEW_BUS") {
- q.type = QueryType::NewBus;
- is >> q.bus;
- int stop_count = 0;
- q.stops.resize(0);
- is >> stop_count;
- for (int i = 0; i < stop_count; i++) {
- is >> q.stop;
- q.stops.push_back(q.stop);
- }
- }else if(tString == "BUSES_FOR_STOP"){
- q.type = QueryType::BusesForStop;
- is >> q.stop;
- }else if (tString == "STOPS_FOR_BUS") {
- q.type = QueryType::StopsForBus;
- is >> q.bus;
- }
- else if (tString == "ALL_BUSES") {
- q.type = QueryType::AllBuses;
- }
- return is;
- }
- struct BusesForStopResponse {
- string stop;
- vector<string> buses;
- };
- ostream& operator << (ostream& os, const BusesForStopResponse& r) {
- if (r.buses.size() == 0) {
- os << "No stop";
- }
- else {
- bool flag = true;
- for (const auto& item : r.buses) {
- if (flag == false) {
- os << " ";
- }
- os << item;
- flag = false;
- }
- }
- return os;
- }
- struct StopsForBusResponse {
- string bus;
- map<string,vector<string>> stop_bus,bus_stops;
- };
- ostream& operator << (ostream& os, const StopsForBusResponse& r) {
- if (r.stop_bus.empty()==true) {
- os << "No bus";
- }
- else {
- bool flag = true;
- for (auto item : r.bus_stops.at(r.bus)) {
- if (flag == false) {
- os << endl;
- }
- flag = false;
- os << "Stop " << item << ":";
- if (r.stop_bus.at(item)[0] == r.bus && r.stop_bus.at(item).size()==1) {
- os << " no interchange";
- }
- else {
- for (unsigned int i = 0; i < r.stop_bus.at(item).size();i++) {
- if (r.stop_bus.at(item)[i]!= r.bus) {
- os << " " << r.stop_bus.at(item)[i];
- }
- }
- }
- }
- }
- return os;
- }
- struct AllBusesResponse {
- map<string, vector<string>> bus_stops;
- };
- ostream& operator << (ostream& os, const AllBusesResponse& r) {
- if (r.bus_stops.empty() == true) {
- os << "No buses";
- }
- else {
- bool flag = true;
- for (auto item : r.bus_stops) {
- if (flag == false) {
- os << endl;
- }
- flag = false;
- os << "Bus " << item.first << ":";
- for (const auto& tmp : item.second) {
- os << " " << tmp;
- }
- }
- }
- return os;
- }
- class BusManager {
- public:
- void AddBus(const string& bus, const vector<string>& stops) {
- bus_stops[bus]=stops;
- for (unsigned int i = 0; i < stops.size(); i++) {
- stop_buses[stops[i]].push_back(bus);
- }
- }
- BusesForStopResponse GetBusesForStop(const string& stop) const {
- BusesForStopResponse tmp;
- tmp.stop = stop;
- if (stop_buses.count(stop) != 0){
- tmp.buses = stop_buses.at(stop);
- }
- return tmp;
- }
- StopsForBusResponse GetStopsForBus(const string& bus) const {
- StopsForBusResponse tmp;
- tmp.bus = bus;
- if (bus_stops.count(bus) != 0) {
- for (auto item : bus_stops.at(bus)) {
- if (stop_buses.count(item) != 0) {
- tmp.stop_bus[item] = stop_buses.at(item);
- }
- }
- }
- tmp.bus_stops = bus_stops;
- return tmp;
- }
- AllBusesResponse GetAllBuses() const {
- AllBusesResponse tmp;
- tmp.bus_stops= bus_stops;
- return tmp;
- }
- private:
- map<string, vector<string>> bus_stops,stop_buses;
- };
- void TestNEW_BUS() {
- {
- BusManager bm;
- Query q;
- q.bus = "13";
- q.stops = { "qwe","asd","zxc" };
- bm.AddBus(q.bus, q.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;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement