#include #include #include #include #include #include using namespace std::literals; int main() { std::vector> lines { }; std::map frequency { }; // read each line std::string tmp; while (std::getline(std::cin, tmp)) { // read each word std::istringstream iss { tmp }; std::vector line { }; std::string word; while (iss >> word) { line.push_back(word); ++frequency[word]; } lines.push_back(std::move(line)); } // find the 3 most common words std::vector> common(3); std::partial_sort_copy(frequency.begin(), frequency.end(), common.begin(), common.end(), [](auto&& lhs, auto&& rhs) { return lhs.second > rhs.second; }); for (std::vector& line : lines) { for (auto&& [word, count] : common) { std::replace(line.begin(), line.end(), word, "REPLACED"s); } } std::cout << "== Replaced text: " << std::endl; for (std::vector const& line : lines) { for (std::string const& word : line) { std::cout << word << " "; } std::cout << std::endl; } }