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;
}