#include #include #include #include #include #include #include #include #include #include #include int main(int argc, char **argv) { std::multimap replacements; bool collecting_replacements = true; std::string line; while (std::getline(std::cin, line)) { if (line.empty()) { collecting_replacements = false; } else if (collecting_replacements) { auto sep = line.find(" => "); replacements.insert({line.substr(0, sep), line.substr(sep + 4)}); } else { std::set new_molecules; for (unsigned pos = 0; pos < line.size(); ++pos) { auto [it, ite] = replacements.equal_range(line.substr(pos, 1)); while (it != ite) { new_molecules.insert(line.substr(0, pos) + it->second + line.substr(pos + 1)); ++it; } if (pos < line.size() - 1) { auto [it, ite] = replacements.equal_range(line.substr(pos, 2)); while (it != ite) { new_molecules.insert(line.substr(0, pos) + it->second + line.substr(pos + 2)); ++it; } } } std::cout << line << "\n"; std::cout << "Solution: " << new_molecules.size() << "\n"; } } return 0; }