Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : String Formation

#include <bits/stdc++.h>
using namespace std;

void solve() {
    deque<char> dq;

    bool flipped = false;
    string str;
    cin >> str;

    for (auto ch : str) {
        dq.push_back(ch);
    }

    int q;
    cin >> q;
    for (int zz = 0; zz < q; zz++) {
        int type;
        cin >> type;
        if (type == 1) {
            flipped = !flipped;
        } else {
            int F;
            cin >> F;
            char ch;
            cin >> ch;

            // Beginnig of string.
            if (F == 1) {
                if (!flipped) {
                    dq.push_front(ch);
                } else {
                    dq.push_back(ch);
                }
            } else {
                if (!flipped) {
                    dq.push_back(ch);
                } else {
                    dq.push_front(ch);
                }
            }
        }
    }

    string res = "";
    while (!dq.empty()) {
        res += dq.front();
        dq.pop_front();
    }
    if (flipped) {
        reverse(res.begin(), res.end());
    }
    cout << res << "\n";
}

int main() {
    solve();
    return 0;
}