Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<sstream>
- #include<vector>
- #include<algorithm>
- #include<utility>
- #include<array>
- #include<unordered_map>
- const int maxSizeEntities = 300;
- const int maxQueriesSize = 300;
- std::array<std::unordered_map<std::string, std::string>, maxSizeEntities> readEntities(int& sizeEntities) {
- std::array<std::unordered_map<std::string, std::string>, maxSizeEntities> entities{};
- sizeEntities = 0;
- const std::string delimiter = "[queries]";
- std::string line;
- getline(std::cin, line);
- while (line != delimiter)
- {
- std::unordered_map<std::string, std::string> currEntity{};
- std::istringstream istr(line);
- std::string currKey;
- std::string currValue;
- while (istr >> currKey)
- {
- istr >> currValue;
- currEntity[currKey] = currValue;
- }
- entities[sizeEntities] = currEntity;
- sizeEntities++;
- getline(std::cin, line);
- }
- return entities;
- }
- std::array<std::pair<std::string, std::string>, maxQueriesSize> readQueries(int& queriesSize) {
- std::array<std::pair<std::string, std::string>, maxQueriesSize> queries{};
- queriesSize = 0;
- std::pair<std::string, std::string> currQuery;
- const std::string delimiter = "end";
- while (true)
- {
- std::cin >> currQuery.first;
- if (currQuery.first == delimiter)
- {
- break;
- }
- std::cin >> currQuery.second;
- queries[queriesSize] = currQuery;
- queriesSize++;
- }
- return queries;
- }
- std::vector<std::string> processSingleQuery(const std::array<std::unordered_map<std::string, std::string>, maxSizeEntities>& entities,
- const int sizeEntities, const std::pair<std::string, std::string>& query) {
- std::vector<std::string> resultSingleQuery{};
- bool isFound = false;
- for (size_t j = 0; j < sizeEntities; j++)
- {
- bool isFoundInCurrEntity = false;
- for (const auto& currFieldValue : entities[j])
- {
- if (currFieldValue.second == query.first)
- {
- isFoundInCurrEntity = true;
- isFound = true;
- break;
- }
- }
- if (isFoundInCurrEntity)
- {
- const auto it = entities[j].find(query.second);
- if (it != entities[j].end())
- {
- resultSingleQuery.push_back(it->second);
- }
- }
- }
- if (!isFound)
- {
- resultSingleQuery.push_back("[not found]");
- }
- return resultSingleQuery;
- }
- std::array<std::vector<std::string>, maxQueriesSize> getSolution(const std::array<std::unordered_map<std::string, std::string>, maxSizeEntities>& entities,
- const int sizeEntities,
- const std::array<std::pair<std::string, std::string>, maxQueriesSize>& queries,
- const int queriesSize) {
- std::array<std::vector<std::string>, maxQueriesSize> solution{};
- for (size_t i = 0; i < queriesSize; i++)
- {
- solution[i] = processSingleQuery(entities, sizeEntities, queries[i]);
- }
- return solution;
- }
- void printArrSolution(const std::array<std::vector<std::string>, maxQueriesSize>& solution, const int queriesSize) {
- for (size_t i = 0; i < queriesSize; i++)
- {
- for (const auto& field : solution[i])
- {
- std::cout << field << ' ';
- }
- std::cout << std::endl;
- }
- }
- int main() {
- int sizeEntities = 0;
- const std::array<std::unordered_map<std::string, std::string>, maxSizeEntities> entities = readEntities(sizeEntities);
- int queriesSize = 0;
- const std::array<std::pair<std::string, std::string>, maxQueriesSize> queries = readQueries(queriesSize);
- const std::array<std::vector<std::string>, maxQueriesSize> solution = getSolution(entities, sizeEntities, queries, queriesSize);
- printArrSolution(solution, queriesSize);
- return 0;
- }
Add Comment
Please, Sign In to add comment