Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<unordered_set>
- #include<unordered_map>
- #include<string>
- #include<algorithm>
- std::vector<std::string> readInput() {
- std::vector< std::string > words;
- std::string delimiter = "stop";
- std::string currWord;
- std::cin >> currWord;
- while (currWord != delimiter)
- {
- words.push_back(currWord);
- std::cin >> currWord;
- }
- return words;
- }
- std::unordered_map<std::string, int> getResources(const std::vector< std::string >& words) {
- std::unordered_map<std::string, int> resources;
- size_t wordsSize = words.size();
- for (size_t i = 0; i < wordsSize - 1; i++)
- {
- if ((i + 1) % 2 != 0)
- {
- resources[words[i]] += std::stoi(words[i + 1]);
- }
- }
- return resources;
- }
- void printSolution(const std::vector<std::string>& wordsInput,
- const std::unordered_map<std::string, int>& resources)
- {
- std::unordered_set<std::string> unicResourses;
- const size_t wordsInputSize = wordsInput.size();
- for (size_t i = 0; i < wordsInputSize; i += 2)
- {
- const auto it = resources.find(wordsInput[i]);
- if (unicResourses.find(it->first) == unicResourses.end())
- {
- std::cout << it->first << " -> " << it->second << std::endl;
- unicResourses.insert(it->first);
- }
- }
- }
- int main() {
- const auto wordsInput = readInput();
- const std::unordered_map<std::string, int> resources = getResources(wordsInput);
- printSolution(wordsInput,resources);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment