Advertisement
Guest User

Untitled

a guest
Nov 5th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <sstream>
  5. #include <string>
  6. #include <iterator>
  7.  
  8. class message
  9. {
  10. public:
  11.  
  12. std::string input;
  13.  
  14. void readInput (std::string input)
  15. {
  16. while (std::cin >> input && input != ".") {
  17. extractWords(input, extractedWords, message);
  18. }
  19.  
  20. std::cin >> numberOfQueries;
  21.  
  22. checkExtractedWords (numberOfQueries,
  23. occurenceCount,
  24. occurenceIndex,
  25. message,
  26. output,
  27. itr);
  28. }
  29.  
  30. private:
  31.  
  32. bool isEmpty = true;
  33. int occurenceCount = 0;
  34. int occurenceIndex = 0;
  35. int numberOfQueries = 0;
  36. std::string extractedWords;
  37. std::vector<std::string> output;
  38. std::map<std::string,int> message;
  39. std::map<std::string, int>::iterator itr;
  40.  
  41.  
  42. void extractWords (std::string input,
  43. std::string extractedWords,
  44. std::map<std::string,int> & message)
  45. {
  46. std::istringstream iss(input);
  47.  
  48. while(iss >> extractedWords) {
  49. message[extractedWords]++;
  50. }
  51. }
  52.  
  53. void checkExtractedWords (int numberOfQueries,
  54. int occurenceCount,
  55. int occurenceIndex,
  56. std::map<std::string,int> & message,
  57. std::vector<std::string> & output,
  58. std::map<std::string,int>::iterator & itr)
  59. {
  60. for (int i = 0; i < numberOfQueries; ++i) {
  61. std::cin >> occurenceCount;
  62. std::cin >> occurenceIndex;
  63.  
  64. output.clear();
  65. output.shrink_to_fit();
  66. bool isEmpty = true;
  67.  
  68. for(itr = message.begin(); itr != message.end(); ++itr) {
  69. if (itr -> second == occurenceCount) {
  70. output.emplace_back(itr -> first);
  71. isEmpty = false;
  72. }
  73. }
  74.  
  75. printOutput(output, occurenceIndex, isEmpty);
  76.  
  77. }
  78. }
  79.  
  80. void printOutput (const std::vector<std::string> & output,
  81. int occurenceIndex,
  82. bool isEmpty)
  83. {
  84. if (!isEmpty && occurenceIndex <= output.size()) {
  85. std::cout << output[occurenceIndex] << std::endl;
  86. } else {
  87. std::cout << '.' << std::endl;
  88. }
  89. }
  90. };
  91.  
  92. int main ()
  93. {
  94. std::cin.sync_with_stdio(false);
  95. std::cout.sync_with_stdio(false);
  96.  
  97. class message msg;
  98.  
  99. msg.readInput(msg.input);
  100.  
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement