Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <unordered_map>
- #include <vector>
- #include <array>
- #include <algorithm>
- const int CITY_IDX = 0;
- const int LATITUDE_IDX = 1;
- const int LONGITUDE_IDX = 2;
- const int ATTRIBUTES_COUNT = 3;
- const std::string lineDelimiter = ".";
- const char attributeDelimiter = ',';
- std::vector<std::array<std::string, ATTRIBUTES_COUNT>> readCityAttributesFromInput() {
- std::vector<std::array<std::string, ATTRIBUTES_COUNT>> attributes;
- std::string line;
- std::array<std::string, ATTRIBUTES_COUNT> currAttributes;
- while (true) {
- getline(std::cin, line);
- if (line == lineDelimiter) {
- break;
- }
- std::istringstream istr(line);
- getline(istr, currAttributes[CITY_IDX], attributeDelimiter);
- getline(istr, currAttributes[LATITUDE_IDX], attributeDelimiter);
- getline(istr, currAttributes[LONGITUDE_IDX], attributeDelimiter);
- attributes.push_back(currAttributes);
- }
- return attributes;
- }
- std::vector<std::string> readQueriesFromInput() {
- std::vector<std::string> queries;
- std::string line;
- while (true) {
- getline(std::cin, line);
- if (line == lineDelimiter) {
- break;
- }
- queries.push_back(line);
- }
- return queries;
- }
- std::unordered_map<std::string, std::string> construnctCityMap(const std::vector<std::array<std::string, ATTRIBUTES_COUNT>>& attributes) {
- std::unordered_map<std::string, std::string> cityMap;
- std::string currCoordsStringified;
- for (auto& currAttribute : attributes) {
- currCoordsStringified = currAttribute[LATITUDE_IDX];
- currCoordsStringified.append(",").append(currAttribute[LONGITUDE_IDX]);
- cityMap[currAttribute[CITY_IDX]] = currCoordsStringified;
- }
- return cityMap;
- }
- std::unordered_map<std::string, std::vector<std::string>> construnctCoordsMap(const std::vector<std::array<std::string, ATTRIBUTES_COUNT>>& attributes) {
- std::unordered_map<std::string, std::vector<std::string>> coordMap;
- std::string currCoordsStringified;
- for (auto& currAttribute : attributes) {
- currCoordsStringified = currAttribute[LATITUDE_IDX];
- currCoordsStringified.append(",").append(currAttribute[LONGITUDE_IDX]);
- coordMap[currCoordsStringified].push_back(currAttribute[CITY_IDX]);
- }
- return coordMap;
- }
- bool isCityQuery(const std::string& query) {
- std::istringstream istr(query);
- const char delimiter = ' ';
- std::string placeHolder;
- getline(istr, placeHolder, delimiter);
- if (getline(istr, placeHolder, delimiter) && !placeHolder.empty()) {
- return false;
- }
- return true;
- }
- std::string executeCityQuery(const std::unordered_map<std::string, std::string>& cityMap,
- const std::string& query) {
- std::string result;
- auto it = cityMap.find(query);
- result = it->first;
- result.append(",").append(it->second).append("\n");
- return result;
- }
- std::string getProcessedQuery(const std::string& query) {
- std::string processedQuery = query;
- std::replace(processedQuery.begin(), processedQuery.end(), ' ', ',');
- return processedQuery;
- }
- std::string executeCoordQuery(const std::unordered_map<std::string, std::vector<std::string>>& coordMap,
- const std::string& query) {
- std::string result;
- const std::string processedQuery = getProcessedQuery(query);
- auto it = coordMap.find(processedQuery);
- auto& coords = it->first;
- auto cities = it->second;
- for (auto& city : cities) {
- result.append(city).append(",").append(coords).append("\n");
- }
- return result;
- }
- std::string executeSingleQuery(const std::unordered_map<std::string, std::string>& cityMap,
- const std::unordered_map<std::string, std::vector<std::string>>& coordMap,
- const std::string& query) {
- std::string result;
- const bool isCity = isCityQuery(query);
- if (isCity) {
- result = executeCityQuery(cityMap, query);
- }
- else {
- result = executeCoordQuery(coordMap, query);
- }
- return result;
- }
- std::vector<std::string> executeQueries(const std::unordered_map<std::string, std::string>& cityMap,
- const std::unordered_map<std::string, std::vector<std::string>>& coordMap,
- const std::vector<std::string>& queries) {
- std::vector<std::string> results(queries.size());
- for (size_t i = 0; i < queries.size(); ++i) {
- results[i] = executeSingleQuery(cityMap, coordMap, queries[i]);
- }
- return results;
- }
- void printResult(const std::vector<std::string>& results) {
- for (const auto& result : results) {
- std::cout << result;
- }
- }
- int main() {
- const auto cityAttributes = readCityAttributesFromInput();
- const auto queries = readQueriesFromInput();
- const auto cityMap = construnctCityMap(cityAttributes);
- const auto coordMap = construnctCoordsMap(cityAttributes);
- const auto results = executeQueries(cityMap, coordMap, queries);
- printResult(results);
- }
Add Comment
Please, Sign In to add comment