Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <iostream>
- #include <map>
- #include <string>
- #include <vector>
- #include <sstream>
- #include <fstream>
- using namespace std;
- //string path = "C:/Users/musta/Downloads/nova.txt";
- //fstream f(path);
- enum class QueryType
- {
- NewBus,
- BusesForStop,
- StopsForBus,
- AllBuses,
- };
- struct Query
- {
- QueryType type;
- string bus;
- string stop;
- vector<string> stops;
- };
- istream& operator>>(istream& is, QueryType& query)
- {
- string type;
- is >> type;
- if (type == "NEW_BUS")
- {
- query = QueryType::NewBus;
- }
- else if (type == "BUSES_FOR_STOP")
- {
- query = QueryType::BusesForStop;
- }
- else if (type == "STOPS_FOR_BUS")
- {
- query = QueryType::StopsForBus;
- }
- else
- {
- query = QueryType::AllBuses;
- }
- return is;
- }
- istream& operator>>(istream& is, vector<string>& q)
- {
- int count = 0;
- cin >> count;
- q.resize(count);
- for (int i = 0; i < count; ++i)
- {
- is >> q[i];
- }
- return is;
- }
- istream& operator>>(istream& is, Query& q)
- {
- is >> q.type;
- switch (q.type)
- {
- case QueryType::NewBus:
- is >> q.bus;
- is >> q.stops;
- break;
- case QueryType::BusesForStop:
- is >> q.stop;
- break;
- case QueryType::StopsForBus:
- is >> q.bus;
- break;
- default:
- break;
- }
- return is;
- }
- struct BusesForStopResponse
- {
- string response;
- };
- ostream& operator<<(ostream& os, const BusesForStopResponse& r)
- {
- istringstream ir(r.response);
- bool first = true;
- for (string str; getline(ir, str, ' ');)
- {
- if (first)
- {
- os << str;
- first = false;
- }
- else
- {
- os << " " << str;
- }
- }
- return os;
- }
- struct StopsForBusResponse
- {
- vector <string> response;
- };
- ostream& operator<<(ostream& os, const StopsForBusResponse& r)
- {
- int count = 1;
- for (const auto& text : r.response)
- {
- istringstream ir(text);
- bool first = true;
- for (string str; getline(ir, str, ' ');)
- {
- if (first)
- {
- os << str;
- first = false;
- }
- else
- {
- os << " " << str;
- }
- }
- if (count < r.response.size())
- {
- os << endl;
- ++count;
- }
- }
- return os;
- }
- struct AllBusesResponse
- {
- // Наполните полями эту структуру
- vector <string> response;
- };
- ostream& operator<<(ostream& os, const AllBusesResponse& r)
- {
- int count = 1;
- for (const auto& text : r.response)
- {
- istringstream ir(text);
- bool first = true;
- for (string str; getline(ir, str, ' ');)
- {
- if (first)
- {
- os << str;
- first = false;
- }
- else
- {
- os << " " << str;
- }
- }
- if (count < r.response.size())
- {
- os << endl;
- ++count;
- }
- }
- return os;
- }
- class BusManager
- {
- public:
- void AddBus(const string& bus, const vector<string>& stops)
- {
- buses_to_stops[bus] = stops;
- for (const string& stop : stops) {
- stops_to_buses[stop].push_back(bus);
- }
- }
- BusesForStopResponse GetBusesForStop(const string& stop) const
- {
- BusesForStopResponse bs;
- stringstream ss;
- if (stops_to_buses.count(stop) == 0) {
- ss << "No stop"s;
- }
- else {
- bool is_first = true;
- for (const string& bus : stops_to_buses.at(stop))
- {
- if (!is_first)
- {
- ss << " "s;
- }
- is_first = false;
- ss << bus;
- }
- }
- bs.response = ss.str();
- return bs;
- }
- StopsForBusResponse GetStopsForBus(const string& bus) const
- {
- StopsForBusResponse rs;
- vector<string> v;
- stringstream ss;
- if (buses_to_stops.count(bus) == 0)
- {
- ss << "No bus"s;
- v.push_back(ss.str());
- }
- else {
- for (const string& stop : buses_to_stops.at(bus))
- {
- ss << "Stop "s << stop << ":"s;
- if (stops_to_buses.at(stop).size() == 1)
- {
- ss << " no interchange"s;
- }
- else {
- for (const string& other_bus : stops_to_buses.at(stop))
- {
- if (bus != other_bus)
- {
- ss << " "s << other_bus;
- }
- }
- }
- v.push_back(ss.str());
- ss.str(string());
- }
- }
- rs.response = v;
- return rs;
- }
- AllBusesResponse GetAllBuses() const
- {
- AllBusesResponse ar;
- vector<string> result;
- stringstream ss;
- if (buses_to_stops.empty())
- {
- ss << "No buses"s;
- result.push_back(ss.str());
- }
- else
- {
- for (const auto& bus_item : buses_to_stops)
- {
- ss << "Bus "s << bus_item.first << ":"s;
- for (const string& stop : bus_item.second)
- {
- ss << " "s << stop;
- }
- result.push_back(ss.str());
- ss.str(string());
- }
- }
- ar.response = result;
- return ar;
- }
- private:
- map<string, vector<string>> buses_to_stops; // автобус и остановки через которые он проходит
- map<string, vector<string>> stops_to_buses; // остановка и какие автобусы проходят, через эту остановку
- };
- // Реализуйте функции и классы, объявленные выше, чтобы эта функция main
- // решала задачу "Автобусные остановки"
- 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