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>
- const int maxSizeEntities = 300;
- const int maxQueriesSize = 300;
- std::array<std::vector<std::pair<std::string, std::string>>, maxSizeEntities> readEntities(int& sizeEntities) {
- std::array<std::vector<std::pair<std::string, std::string>>, maxSizeEntities> entities{};
- sizeEntities = 0;
- const std::string delimiter = "[queries]";
- std::string line;
- getline(std::cin, line);
- while (line != delimiter)
- {
- std::istringstream istr(line);
- std::string currWord;
- std::vector<std::pair<std::string, std::string>> currEntity{};
- std::pair<std::string, std::string> currFieldValue{};
- while (istr >> currWord)
- {
- currFieldValue.first = currWord;
- istr >> currFieldValue.second;
- currEntity.push_back(currFieldValue);
- }
- 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::vector<std::string>> getSolution(const std::array<std::vector<std::pair<std::string, std::string>>,maxSizeEntities>& entities,
- const int sizeEntities,
- const std::array<std::pair<std::string, std::string>, maxQueriesSize>& queries,
- const int queriesSize) {
- std::vector<std::vector<std::string>> solution{};
- for (size_t i = 0; i < queriesSize; i++)
- {
- std::vector<std::string> currQueryResult{};
- bool isFound = false;
- for (size_t j = 0; j < sizeEntities; j++)
- {
- bool isFoundInCurrEntity = false;
- for (const auto& currFieldValue : entities[j])
- {
- if (currFieldValue.second == queries[i].first)
- {
- isFoundInCurrEntity = true;
- isFound = true;
- break;
- }
- }
- if (isFoundInCurrEntity)
- {
- for (const auto& currFieldValue : entities[j]) {
- if (currFieldValue.first == queries[i].second)
- {
- currQueryResult.push_back(currFieldValue.second);
- break;
- }
- }
- }
- }
- if (!isFound)
- {
- currQueryResult.push_back("[not found]");
- }
- solution.push_back(currQueryResult);
- }
- return solution;
- }
- void printSolution(const std::vector<std::vector<std::string>>& solution) {
- for (const auto& currLine : solution)
- {
- for (const auto& word : currLine)
- {
- std::cout << word << ' ';
- }
- std::cout << std::endl;
- }
- }
- int main() {
- int sizeEntities = 0;
- const std::array<std::vector<std::pair<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::vector<std::vector<std::string>> solution = getSolution(entities, sizeEntities, queries, queriesSize);
- printSolution(solution);
- /*for (size_t i = 0; i < sizeEntities; i++)
- {
- for (const auto& fieldValue : entities[i])
- {
- std::cout << fieldValue.first << ' ' << fieldValue.second << std::endl;
- }
- std::cout << std::endl;
- }
- for (size_t i = 0; i < queriesSize; i++)
- {
- std::cout << queries[i].first << ' ' << queries[i].second << std::endl;
- }*/
- return 0;
- }
Add Comment
Please, Sign In to add comment