Advertisement
Guest User

Untitled

a guest
Nov 9th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <sstream>
  5. #include <unordered_map>
  6.  
  7. std::vector<std::string> readSingleLine()
  8. {
  9.     std::vector<std::string>result;
  10.  
  11.     std::string input;
  12.     getline(std::cin, input);
  13.  
  14.     std::istringstream istr(input);
  15.     std::string word;
  16.  
  17.     while (istr >> word)
  18.     {
  19.         result.push_back(word);
  20.     }
  21.  
  22.     return result;
  23. }
  24.  
  25. typedef std::unordered_map<std::string, int> ValueContainer;
  26.  
  27. void populateContiner(const std::vector<std::string> & currLine, std::vector<ValueContainer> & containers)
  28. {
  29.     for (size_t i = 0; i < currLine.size(); ++i)
  30.     {
  31.         ++(containers[i][currLine[i]]);
  32.     }
  33. }
  34.  
  35. int getIndexFromHeader(const std::vector<std::string> & header, const std::string & query)
  36. {
  37.     int index = -1;
  38.  
  39.     for (int i = 0; i < (int)header.size; ++i)
  40.     {
  41.         if (header[i] == query)
  42.         {
  43.             index = i;
  44.             break;
  45.         }
  46.     }
  47.  
  48.     return index;
  49. }
  50.  
  51. void printMostCommonElement(const ValueContainer & container)
  52. {
  53.     int currMax = -1;
  54.  
  55.     ValueContainer::const_iterator maxPair;
  56.  
  57.     for (ValueContainer::const_iterator it = container.begin(); it != container.end(); ++it)
  58.     {
  59.         if (it->second > maxPair->second)
  60.         {
  61.             currMax = it->second;
  62.             maxPair = it;
  63.         }
  64.     }
  65.  
  66.     std::cout << maxPair->first << ' ' << maxPair->second << "\n";
  67. }
  68.  
  69. int main()
  70. {
  71.     const std::vector<std::string>readSingleLine();
  72.     std::vector<ValueContainer> containers(header.size());
  73.  
  74.     std::string input;
  75.     while (true)
  76.     {
  77.         const std::vector<std::string> currLine = readSingleLine();
  78.        
  79.         if (currLine[0] == "end")
  80.         {
  81.             break;
  82.         }
  83.  
  84.         populateContiner(currLine, containers);
  85.     }
  86.  
  87.     getline(std::cin, input);
  88.  
  89.     const int IDX = getIndexFromHeader(header, input);
  90.  
  91.     if (-1 == getIndexFromHeader(header, input))
  92.     {
  93.         std::cerr << "Warning, getINdexFromHeader() failed" << "\n";
  94.  
  95.         return 1;
  96.     }
  97.  
  98.     printMostCommonElement(containers[IDX]);
  99.  
  100.     system("pause");
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement