Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- template <class T>
- class Stack{
- private:
- T *array;
- int capacity;
- int index;
- void Expand() {
- T *newArray = new T[capacity * 4];
- for (int i = 0; i < capacity; i++) {
- newArray[i] = array[i];
- }
- delete[] array;
- capacity *= 4;
- array = newArray;
- }
- public:
- Stack() {
- array = new T[10];
- capacity = 8;
- index = 0;
- }
- void push(T n) {
- if (capacity == index) {
- Expand();
- }
- array[index++] = n;
- }
- T pop() {
- return array[--index];
- }
- T back() {
- return array[index - 1];
- }
- int size() {
- return index;
- }
- void clear() {
- delete[] array;
- capacity = 8;
- array = new T[capacity];
- index = 0;
- }
- bool empty() {
- return index == 0;
- }
- ~Stack() {
- delete[] array;
- index = 0;
- capacity = 0;
- }
- };
- std::string times_string(const std::string &str, int n) {
- std::string result;
- for (int i = 0; i < n; i++) {
- result += str;
- }
- return result;
- }
- void unpacking(const std::string &str) {
- Stack<std::string> stack;
- Stack<int> numbers;
- stack.push("");
- for (char c : str) {
- if (c == '[') {
- stack.push("");
- } else if (c == ']') {
- std::string str_times = times_string(stack.pop(), numbers.pop());
- std::string prev = stack.pop() + str_times;
- stack.push(prev);
- } else if (c >= 'a' && c <= 'z') {
- std::string prev = stack.pop() + c;
- stack.push(prev);
- } else {
- numbers.push(c - '0');
- }
- }
- std::cout << stack.pop();
- }
- int main() {
- std::string str;
- std::cin >> str;
- unpacking(str);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment