#include #include #include #include #include using namespace std; // List of all "synonyms" vector words { "back", "ball", "band", "bare", "bass", "beads", "bean", "belt", "berry", "bids", "bite", "bold", "bone", "bones", "book", "cafe", "caps", "card", "cash", "cell", "cent", "chan", "code", "cool", "cork", "cost", "cure", "dale", "days", "dead", "dish", "dive", "dome", "ears", "east", "eight", "face", "fail", "fear", "feed", "file", "files", "find", "fired", "food", "form", "full", "gage", "grad", "hang", "hart", "hate", "heat", "hill", "hole", "jake", "jose", "king", "lane", "leading", "lets", "life", "lines", "list", "logs", "long", "lover", "mark", "math", "mens", "mice", "mild", "mint", "miss", "modes", "nest", "nick", "nike", "paid", "pine", "poll", "pope", "port", "real", "ride", "ross", "same", "sans", "seas", "seeds", "seek", "shake", "shot", "slow", "span", "takes", "then", "tire", "tons", "track", "wales", "walt", "warm", "wave", "wise", "word", "wound" }; bool is_synonyms(string const& word, string const& other) { // 1. If word and other don't have the same size, return false if (word.size() == other.size()) { // 2. Find the first position where word and other have // different characters auto pos{mismatch(begin(word), end(word), begin(other))}; if (pos.first != end(word)) { // 3. If such a position is found, search for the next // position where they differ ++pos.first; ++pos.second; pos = mismatch(pos.first, end(word), pos.second); // 4. If no other difference is found, return true return pos.first == end(word); } } // 5. return false in every other case return false; } string get_synonym(string const& word) { // 1. Try to find a synonym to word in the words vector; use // the is_synonym function auto it {find_if(begin(words), end(words), [&word](string const& other) { return is_synonyms(other, word); })}; // 2. If a synonym was found, return it // 3. If no synonym was found, return word if (it == end(words)) return word; return *it; } int main() { // 1. Read all words from cin to a container named text vector text { istream_iterator{cin}, istream_iterator{} }; // 2. Replace each word in text with a synonym; use get_synonym transform(begin(text), end(text), begin(text), get_synonym); // 3. Print the new text copy(begin(text), end(text), ostream_iterator{cout, " "}); }