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 <set>
- 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 operation_code;
- is >> operation_code;
- if (operation_code == "NEW_BUS"s) {
- q.type = QueryType::NewBus;
- is >> q.bus;
- int stop_count;
- is >> stop_count;
- q.stops.clear();
- for (int i = 0; i < stop_count; ++i) {
- string stop_word;
- is >> stop_word;
- q.stops.push_back(stop_word);
- }
- }
- else if (operation_code == "BUSES_FOR_STOP"s) {
- is >> q.stop;
- q.type = QueryType::BusesForStop;
- }
- else if (operation_code == "STOPS_FOR_BUS"s) {
- is >> q.bus;
- q.type = QueryType::StopsForBus;
- }
- else if (operation_code == "ALL_BUSES"s) {
- q.type = QueryType::AllBuses;
- }
- return is;
- }
- struct BusesForStopResponse {
- // Наполните полями эту структуру
- vector<string> buses;
- bool no_stop;
- };
- ostream& operator<<(ostream& os, const BusesForStopResponse& r) {
- // Реализуйте эту функцию
- if (r.no_stop == true) {
- os << "No stop"s;
- }
- else {
- bool is_first = true;
- for (const auto& a : r.buses) {
- if (is_first) {
- os << a;
- is_first = false;
- }
- else {
- os << " "s << a;
- }
- }
- }
- return os;
- }
- struct StopsForBusResponse {
- //Наполните полями эту структуру
- //set<string> interchange;
- vector<string> stops;
- map<string, vector<string>> stop_to_buses;
- bool no_stop = false;
- };
- ostream& operator<<(ostream& os, const StopsForBusResponse& r) {
- // Реализуйте эту функцию
- if (r.no_stop == true) {
- os << "No bus"s;
- }
- else {
- int count = 0;
- for (string stop : r.stops) {
- bool is_first = true;
- os << "Stop "s << stop << ": "s;
- for (auto a : r.stop_to_buses.at(stop)) {
- if (is_first) {
- os << a;
- is_first = false;
- }
- else {
- os << " "s << a;
- }
- }
- ++count;
- if (count != r.stops.size()) {
- os << endl;
- }
- }
- }
- return os;
- }
- struct AllBusesResponse {
- // Наполните полями эту структуру
- map<string, vector<string>> bus_to_stops;
- bool no_stop;
- };
- ostream& operator<<(ostream& os, const AllBusesResponse& r) {
- // Реализуйте эту функцию
- if (r.no_stop == true) {
- os << "No buses"s;
- }
- else {
- for (const auto& [bus, stops] : r.bus_to_stops) {
- os << "Bus "s << bus << ": "s;
- bool is_first = true;
- for (const string& stop : stops) {
- if (is_first) {
- os << stop;
- is_first = false;
- }
- else {
- os << " "s << stop;
- }
- }
- os << endl;
- }
- }
- return os;
- }
- class BusManager {
- public:
- void AddBus(const string& bus, const vector<string>& stops) {
- // Реализуйте этот метод
- for (const string& stop : stops) {
- bus_stops_[bus].push_back(stop);
- stop_bases_[stop].push_back(bus);
- }
- }
- BusesForStopResponse GetBusesForStop(const string& stop) const {
- // Реализуйте этот метод
- BusesForStopResponse finish;
- if (stop_bases_.count(stop) == 0) {
- finish.no_stop = true;
- }
- else {
- finish.no_stop = false;
- for (const auto& a : stop_bases_.at(stop)) {
- finish.buses.push_back(a);
- }
- }
- return finish;
- }
- StopsForBusResponse GetStopsForBus(const string& bus) const {
- StopsForBusResponse finish;
- vector <string> interchange = { "no interchange"s };
- if (bus_stops_.count(bus) == 0) {
- finish.no_stop = true;
- }
- else {
- for (const string& stop : bus_stops_.at(bus)) {
- finish.stops.push_back(stop);
- if (stop_bases_.at(stop).size() == 1) {
- finish.stop_to_buses[stop] = interchange;
- }
- else {
- for (const string& other_bus : stop_bases_.at(stop)) {
- if (bus != other_bus) {
- finish.stop_to_buses[stop].push_back(other_bus);
- }
- }
- }
- }
- }
- return finish;
- }
- AllBusesResponse GetAllBuses() const {
- // Реализуйте этот метод
- AllBusesResponse finish;
- if (bus_stops_.empty()) {
- finish.no_stop = true;
- }
- else {
- finish.no_stop = false;
- finish.bus_to_stops = bus_stops_;
- /*for (const auto& [bus, stops] : bus_stops_) {
- for (const string& stop : stops) {
- finish.bus_to_stops[bus].push_back(stop);
- }
- }*/
- }
- return finish;
- }
- private:
- map<string, vector<string>> bus_stops_, stop_bases_;
- };
- 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