Advertisement
Guest User

Untitled

a guest
Nov 4th, 2019
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <set>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main ()
  9. {
  10.  
  11. string input;
  12. getline (cin, input);
  13.  
  14. map <string, int> words;
  15.  
  16. int index = input.find (" ");
  17. string result = input.substr (0, index);
  18. words.insert (pair <string, int> {result, 1});
  19.  
  20. while (index != string::npos)
  21. {
  22.     int tempIndex = input.find (" ", index + 1);
  23.     result = input.substr(index + 1, tempIndex - (index + 1));
  24.  
  25.     if (words.find (result) == words.end())
  26.     {
  27.         words.insert (pair <string, int> {result, 1});
  28.     } else
  29.     {
  30.         words[result]++;
  31.     }
  32.  
  33.     index = tempIndex;
  34. }
  35.  
  36. int queries;
  37. cin >> queries;
  38.  
  39. vector <string> results;
  40.  
  41. for (int i = 0; i < queries; ++i)
  42. {
  43.     int occurency;
  44.     int position;
  45.  
  46.     cin >> occurency >> position;
  47.  
  48.     set <string> tempWords;
  49.  
  50.  
  51.     map <string, int>::iterator it = words.begin();
  52.  
  53.     for (it; it != words.end();)
  54.     {
  55.         if (it -> second == occurency)
  56.         {
  57.             tempWords.insert(it->first);
  58.         }
  59.  
  60.         it++;
  61.  
  62.         if (it == words.end())
  63.         {
  64.             if (tempWords.empty() || position >= tempWords.size())
  65.             {
  66.                 cout << "." << endl;
  67.             } else
  68.             {
  69.                 set <string>::iterator itSet = tempWords.begin();
  70.  
  71.                 int counter = 0;
  72.                 for (itSet; itSet != tempWords.end(); ++itSet)
  73.                 {
  74.  
  75.                     if (counter == position)
  76.                     {
  77.                         results.push_back (*itSet);
  78.                         break;
  79.                     }
  80.  
  81.                     counter ++;
  82.                 }
  83.                 counter = 0;
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. for (int i = 0; i < results.size(); i++)
  90. {
  91.     cout << results[i] << endl;
  92. }
  93.  
  94.  
  95.  
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement