Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <locale>
- #include <string>
- #include <set>
- #include <map>
- const std::map <char, std::set <std::string> > elements = {
- {'a', {"ac", "ag", "al", "am", "ar", "as", "at", "au"}},
- {'b', {"b", "ba", "be", "bh", "bi", "bk", "br"}},
- {'c', {"c", "ca", "cr", "co", "cu", "cl", "cd", "cs", "cn", "cm", "cf", "ce"}},
- {'d', {"db", "ds", "dy"}},
- {'e', {"eu", "er", "es"}},
- {'f', {"f", "fe", "fr", "fl", "fm"}},
- {'g', {"ga", "ge", "gd"}},
- {'h', {"h", "he", "hf", "hg", "hs", "ho"}},
- {'i', {"i", "in", "ir"}},
- {'j', {}},
- {'k', {"k", "kr"}},
- {'l', {"li", "la", "lv", "lu", "lr"}},
- {'m', {"mg", "mn", "mo", "mt", "mc", "md"}},
- {'n', {"n", "na", "ne", "ni", "nb", "nh", "nd", "np", "no"}},
- {'o', {"o", "os", "og"}},
- {'p', {"p", "pb", "pd", "pt", "po", "pr", "pm", "pa", "pu"}},
- {'q', {}},
- {'r', {"rb", "ru", "rh", "re", "rn", "ra", "rf", "rg"}},
- {'s', {"s", "si", "sc", "se", "sr", "sn", "sb", "sg", "sm"}},
- {'t', {"ti", "tc", "te", "ta", "tl", "ts", "tb", "tm", "th"}},
- {'u', {"u", "uue", "ubn", "ubu", "ubb", "ubt", "ubq", "ubp", "ubh"}},
- {'v', {"v"}},
- {'w', {"w"}},
- {'x', {"xe"}},
- {'y', {"y", "yb"}},
- {'z', {"zr", "zn"}}
- };
- std::vector <std::string> step(const std::string & remaining) {
- if (remaining.empty()) {
- return {};
- }
- auto it = elements.find(remaining[0]);
- if (it == elements.end()) {
- throw std::string("Char '") + remaining[0] + std::string("' not found");
- }
- std::vector <std::string> ret;
- for (const auto & elem: it->second) {
- if (remaining.find(elem) != 0) {
- continue;
- }
- ret.clear();
- ret.push_back(elem);
- try {
- auto next = step(std::string(remaining, elem.size()));
- ret.insert(ret.end(), next.begin(), next.end());
- return ret;
- } catch (std::string s) {}
- }
- throw "Word \"" + remaining + "\" cannot be represented as chemical elements";
- }
- int main(int argc, char * args[]) {
- if (argc < 2) {
- std::cout << "Using: " << args[0] << " <word>\n";
- return 0;
- }
- std::locale loc;
- std::string word(args[1]);
- for (auto & c: word) {
- c = std::tolower(c, loc);
- }
- try {
- auto elems = step(args[1]);
- for (auto & elem: elems) {
- elem[0] = std::toupper(elem[0], loc);
- std::cout << elem;
- }
- std::cout << '\n';
- } catch (std::string s) {
- std::cout << s << '\n';
- return 0;
- }
- }
Add Comment
Please, Sign In to add comment