Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- struct _ { ios_base::Init i; _() { cin.sync_with_stdio(0); cin.tie(0); } } _;
- class Node
- {
- public:
- bool isleaf;
- int times;
- list<Node*> children;
- string content;
- Node(int n)
- {
- isleaf = false;
- times = n;
- }
- Node(string s)
- {
- isleaf = true;
- content = s;
- }
- void push(Node *next)
- {
- children.push_back(next);
- }
- };
- string s;
- int n;
- int create(Node *parent, int pos)
- {
- int times = 0;
- while (s[pos] >= '0' && s[pos] <= '9')
- times = times * 10 + s[pos++] - '0';
- if (times == 0)
- times = 1;
- Node *current = new Node(times);
- parent->push(current);
- string curs;
- while (pos < n && s[pos] != ')')
- if (s[pos] != '(')
- curs += s[pos++];
- else
- {
- if (curs.length() > 0)
- current->push(new Node(curs));
- curs = "";
- pos = create(current, pos + 1);
- }
- if (curs.length() > 0)
- current->push(new Node(curs));
- return pos + 1;
- }
- void dfs(Node *v)
- {
- if (v->isleaf)
- cout << v->content;
- else
- for (int i = 0; i < v->times; ++i)
- for (auto node : v->children)
- dfs(node);
- }
- Node *head;
- int main()
- {
- cin >> s;
- n = s.length();
- head = new Node(1);
- create(head, 0);
- dfs(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment