Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <unordered_map>
  6.  
  7. using namespace std;
  8.  
  9. class Object {
  10. private:
  11.     unordered_map<string, string> properties;
  12. public:
  13.     Object() {
  14.     }
  15.  
  16.     Object(vector<string> fieldNames, vector<string> fieldValues) {
  17.         for (int i = 0; i < fieldNames.size(); i++) {
  18.             properties[fieldNames[i]] = fieldValues[i];
  19.         }
  20.     }
  21.  
  22.     bool has(string propName) {
  23.         return this->properties.find(propName) != this->properties.end();
  24.     }
  25.  
  26.     string get(string propName) {
  27.         auto propIter = this->properties.find(propName);
  28.  
  29.         if (propIter != this->properties.end()) {
  30.             return propIter->second;
  31.         } else {
  32.             return "";
  33.         }
  34.     }
  35. };
  36.  
  37. vector<string> readLineOfStrings() {
  38.     vector<string> strings;
  39.  
  40.     string line;
  41.     getline(cin, line);
  42.  
  43.     istringstream lineIn(line);
  44.     string str;
  45.     while (lineIn >> str) {
  46.         strings.push_back(str);
  47.     }
  48.  
  49.     return strings;
  50. }
  51.  
  52. int main() {
  53.     vector<string> columnNames = readLineOfStrings();
  54.  
  55.     vector<string> objectValues = readLineOfStrings();
  56.  
  57.     vector<Object> objects;
  58.     while (objectValues[0] != "end") {
  59.         objects.push_back(Object(columnNames, objectValues));
  60.  
  61.         objectValues = readLineOfStrings();
  62.     }
  63.  
  64.     string searchColumn = readLineOfStrings()[0];
  65.  
  66.     unordered_map<string, int> valueOccurrences;
  67.     for (auto o : objects) {
  68.         valueOccurrences[o.get(searchColumn)]++;
  69.     }
  70.  
  71.     pair<string, int> maxOccurrencesPair("", 0);
  72.     for (auto valueOccurrencesPair : valueOccurrences) {
  73.         if (valueOccurrencesPair.second > maxOccurrencesPair.second) {
  74.             maxOccurrencesPair = valueOccurrencesPair;
  75.         }
  76.     }
  77.  
  78.     cout << maxOccurrencesPair.first << " " << maxOccurrencesPair.second << endl;
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement