Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <sstream>
- #include <string>
- #include <iterator>
- class message
- {
- public:
- std::string input;
- void readInput (std::string input)
- {
- while (std::cin >> input && input != ".") {
- extractWords(input, extractedWords, message);
- }
- std::cin >> numberOfQueries;
- checkExtractedWords (numberOfQueries,
- occurenceCount,
- occurenceIndex,
- message,
- output,
- itr);
- }
- private:
- bool isEmpty = true;
- int occurenceCount = 0;
- int occurenceIndex = 0;
- int numberOfQueries = 0;
- std::string extractedWords;
- std::vector<std::string> output;
- std::map<std::string,int> message;
- std::map<std::string, int>::iterator itr;
- void extractWords (std::string input,
- std::string extractedWords,
- std::map<std::string,int> & message)
- {
- std::istringstream iss(input);
- while(iss >> extractedWords) {
- message[extractedWords]++;
- }
- }
- void checkExtractedWords (int numberOfQueries,
- int occurenceCount,
- int occurenceIndex,
- std::map<std::string,int> & message,
- std::vector<std::string> & output,
- std::map<std::string,int>::iterator & itr)
- {
- for (int i = 0; i < numberOfQueries; ++i) {
- std::cin >> occurenceCount;
- std::cin >> occurenceIndex;
- output.clear();
- output.shrink_to_fit();
- bool isEmpty = true;
- for(itr = message.begin(); itr != message.end(); ++itr) {
- if (itr -> second == occurenceCount) {
- output.emplace_back(itr -> first);
- isEmpty = false;
- }
- }
- printOutput(output, occurenceIndex, isEmpty);
- }
- }
- void printOutput (const std::vector<std::string> & output,
- int occurenceIndex,
- bool isEmpty)
- {
- if (!isEmpty && occurenceIndex <= output.size()) {
- std::cout << output[occurenceIndex] << std::endl;
- } else {
- std::cout << '.' << std::endl;
- }
- }
- };
- int main ()
- {
- std::cin.sync_with_stdio(false);
- std::cout.sync_with_stdio(false);
- class message msg;
- msg.readInput(msg.input);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement