Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <sstream>
- #include <unordered_map>
- std::vector<std::string> readSingleLine()
- {
- std::vector<std::string>result;
- std::string input;
- getline(std::cin, input);
- std::istringstream istr(input);
- std::string word;
- while (istr >> word)
- {
- result.push_back(word);
- }
- return result;
- }
- typedef std::unordered_map<std::string, int> ValueContainer;
- void populateContiner(const std::vector<std::string> & currLine, std::vector<ValueContainer> & containers)
- {
- for (size_t i = 0; i < currLine.size(); ++i)
- {
- ++(containers[i][currLine[i]]);
- }
- }
- int getIndexFromHeader(const std::vector<std::string> & header, const std::string & query)
- {
- int index = -1;
- for (int i = 0; i < (int)header.size; ++i)
- {
- if (header[i] == query)
- {
- index = i;
- break;
- }
- }
- return index;
- }
- void printMostCommonElement(const ValueContainer & container)
- {
- int currMax = -1;
- ValueContainer::const_iterator maxPair;
- for (ValueContainer::const_iterator it = container.begin(); it != container.end(); ++it)
- {
- if (it->second > maxPair->second)
- {
- currMax = it->second;
- maxPair = it;
- }
- }
- std::cout << maxPair->first << ' ' << maxPair->second << "\n";
- }
- int main()
- {
- const std::vector<std::string>readSingleLine();
- std::vector<ValueContainer> containers(header.size());
- std::string input;
- while (true)
- {
- const std::vector<std::string> currLine = readSingleLine();
- if (currLine[0] == "end")
- {
- break;
- }
- populateContiner(currLine, containers);
- }
- getline(std::cin, input);
- const int IDX = getIndexFromHeader(header, input);
- if (-1 == getIndexFromHeader(header, input))
- {
- std::cerr << "Warning, getINdexFromHeader() failed" << "\n";
- return 1;
- }
- printMostCommonElement(containers[IDX]);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement