mstoyanov7

Matching Locations

Jun 28th, 2021 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <unordered_map>
  5. #include <vector>
  6. #include <array>
  7. #include <algorithm>
  8.  
  9. const int CITY_IDX = 0;
  10. const int LATITUDE_IDX = 1;
  11. const int LONGITUDE_IDX = 2;
  12. const int ATTRIBUTES_COUNT = 3;
  13. const std::string lineDelimiter = ".";
  14. const char attributeDelimiter = ',';
  15.  
  16. std::vector<std::array<std::string, ATTRIBUTES_COUNT>> readCityAttributesFromInput() {
  17.     std::vector<std::array<std::string, ATTRIBUTES_COUNT>> attributes;
  18.     std::string line;
  19.    
  20.  
  21.     std::array<std::string, ATTRIBUTES_COUNT> currAttributes;
  22.     while (true) {
  23.         getline(std::cin, line);
  24.         if (line == lineDelimiter) {
  25.             break;
  26.         }
  27.         std::istringstream istr(line);
  28.         getline(istr, currAttributes[CITY_IDX], attributeDelimiter);
  29.         getline(istr, currAttributes[LATITUDE_IDX], attributeDelimiter);
  30.         getline(istr, currAttributes[LONGITUDE_IDX], attributeDelimiter);
  31.         attributes.push_back(currAttributes);
  32.     }
  33.     return attributes;
  34. }
  35.  
  36. std::vector<std::string> readQueriesFromInput() {
  37.     std::vector<std::string> queries;
  38.     std::string line;
  39.    
  40.     while (true) {
  41.         getline(std::cin, line);
  42.         if (line == lineDelimiter) {
  43.             break;
  44.         }
  45.         queries.push_back(line);
  46.     }
  47.     return queries;
  48. }
  49.  
  50. std::unordered_map<std::string, std::string> construnctCityMap(const std::vector<std::array<std::string, ATTRIBUTES_COUNT>>& attributes) {
  51.     std::unordered_map<std::string, std::string> cityMap;
  52.    
  53.     std::string currCoordsStringified;
  54.     for (auto& currAttribute : attributes) {
  55.         currCoordsStringified = currAttribute[LATITUDE_IDX];
  56.         currCoordsStringified.append(",").append(currAttribute[LONGITUDE_IDX]);
  57.         cityMap[currAttribute[CITY_IDX]] = currCoordsStringified;
  58.     }
  59.     return cityMap;
  60. }
  61.  
  62. std::unordered_map<std::string, std::vector<std::string>> construnctCoordsMap(const std::vector<std::array<std::string, ATTRIBUTES_COUNT>>& attributes) {
  63.     std::unordered_map<std::string, std::vector<std::string>> coordMap;
  64.  
  65.     std::string currCoordsStringified;
  66.     for (auto& currAttribute : attributes) {
  67.         currCoordsStringified = currAttribute[LATITUDE_IDX];
  68.         currCoordsStringified.append(",").append(currAttribute[LONGITUDE_IDX]);
  69.         coordMap[currCoordsStringified].push_back(currAttribute[CITY_IDX]);
  70.     }
  71.  
  72.     return coordMap;
  73. }
  74.  
  75. bool isCityQuery(const std::string& query) {
  76.     std::istringstream istr(query);
  77.     const char delimiter = ' ';
  78.     std::string placeHolder;
  79.     getline(istr, placeHolder, delimiter);
  80.  
  81.     if (getline(istr, placeHolder, delimiter) && !placeHolder.empty()) {
  82.         return false;
  83.     }
  84.  
  85.     return true;
  86. }
  87.  
  88. std::string executeCityQuery(const std::unordered_map<std::string, std::string>& cityMap,
  89.                         const std::string& query) {
  90.     std::string result;
  91.     auto it = cityMap.find(query);
  92.     result = it->first;
  93.     result.append(",").append(it->second).append("\n");
  94.  
  95.     return result;
  96. }
  97.  
  98. std::string getProcessedQuery(const std::string& query) {
  99.     std::string processedQuery = query;
  100.     std::replace(processedQuery.begin(), processedQuery.end(), ' ', ',');
  101.     return processedQuery;
  102. }
  103.  
  104. std::string executeCoordQuery(const std::unordered_map<std::string, std::vector<std::string>>& coordMap,
  105.     const std::string& query) {
  106.     std::string result;
  107.     const std::string processedQuery = getProcessedQuery(query);
  108.  
  109.     auto it = coordMap.find(processedQuery);
  110.  
  111.     auto& coords = it->first;
  112.     auto cities = it->second;
  113.  
  114.     for (auto& city : cities) {
  115.         result.append(city).append(",").append(coords).append("\n");
  116.     }
  117.  
  118.     return result;
  119. }
  120.  
  121. std::string executeSingleQuery(const std::unordered_map<std::string, std::string>& cityMap,
  122.     const std::unordered_map<std::string, std::vector<std::string>>& coordMap,
  123.     const std::string& query) {
  124.    
  125.     std::string result;
  126.     const bool isCity = isCityQuery(query);
  127.     if (isCity) {
  128.         result = executeCityQuery(cityMap, query);
  129.     }
  130.     else {
  131.         result = executeCoordQuery(coordMap, query);
  132.     }
  133.      
  134.     return result;
  135. }
  136.  
  137. std::vector<std::string> executeQueries(const std::unordered_map<std::string, std::string>& cityMap,
  138.     const std::unordered_map<std::string, std::vector<std::string>>& coordMap,
  139.     const std::vector<std::string>& queries) {
  140.    
  141.     std::vector<std::string> results(queries.size());
  142.     for (size_t i = 0; i < queries.size(); ++i) {
  143.         results[i] = executeSingleQuery(cityMap, coordMap, queries[i]);
  144.     }
  145.  
  146.     return results;
  147. }
  148.  
  149. void printResult(const std::vector<std::string>& results) {
  150.     for (const auto& result : results) {
  151.         std::cout << result;
  152.     }
  153. }
  154.  
  155. int main() {
  156.     const auto cityAttributes = readCityAttributesFromInput();
  157.     const auto queries = readQueriesFromInput();
  158.     const auto cityMap = construnctCityMap(cityAttributes);
  159.     const auto coordMap = construnctCoordsMap(cityAttributes);
  160.     const auto results = executeQueries(cityMap, coordMap, queries);
  161.     printResult(results);
  162. }
Add Comment
Please, Sign In to add comment