Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <string>
- #include <vector>
- using namespace std;
- enum class type_t {
- string,
- begin,
- end,
- number
- };
- struct lex_t {
- string s;
- size_t x;
- type_t type;
- lex_t(type_t type) :
- s(),
- x(),
- type(type) {
- }
- };
- string search(queue<lex_t> &q) {
- string res;
- while (!q.empty()) {
- lex_t lex = q.front();
- q.pop();
- switch (lex.type) {
- case type_t::string:
- res += lex.s;
- break;
- case type_t::begin:
- res += search(q);
- break;
- case type_t::end:
- return res;
- break;
- case type_t::number:
- string buf = search(q);
- for (size_t i = 0; i < lex.x; ++i)
- res += buf;
- return res;
- }
- }
- return res;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- string s;
- cin >> s;
- std::vector<lex_t> lex;
- for (size_t i = 0; i < s.length(); ++i) {
- if (s[i] == '(') {
- lex.push_back(lex_t(type_t::begin));
- } else if (s[i] == ')') {
- lex.push_back(lex_t(type_t::end));
- } else if (s[i] <= '9' && s[i] >= '0') {
- size_t x = s[i] - '0';
- if (lex.empty() || lex.back().type != type_t::number) {
- lex.push_back(lex_t(type_t::number));
- lex.back().x = x;
- } else {
- lex.back().x *= 10;
- lex.back().x += x;
- }
- } else {
- string t;
- t += s[i];
- if (lex.empty() || lex.back().type != type_t::string) {
- lex.push_back(lex_t(type_t::string));
- lex.back().s = t;
- } else {
- lex.back().s += t;
- }
- }
- }
- queue<lex_t> q;
- for (lex_t const &l : lex)
- q.push(l);
- string ans;
- while (!q.empty())
- ans += search(q);
- cout << ans << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment