kolioi

Transmission (JA3-Task-4-Message) C++

Jan 14th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <map>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. void parse(const string& str, vector<vector<string>>& vResult)
  11. {
  12.     map<string, int> mList;
  13.     stringstream ss(str);
  14.     string token;
  15.     char delim = ' ';
  16.     while (getline(ss, token, delim) && token[0] != '.')
  17.         mList[token]++;
  18.  
  19.     using pair_type = decltype(mList)::value_type;
  20.     int nMaxCount = max_element(mList.begin(), mList.end(),
  21.         [](const pair_type& p1, const pair_type& p2)
  22.         { return p1.second < p2.second; })->second;
  23.     vResult.resize(nMaxCount + 1);
  24.     for (const pair_type& item : mList)
  25.         vResult[item.second].push_back(item.first);
  26. }
  27.  
  28. int main()
  29. {
  30.     cin.sync_with_stdio(false);
  31.     cout.sync_with_stdio(false);
  32.    
  33.     string szInput;
  34.     getline(cin, szInput, '\n');
  35.  
  36.     vector<vector<string>> vResult;
  37.     parse(szInput, vResult);
  38.  
  39.     size_t nQueries;
  40.     cin >> nQueries;
  41.    
  42.     for (size_t i = 0; i < nQueries; ++i)
  43.     {
  44.         size_t nIndex, nCount;
  45.         cin >> nIndex >> nCount;
  46.         if (nIndex < vResult.size() && nCount < vResult[nIndex].size())
  47.             cout << vResult[nIndex][nCount] << '\n';
  48.         else
  49.             cout << '.' << '\n';
  50.     }
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment