DimovIvan

Advanced - Retake Exam - Data2

May 27th, 2022 (edited)
624
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<string>
  3. #include<sstream>
  4. #include<vector>
  5. #include<algorithm>
  6. #include<utility>
  7. #include<array>
  8. #include<unordered_map>
  9.  
  10. const int maxSizeEntities = 300;
  11. const int maxQueriesSize = 300;
  12.  
  13. std::array<std::unordered_map<std::string, std::string>, maxSizeEntities> readEntities(int& sizeEntities) {
  14.     std::array<std::unordered_map<std::string, std::string>, maxSizeEntities> entities{};
  15.  
  16.     sizeEntities = 0;
  17.     const std::string delimiter = "[queries]";
  18.     std::string line;
  19.     getline(std::cin, line);
  20.  
  21.     while (line != delimiter)
  22.     {
  23.         std::unordered_map<std::string, std::string> currEntity{};
  24.         std::istringstream istr(line);
  25.         std::string currKey;
  26.         std::string currValue;
  27.         while (istr >> currKey)
  28.         {
  29.             istr >> currValue;
  30.             currEntity[currKey] = currValue;
  31.         }
  32.         entities[sizeEntities] = currEntity;
  33.         sizeEntities++;
  34.         getline(std::cin, line);
  35.     }
  36.     return entities;
  37. }
  38.  
  39. std::array<std::pair<std::string, std::string>, maxQueriesSize> readQueries(int& queriesSize) {
  40.     std::array<std::pair<std::string, std::string>, maxQueriesSize> queries{};
  41.  
  42.     queriesSize = 0;
  43.     std::pair<std::string, std::string> currQuery;
  44.     const std::string delimiter = "end";
  45.     while (true)
  46.     {
  47.         std::cin >> currQuery.first;
  48.         if (currQuery.first == delimiter)
  49.         {
  50.             break;
  51.         }
  52.         std::cin >> currQuery.second;
  53.         queries[queriesSize] = currQuery;
  54.         queriesSize++;
  55.     }
  56.     return queries;
  57. }
  58.  
  59. std::vector<std::string> processSingleQuery(const std::array<std::unordered_map<std::string, std::string>, maxSizeEntities>& entities,
  60.                                             const int sizeEntities, const std::pair<std::string, std::string>& query) {
  61.     std::vector<std::string> resultSingleQuery{};
  62.     bool isFound = false;
  63.     for (size_t j = 0; j < sizeEntities; j++)
  64.     {
  65.         bool isFoundInCurrEntity = false;
  66.         for (const auto& currFieldValue : entities[j])
  67.         {
  68.             if (currFieldValue.second == query.first)
  69.             {
  70.                 isFoundInCurrEntity = true;
  71.                 isFound = true;
  72.                 break;
  73.             }
  74.         }
  75.         if (isFoundInCurrEntity)
  76.         {
  77.             const auto it = entities[j].find(query.second);
  78.             if (it != entities[j].end())
  79.             {
  80.                 resultSingleQuery.push_back(it->second);
  81.             }          
  82.         }
  83.     }
  84.     if (!isFound)
  85.     {
  86.         resultSingleQuery.push_back("[not found]");
  87.     }
  88.     return resultSingleQuery;
  89. }
  90.  
  91. std::array<std::vector<std::string>, maxQueriesSize> getSolution(const std::array<std::unordered_map<std::string, std::string>, maxSizeEntities>& entities,
  92.                                                                  const int sizeEntities,
  93.                                                                  const std::array<std::pair<std::string, std::string>, maxQueriesSize>& queries,
  94.                                                                  const int queriesSize) {
  95.     std::array<std::vector<std::string>, maxQueriesSize> solution{};
  96.     for (size_t i = 0; i < queriesSize; i++)
  97.     {
  98.         solution[i] = processSingleQuery(entities, sizeEntities, queries[i]);
  99.     }
  100.  
  101.     return solution;
  102. }
  103.  
  104. void printArrSolution(const std::array<std::vector<std::string>, maxQueriesSize>& solution, const int queriesSize) {
  105.     for (size_t i = 0; i < queriesSize; i++)
  106.     {
  107.         for (const auto& field : solution[i])
  108.         {
  109.             std::cout << field << ' ';
  110.         }
  111.         std::cout << std::endl;
  112.     }
  113. }
  114. int main() {
  115.    int sizeEntities = 0;
  116.    const  std::array<std::unordered_map<std::string, std::string>, maxSizeEntities> entities = readEntities(sizeEntities);
  117.    int queriesSize = 0;
  118.    const std::array<std::pair<std::string, std::string>, maxQueriesSize> queries = readQueries(queriesSize);
  119.    const std::array<std::vector<std::string>, maxQueriesSize> solution = getSolution(entities, sizeEntities, queries, queriesSize);
  120.    printArrSolution(solution, queriesSize);
  121.  
  122.     return 0;
  123. }
RAW Paste Data Copied