boyanandreev

Entities

May 5th, 2021
497
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <algorithm>
  2. #include <array>
  3. #include <iostream>
  4. #include <map>
  5. #include <string>
  6. #include <sstream>
  7. #include <unordered_map>
  8. #include <vector>
  9.  
  10. std::vector<std::string> readFirstLine();
  11. std::vector<std::string> readLines(std::string& line);
  12. std::unordered_map<std::string, std::vector<std::string>> generateMap(std::vector<std::string>& header);
  13. std::unordered_map<std::string, int> mostFrequent(std::vector<std::string>& column);
  14. void printResult(std::unordered_map<std::string, int>& countFrequent);
  15. int main()
  16. {  
  17.     std::string line;
  18.     std::vector<std::string> header{ readFirstLine() };
  19.    
  20.     std::unordered_map<std::string, std::vector<std::string>> table{ generateMap(header) };
  21.    
  22.    
  23.     std::string column;
  24.     std::cin >> column;
  25.  
  26.     std::unordered_map<std::string, int> countFrequent = mostFrequent(table[column]);
  27.     printResult(countFrequent);
  28.     return 0;
  29. }
  30.  
  31. void printResult(std::unordered_map<std::string, int>& countFrequent)
  32. {
  33.     int biggest{ INT32_MIN };
  34.     std::pair<std::string, int> result;
  35.     std::unordered_map<std::string, int>::iterator iter;
  36.     for (iter = countFrequent.begin(); iter != countFrequent.end(); ++iter)
  37.     {
  38.         if (iter->second > biggest)
  39.         {
  40.             biggest = iter->second;
  41.             result.first = iter->first;
  42.             result.second = biggest;
  43.         }
  44.     }  
  45.     std::cout << result.first << " " << result.second << std::endl;
  46.  
  47. }
  48.  
  49. std::unordered_map<std::string, int> mostFrequent(std::vector<std::string>& column)
  50. {
  51.     std::unordered_map<std::string, int> countFrequent;
  52.     for (std::string& element : column)
  53.     {
  54.         if (countFrequent.find(element) != countFrequent.end())
  55.         {
  56.             countFrequent[element] = 1;
  57.         }
  58.         countFrequent[element]++;
  59.     }
  60.  
  61.     return countFrequent;
  62. }
  63.  
  64. std::vector<std::string> readLines(std::string& line)
  65. {
  66.    
  67.     std::string word;
  68.     std::istringstream iStr(line);
  69.     std::vector<std::string> properties;    
  70.     while (iStr >> word)
  71.     {
  72.         properties.push_back(word);
  73.     }
  74.     return properties;
  75. }
  76.  
  77.  
  78. std::unordered_map<std::string, std::vector<std::string>> generateMap(std::vector<std::string>& header)                                                                      
  79. {
  80.     size_t headerSize{ header.size() };
  81.     std::vector<std::string> properties;
  82.     std::string line;
  83.     std::getline(std::cin, line);
  84.     std::unordered_map<std::string, std::vector<std::string>> table;
  85.     while (line != "end") {
  86.         properties = readLines(line);    
  87.         for (size_t i = 0; i < headerSize; ++i)
  88.         {
  89.             table[header[i]].push_back(properties[i]);
  90.         }
  91.         std::getline(std::cin, line);
  92.     }
  93.     return table;
  94. }
  95.  
  96. std::vector<std::string> readFirstLine()
  97. {    
  98.     std::string word;
  99.     std::string line;
  100.     std::vector<std::string> vec;
  101.     std::getline(std::cin, line);    
  102.     std::istringstream iStr(line);
  103.     while (iStr >> word)
  104.     {
  105.         vec.push_back(word);
  106.     }
  107.     return vec;
  108. }
  109.  
  110.  
  111.  
  112.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×