Guest User

Untitled

a guest
Jul 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <locale>
  4. #include <string>
  5. #include <set>
  6. #include <map>
  7.  
  8. const std::map <char, std::set <std::string> > elements = {
  9. {'a', {"ac", "ag", "al", "am", "ar", "as", "at", "au"}},
  10. {'b', {"b", "ba", "be", "bh", "bi", "bk", "br"}},
  11. {'c', {"c", "ca", "cr", "co", "cu", "cl", "cd", "cs", "cn", "cm", "cf", "ce"}},
  12. {'d', {"db", "ds", "dy"}},
  13. {'e', {"eu", "er", "es"}},
  14. {'f', {"f", "fe", "fr", "fl", "fm"}},
  15. {'g', {"ga", "ge", "gd"}},
  16. {'h', {"h", "he", "hf", "hg", "hs", "ho"}},
  17. {'i', {"i", "in", "ir"}},
  18. {'j', {}},
  19. {'k', {"k", "kr"}},
  20. {'l', {"li", "la", "lv", "lu", "lr"}},
  21. {'m', {"mg", "mn", "mo", "mt", "mc", "md"}},
  22. {'n', {"n", "na", "ne", "ni", "nb", "nh", "nd", "np", "no"}},
  23. {'o', {"o", "os", "og"}},
  24. {'p', {"p", "pb", "pd", "pt", "po", "pr", "pm", "pa", "pu"}},
  25. {'q', {}},
  26. {'r', {"rb", "ru", "rh", "re", "rn", "ra", "rf", "rg"}},
  27. {'s', {"s", "si", "sc", "se", "sr", "sn", "sb", "sg", "sm"}},
  28. {'t', {"ti", "tc", "te", "ta", "tl", "ts", "tb", "tm", "th"}},
  29. {'u', {"u", "uue", "ubn", "ubu", "ubb", "ubt", "ubq", "ubp", "ubh"}},
  30. {'v', {"v"}},
  31. {'w', {"w"}},
  32. {'x', {"xe"}},
  33. {'y', {"y", "yb"}},
  34. {'z', {"zr", "zn"}}
  35. };
  36.  
  37. std::vector <std::string> step(const std::string & remaining) {
  38. if (remaining.empty()) {
  39. return {};
  40. }
  41.  
  42. auto it = elements.find(remaining[0]);
  43.  
  44. if (it == elements.end()) {
  45. throw std::string("Char '") + remaining[0] + std::string("' not found");
  46. }
  47.  
  48. std::vector <std::string> ret;
  49. for (const auto & elem: it->second) {
  50. if (remaining.find(elem) != 0) {
  51. continue;
  52. }
  53.  
  54. ret.clear();
  55. ret.push_back(elem);
  56.  
  57. try {
  58. auto next = step(std::string(remaining, elem.size()));
  59. ret.insert(ret.end(), next.begin(), next.end());
  60.  
  61. return ret;
  62. } catch (std::string s) {}
  63. }
  64.  
  65. throw "Word \"" + remaining + "\" cannot be represented as chemical elements";
  66. }
  67.  
  68. int main(int argc, char * args[]) {
  69. if (argc < 2) {
  70. std::cout << "Using: " << args[0] << " <word>\n";
  71. return 0;
  72. }
  73.  
  74. std::locale loc;
  75.  
  76. std::string word(args[1]);
  77. for (auto & c: word) {
  78. c = std::tolower(c, loc);
  79. }
  80.  
  81. try {
  82. auto elems = step(args[1]);
  83.  
  84. for (auto & elem: elems) {
  85. elem[0] = std::toupper(elem[0], loc);
  86.  
  87. std::cout << elem;
  88. }
  89.  
  90. std::cout << '\n';
  91. } catch (std::string s) {
  92. std::cout << s << '\n';
  93.  
  94. return 0;
  95. }
  96. }
Add Comment
Please, Sign In to add comment