Posted on

Description

Submission

  • The reversed operation of append is delete, so the following program can be simplified if we implement these 2 operations as separate modules.
#include <cmath>
#include <cstdio>
#include <vector>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int Q; cin >> Q;
    stack<pair<int, string>> done;

    string s = "";

    while(Q--) {
        int operand; cin >> operand;
        if(operand == 1) {
            string a; cin >> a;
            s.append(a);
            done.push(make_pair(1, a));
        }
        if(operand == 2) {
            int k; cin >> k;
            done.push(make_pair(2, s.substr(s.size() - k, k)));
            s = s.substr(0, s.size() - k);
        }
        if(operand == 3) {
            int k; cin >> k;
            cout << s[k-1] << endl;
        }
        if(operand == 4) {
            if(done.top().first == 1) {
                s = s.substr(0, s.size() - done.top().second.size());
            }

            if(done.top().first == 2) {
                s.append(done.top().second);
            }

            done.pop();
        }
    }
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *