DimovIvan

C++Advanced-Retake Exam-Data

Jul 10th, 2021 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<sstream>
  4. #include<vector>
  5. #include<algorithm>
  6. #include<utility>
  7. #include<array>
  8.  
  9. const int maxSizeEntities = 300;
  10. const int maxQueriesSize = 300;
  11.  
  12. std::array<std::vector<std::pair<std::string, std::string>>, maxSizeEntities> readEntities(int& sizeEntities) {
  13.     std::array<std::vector<std::pair<std::string, std::string>>, maxSizeEntities> entities{};
  14.  
  15.     sizeEntities = 0;
  16.     const std::string delimiter = "[queries]";
  17.     std::string line;
  18.     getline(std::cin, line);
  19.    
  20.     while (line != delimiter)
  21.     {
  22.         std::istringstream istr(line);
  23.         std::string currWord;
  24.         std::vector<std::pair<std::string, std::string>> currEntity{};
  25.         std::pair<std::string, std::string> currFieldValue{};
  26.         while (istr >> currWord)
  27.         {
  28.            
  29.             currFieldValue.first = currWord;
  30.             istr >> currFieldValue.second;
  31.             currEntity.push_back(currFieldValue);
  32.         }
  33.        
  34.         entities[sizeEntities] = currEntity;
  35.         sizeEntities++;
  36.         getline(std::cin, line);
  37.     }
  38.     return entities;
  39. }
  40.  
  41. std::array<std::pair<std::string, std::string>, maxQueriesSize> readQueries(int& queriesSize) {
  42.     std::array<std::pair<std::string, std::string>, maxQueriesSize> queries{};
  43.  
  44.     queriesSize = 0;
  45.     std::pair<std::string, std::string> currQuery;
  46.     const std::string delimiter = "end";
  47.     while (true)
  48.     {
  49.         std::cin >> currQuery.first;
  50.         if (currQuery.first == delimiter)
  51.         {
  52.             break;
  53.         }
  54.         std::cin >> currQuery.second;
  55.         queries[queriesSize] = currQuery;
  56.         queriesSize++;
  57.     }
  58.     return queries;
  59. }
  60.  
  61. std::vector<std::vector<std::string>> getSolution(const std::array<std::vector<std::pair<std::string, std::string>>,maxSizeEntities>&  entities,
  62.                                                   const int sizeEntities,
  63.                                                   const std::array<std::pair<std::string, std::string>, maxQueriesSize>& queries,
  64.                                                   const int queriesSize) {
  65.     std::vector<std::vector<std::string>> solution{};
  66.  
  67.     for (size_t i = 0; i < queriesSize; i++)
  68.     {
  69.         std::vector<std::string> currQueryResult{};
  70.         bool isFound = false;
  71.         for (size_t j = 0; j < sizeEntities; j++)
  72.         {
  73.             bool isFoundInCurrEntity = false;
  74.             for (const auto& currFieldValue : entities[j])
  75.             {
  76.                 if (currFieldValue.second == queries[i].first)
  77.                 {
  78.                     isFoundInCurrEntity = true;
  79.                     isFound = true;
  80.                     break;
  81.                 }
  82.             }
  83.             if (isFoundInCurrEntity)
  84.             {
  85.                 for (const auto& currFieldValue : entities[j]) {
  86.                     if (currFieldValue.first == queries[i].second)
  87.                     {
  88.                         currQueryResult.push_back(currFieldValue.second);
  89.                         break;
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.         if (!isFound)
  95.         {
  96.             currQueryResult.push_back("[not found]");
  97.         }
  98.         solution.push_back(currQueryResult);
  99.     }
  100.  
  101.     return solution;
  102. }
  103.  
  104. void printSolution(const std::vector<std::vector<std::string>>& solution) {
  105.     for (const auto& currLine : solution)
  106.     {
  107.         for (const auto& word : currLine)
  108.         {
  109.             std::cout << word << ' ';
  110.         }
  111.         std::cout << std::endl;
  112.     }
  113. }
  114.  
  115. int main() {
  116.    int sizeEntities = 0;
  117.    const std::array<std::vector<std::pair<std::string, std::string>>, maxSizeEntities>  entities = readEntities(sizeEntities);
  118.    int queriesSize = 0;
  119.    const std::array<std::pair<std::string, std::string>, maxQueriesSize> queries = readQueries(queriesSize);
  120.    const std::vector<std::vector<std::string>> solution = getSolution(entities, sizeEntities, queries, queriesSize);
  121.    printSolution(solution);
  122.  
  123.    /*for (size_t i = 0; i < sizeEntities; i++)
  124.    {
  125.        for (const auto& fieldValue : entities[i])
  126.        {
  127.            std::cout << fieldValue.first << ' ' << fieldValue.second << std::endl;
  128.        }
  129.        std::cout << std::endl;
  130.    }
  131.    for (size_t i = 0; i < queriesSize; i++)
  132.    {
  133.        std::cout << queries[i].first << ' ' << queries[i].second << std::endl;
  134.    }*/
  135.  
  136.     return 0;
  137. }
  138.  
  139.  
  140.  
Add Comment
Please, Sign In to add comment