Advertisement
Derga

Untitled

Oct 29th, 2023
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <forward_list>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class MyStack {
  9. private:
  10. std::forward_list<string> my_stack;
  11.  
  12. public:
  13. void Push(string number) {
  14. my_stack.push_front(number);
  15. }
  16. string Top() {
  17. return my_stack.front();
  18. }
  19. void Pop() {
  20. my_stack.pop_front();
  21. }
  22. bool Empty() {
  23. return my_stack.empty();
  24. }
  25. };
  26.  
  27. int64_t GetNumber(const string& str, int64_t& idx) {
  28. int64_t number = 0;
  29. while (idx < str.size() && '0' <= str[idx] && str[idx] <= '9') {
  30. number *= 10;
  31. number += (str[idx] - '0');
  32. idx++;
  33. }
  34. return number;
  35. }
  36.  
  37. vector<int64_t> ParseFormulaPart(const string& formula_part) {
  38. vector<int64_t> statistics(26, 0);
  39. int64_t i = 0;
  40. while (i < formula_part.size()) {
  41. if ('A' <= formula_part[i] && formula_part[i] <= 'Z') {
  42. char element = formula_part[i];
  43. i++;
  44. int64_t count = GetNumber(formula_part, i);
  45. if (count == 0) {
  46. count = 1;
  47. }
  48. statistics[element - 'A'] += count;
  49. }
  50. }
  51. return statistics;
  52. }
  53.  
  54. int main() {
  55. string formula;
  56. cin >> formula;
  57. MyStack formula_parts;
  58. string formula_part;
  59. vector<int64_t> result(26, 0);
  60.  
  61. int64_t i = 0;
  62. while (i < formula.size()) {
  63. if (formula[i] == '(') {
  64. i++;
  65. formula_parts.Push(formula_part);
  66. formula_part.clear();
  67. continue;
  68. }
  69.  
  70. if (formula[i] == ')') {
  71. i++;
  72. int64_t multiplier = GetNumber(formula, i);
  73. if (multiplier == 0) {
  74. multiplier = 1;
  75. }
  76.  
  77. vector<int64_t> statistics = ParseFormulaPart(formula_part);
  78.  
  79. for (int64_t i = 0; i < 26; ++i) {
  80. result[i] += statistics[i];
  81. result[i] *= multiplier;
  82. }
  83.  
  84. if (!formula_parts.Empty()) {
  85. formula_part = formula_parts.Top();
  86. formula_parts.Pop();
  87. }
  88. continue;
  89. }
  90.  
  91. formula_part += formula[i];
  92. i++;
  93. }
  94.  
  95. int64_t multiplier = GetNumber(formula, i);
  96. if (multiplier == 0) {
  97. multiplier = 1;
  98. }
  99. vector<int64_t> v = ParseFormulaPart(formula_part);
  100.  
  101. for (int64_t i = 0; i < 26; ++i) {
  102. result[i] += v[i];
  103. result[i] *= multiplier;
  104. }
  105.  
  106. for (int64_t i = 0; i < result.size(); ++i) {
  107. if (result[i]) {
  108. cout << (char)('A' + i);
  109. if (result[i] > 1) {
  110. cout << result[i];
  111. }
  112. }
  113. }
  114. }
  115.  
  116. /*
  117. test1
  118. H4ZO2
  119. H4O2Z
  120.  
  121. test2
  122. N(OY3)2
  123. NO2Y6
  124.  
  125. test3
  126. Z3(O(N2O3)2)3
  127. N12O21Z3
  128.  
  129. test4
  130. ZZZ
  131. Z3
  132.  
  133. test5
  134. (ZZZ)
  135. Z3
  136.  
  137. test6
  138. (((ZZZ)Z)Z)Z
  139.  
  140. test7
  141. A(BBB)A
  142.  
  143. test8
  144. (A(BBB)A)5
  145.  
  146. test9
  147. A(B(C(DE2)3F)5G)7H
  148. AB7C35D105E210F35G7H
  149.  
  150. test10
  151. A(B(C(DE2)3F)5G)7H8
  152. AB7C35D105E210F35G7H8
  153. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement