Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Accepted

Solution code:
s = input()
q = int(input())
query_list = [list(input().split()) for _ in range(q)]
reverse_flag = False
concat = ''
initial = 0
for query in query_list:
    if query[0] == '1':
        reverse_flag = not reverse_flag
    elif query[0] == '2':
        if (query[1] == '1' and not reverse_flag) or (query[1] == '2' and reverse_flag):
            concat = query[2] + concat
            initial += 1
        elif (query[1] == '2' and not reverse_flag) or (query[1] == '1' and reverse_flag):
            concat = concat + query[2]
ans = ''
if reverse_flag:
    s = s[::-1]
    concat = concat[::-1]
    ans = concat[:-initial]+s+concat[-initial:]
else:
    ans = concat[:initial]+s+concat[initial:]
print(ans)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())

left = ''
right = S
for q in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == '1':
        #反転
        tmp = left
        left = right
        right = tmp
    else:   #Query[0]==2
        if Query[1] == '1':
            #先頭追加
            left += Query[2]
        else:   #Query[1] ==2
            #末尾追加
            right += Query[2]

ans = left[::-1] + right
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())

dir = 1
for q in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == '1':
        #反転
        dir *= -1
    else:   #Query[0]==2
        if Query[1] == '1':
            #先頭追加
            if dir > 0:
                #Sの先頭追加
                S.appendleft(Query[2])
            else:   #dir < 0
                #Sの末尾追加
                S.append(Query[2])
        else:   #Query[1] ==2
            #末尾追加
            if dir > 0:
                #Sの末尾追加
                S.append(Query[2])
            else:   #dir < 0
                #Sの先頭追加
                S.appendleft(Query[2])

if dir < 0:
    S.reverse()
else:   #dir > 0
    pass

print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
s=input()
q=int(input())
t=[input() for i in range(q)]
l=deque(list(s))
m=0
for i in range(q):
  if int(t[i][0])==1:
    m+=1
  else:
    if m%2==0:
      if int(t[i][2])==1:
        l.appendleft(t[i][4]) 
      else:
        l.append(t[i][4])
    else:
      if int(t[i][2])==2:
        l.appendleft(t[i][4]) 
      else:
        l.append(t[i][4])
if m%2==1:
  l.reverse()
print(''.join(l))

Submission status: Accepted

Solution code:
from collections import deque
s=input()
n = int(input())

f=False
q= deque(s)

for _ in range(n):
    i = input()

    if i[0] == '1': f=False if f else True
    else:
        _,a,b =i.split()
        
        if f :
            if a == '1':q.append(b)
            else:q.appendleft(b)
        else:
            if a == '1':q.appendleft(b)
            else:q.append(b)

if f: q.reverse()

for i in q:print(i,end='')

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
que = [0]*Q
count = 0
flag = True
flist = []
llist = []

for i in range(Q):
    que[i] = list(map(str, input().split()))
for i in que:
    if len(i) == 1:
        count += 1
        flag = not flag
    else:
        if i[1] == '1' and flag:
            flist.append(i[2])
        elif i[1] == '2' and not flag:
            flist.append(i[2])
        elif i[1] == '1' and not flag:
            llist.append(i[2])
        else:
            llist.append(i[2])

F = ''.join(flist)
L = ''.join(llist)

if count % 2 == 1:
    S = S[::-1]
    L = L[::-1]
    print(L + S + F)
else:
    F = F[::-1]
    print(F + S + L)

Submission status: Accepted

Solution code:
s = input()
n = int(input())
head = ''
tail = s
for _ in range(n):
    q = input()
    if q[0] == '1':
        head, tail = tail, head
    else:
        _, f, c = q.split()
        if f == '1':
            head += c
        else:
            tail += c
print(head[::-1] + tail)

Submission status: Accepted

Solution code:
def string():
    s = input()
    q = int(input())
    l = ''
    r = ''
    re = False
    for _ in range(q):
        t = input().split()
        if t[0] == '1':
            re = not re
        else:
            if re:
                if t[1] == '1':
                    r = r + t[2]
                else:
                    l = t[2] + l
            else:
                if t[1] == '1':
                    l = t[2] + l
                else:
                    r = r + t[2]
    s = l + s + r
    if re:
        s = s[::-1]
    print(s)
string()

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
flip = False
head = ''
tail = ''
for _ in range(Q):
    q = input()
    if q == '1':
        flip = not flip
        head, tail = tail, head
    else:
        _, f, c = q.split()
        if f == '1':
            head += c
        else:
            tail += c
if flip:
    S = S[::-1]
print(head[::-1] + S + tail)

Submission status: Accepted

Solution code:
def main():
    s  = input()
    q  = int(input())
    inv = 1
    sfb = [0,"",""]
    for i in range(q):
        ls = list(input().split())
        if ls[0] == "1":
            inv *=-1
        else:
            f,c = ls[1:]
            if f == "1":
                sfb[inv] +=c
            else:
                sfb[-inv] +=c

    ans = sfb[1][::-1] + s + sfb[-1]
    print(ans[::inv])
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    S = input()
    Q = int(input())
    L = ""
    R = ""
    is_turned = False

    for _ in range(Q):
        Query = input().split()
        if Query[0] == "1":
            is_turned = not is_turned
        else:
            if is_turned ^ (Query[1] == "1"):
                # L.insert(0, Query[2])
                L = Query[2] + L
            else:
                # R.append(Query[2])
                R = R + Query[2]

    if (is_turned):
        print("".join(L + S + R)[::-1])
    else:
        print("".join(L + S + R))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

# 入力はstringで入る．
# intにしたければ int(input())
# floatで受け取ると 1.0 になる．

# 入力
S = input()
Q = int(input())

Change = " "
counter = 0

for i in range(Q):
    # i+1 回目のinput
    proc = input().split()

    if proc[0] == "1":
        counter += 1
    else:
        # 先頭に文字列を足す場合
        if (counter%2 == 0 and proc[1] == "1") or (counter%2 == 1 and proc[1] == "2"):
            Change = proc[2] + Change
        else:
            Change = Change + proc[2]

if counter%2 == 0:
    print(Change.replace(" ",S))
else:
    print(Change.replace(" ",S)[::-1])

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
L = ''
R = ''
rev = False
for _ in range(Q):
    Query = input().split()
    if Query[0] == '1':
        rev = not rev
    else:
        if rev:
            if Query[1] == '1':
                R = R + Query[2]
            else:
                L = Query[2] + L
        else:
            if Query[1] == '1':
                L = Query[2] + L
            else:
                R = R + Query[2]
S = L + S + R
if rev:
    S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read

#h,w,k = [int(i) for i in read().split()]


s = input()
u = ""
t = ""
q = int(input())
num = 0

for _ in range(q):
    a = readline().split()
    if len(a)==1:
        u,t = t,u
        num ^= 1
    else:
        f = int(a[1])
        c = a[2]
        if f==1:
            t += c
        else:
            u += c

if num:
    print(t[::-1]+s[::-1]+u)
else:
    print(t[::-1]+s+u)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input().rstrip())
q = int(input())

reverse = False
for i in range(q):
    query = input()
    if int(query[0]) == 1:
        reverse = not reverse
    else:
        _, f, c = query.split()
        if int(f) == 1:
            if reverse:
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if reverse:
                s.appendleft(c)
            else:
                s.append(c)
if reverse:
    s.reverse()

print(''.join(s))

Submission status: Accepted

Solution code:
s = input()
n = int(input())

reverse  = False
forward  = ""
backward = ""

for _ in range(n):
  query = input().split()
  if query == ["1"]:
    reverse = not reverse
  else:
    if query[1] == "1":
      if reverse:
        backward += query[2][::-1]
      else:
        forward = query[2] + forward
    else:
      if reverse:
        forward = query[2][::-1] + forward
      else:
        backward += query[2]

if reverse:
  print(backward[::-1] + s[::-1] + forward[::-1])
else:
  print(forward + s + backward)

Submission status: Accepted

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  from collections import deque
  s = list(input())
  s = s[:-1]
  s = deque(s)
  q = int(input())
  c = [[i for i in input().split()] for j in range(q)]
  cnt = 1
  for i in c:
    if i[0] == '1':
      cnt *= -1
    else:
      if (i[1] == '1' and cnt == 1) or (i[1] == '2' and cnt == -1):
        s.appendleft(i[2])
      else:
        s.append(i[2])
  if cnt == 1:
    print(''.join(s))
  else:
    print(''.join(s)[::-1])
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    s = input()
    n = int(input())
    pre = ''
    suf = ''
    r = 1
    for _ in range(n):
        q = input().split()
        if len(q) == 1:
            r *= -1
            continue
        if r * int(q[1]) in (1, -2):
            pre = q[2] + pre
        else:
            suf += q[2]
    ans = pre + s + suf
    if r == -1:
        ans = ans[::-1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
q = int(input())

reverse_flag = 1

for _ in range(q):
    query = input().split()
    if query[0] == "1":
        reverse_flag *= -1
    else:
        if (query[1] == "1" and reverse_flag == 1) or (query[1] == "2" and reverse_flag == -1):
            s.appendleft(query[2])
        else:
            s.append(query[2])
if reverse_flag == 1:
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Submission status: Accepted

Solution code:
def RevFlag(flag):
    if flag:
        return False
    else:
        return True

def main():
    from collections import deque
    S = input()
    d = deque()
    for s in S:
        d.append(s)
    N = int(input())
    rev = False

    for i in range(N):
        Q = [i for i in input().split()]
        if len(Q) == 1:
            rev = RevFlag(rev)
        else:
            F = int(Q[1])
            C = Q[2]
            if F == 1:
                if rev:
                    d.append(C)  # 末尾
                else:
                    d.appendleft(C)  # 先頭
            else:
                if rev:
                    d.appendleft(C)  # 先頭
                else:
                    d.append(C)  # 末尾
    if rev:
        d = ''.join(list(reversed(d)))
    else:
        d = ''.join(list(d))
    print(d)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#TLE
from collections import deque
s = deque(input())
q = int(input())
num = 0
for _ in range(q):
    t = input()
    if t == '1':
        if num == 0:
            num = 1
        else:
            num = 0
    else:
        t, f, c = t.split()
        if num == 0:
            if f == '1':
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == '2':
                s.appendleft(c)
            else:
                s.append(c)
s = ''.join(s)
if num == 1:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
s = input()
q = int(input())

cnt = 0
left = ''
right = ''
for i in range(q):
    q_very = input()

    #print(q_very)
    if q_very[0] == '1':
        #print('t is 1')
        cnt += 1
    else:
        instruction = q_very.split()
        # 反転していない
        if cnt % 2 == 0:
            if instruction[1] == '1':
                left = instruction[2] + left
            else:
                right = right + instruction[2]
        else: # 反転している
            if instruction[1] == '1':
                right = right + instruction[2]
            else:
                left = instruction[2] + left

s = left + s + right

if cnt % 2 != 0:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop
from math import floor,ceil
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline

M = mod = 998244353
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)

def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n').split(' ')]
def li3():return [int(i) for i in input().rstrip('\n')]


s = st()
q = val()
l = []

cou = 0

d = deque()
for i in s:d.append(i)


for j in range(q):
    l = li2()
    if len(l) == 1:cou = not cou
    else:
        if cou:
            a = 3 - int(l[1])
        else:a = int(l[1])
        if a == 2:
            d.append(l[-1])
        else:d.appendleft(l[-1])

s = ''.join(e for e in d)
if cou:s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
def resolve():
    S = input()
    Q = int(input())

    from collections import deque
    strings = deque([S])
    switch = 0
    for _ in range(Q):
        line = input()
        if line.startswith('1'):
            switch ^= 1
            continue
        _, n, char = line.split()
        if n == '1':
            if switch == 0:
                strings.appendleft(char)
            else:
                strings.append(char)
        else:
            if switch == 0:
                strings.append(char)
            else:
                strings.appendleft(char)

    if switch:
        print(''.join(strings)[::-1])
    else:
        print(''.join(strings))
resolve()

Submission status: Accepted

Solution code:
S = input()
numQ = int(input())
lead = 1
front = ""
rear = ""
for i in range(numQ):
  Q = input()
  if Q[0] == "1":
    lead *= -1
  else:
    if Q[2] == "1":
      if lead == 1:
        front = Q[4] + front
      else:
        rear = rear + Q[4]
    else:
      if lead == 1:
        rear = rear + Q[4]
      else:
        front = Q[4] + front
S = front + S + rear
if lead == -1:
  S = S[::-1]

print(S)

Submission status: Accepted

Solution code:
S=input()
QN=int(input())
front=''
rear=''
rev=False

for i in range(QN):
    op=input()
    if op[0]=='1':
        rev=not(rev)
    else:
        if op[2]=='1':
            if rev:
                rear+=op[4]
            else:
                front+=op[4]
        else:
            if rev:
                front+=op[4]
            else:
                rear+=op[4]

if rev:
    print(rear[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+rear)

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
right, left = deque(), deque()
right_inv, left_inv, S_inv = False, False, False

for q in range(Q):
  T = input().split(" ")
  if len(T) == 1:
    # reverse
    S_inv = not S_inv
    right_inv = not right_inv
    left_inv = not left_inv
  else:
    # append
    F, C = int(T[1]), T[2]
    if (F == 1) ^ S_inv:
      left.appendleft(C)
    else:
      right.append(C)

if S_inv:
  print(''.join(list(right)[::-1]), S[::-1], ''.join(list(left)[::-1]), sep='')
else:
  print(''.join(list(left)), S, ''.join(list(right)), sep='')

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
right, left = deque(), deque()
right_inv, left_inv, S_inv = False, False, False

for q in range(Q):
  T = input().split(" ")
  if len(T) == 1:
    # reverse
    S_inv = not S_inv
    right_inv = not right_inv
    left_inv = not left_inv
  else:
    # append
    F, C = int(T[1]), T[2]
    if F == 1:
      if S_inv:
        # add right
        right.append(C)
      else:
        # add left
        left.appendleft(C)
    else:
      if S_inv:
        # add left
        left.appendleft(C)
      else:
        # add right
        right.append(C)

if S_inv:
  print(''.join(list(right)[::-1]), S[::-1], ''.join(list(left)[::-1]), sep='')
else:
  print(''.join(list(left)), S, ''.join(list(right)), sep='')

Submission status: Accepted

Solution code:
from collections import deque

s = input()
q = int(input())

ans = deque(s)
rev = False

for _ in range(q):
    t = list(input().split())
    if t[0] == '1':
        rev = not rev
    elif t[0] == '2':
        if rev == False:
            if t[1] == '1':
                ans.appendleft(t[2])
            else:
                ans.append(t[2])
        else: # rev = Trueのとき
            if t[1] == '1':
                ans.append(t[2])
            else:
                ans.appendleft(t[2])

result = ''.join(ans)
if rev == True:
    result = result[::-1]

print(result)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input().split())
q = int(input())
rev = False
for _ in range(q):
    qu = input()
    if qu == "1":
        rev = not rev
    else:
        _, f, c = qu.split()
        if (f != "1") == rev:
            s.appendleft(c)
        else:
            s.append(c)

s = "".join(s)
if rev:
    s = reversed(s)
print("".join(s))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
r, st1, st2 = 0, "", ""
for _ in range(q):
  query = input()
  if query[0] == "1":
    r += 1
  else:
    t, f, c = query.split()
    if (r + int(f)) % 2:
      st1 += c
    else:
      st2 += c

string = "".join(reversed(list(st1))) + s + st2
if r % 2 == 0:
  print(string)
else:
  print("".join(reversed(list(string))))

Submission status: Accepted

Solution code:
from collections import deque

s=input()
n=int(input())
q=deque()
q.append(s)
f=1
for i in range(n):
    t=list(input().split())
    if int(t[0])==1:
        f*=-1
    else:
        if f==1:
            if t[1]=='1' :
                q.appendleft(t[2])
            else:
                q.append(t[2])
        else:
            if t[1]=='2':
                q.appendleft(t[2])
            else:    
                q.append(t[2])
ans=''.join(q)
if f==-1:
    ans=ans[::-1]


print(ans)

Submission status: Accepted

Solution code:
def solve(string):
    s, q, *qs = string.split("\n")
    pre = []
    post = []
    preapp = lambda x:pre.append(x)
    postapp = lambda x:post.append(x)
    r = False
    for _q in qs[::-1]:
        if _q == "1":
            r = not r
            continue
        _, f, c = _q.split()
        if r and f == "2" or not r and f == "1":
            preapp(c)
        else:
            postapp(c)
    pre, post = "".join(pre), "".join(post[::-1])
    return pre + s[::-1] + post if r else pre + s + post

if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
a = input()
n = int(input())
rev = False
front = []
back = []
q = [input() for i in range(n)]
for i in q:
    if i == "1":
        rev = not rev
    else:
        i,f,c = i.split()
        if rev == False:
            if f == "1":
                front.append(c)
            else:
                back.append(c)
        else:
            if f == "1":
                back.append(c)
            else:
                front.append(c)
if rev == False:
    print("".join(list(reversed(front))+[a]+back))
else:
    print("".join(list(reversed(back))+[a[::-1]]+front))

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
r = 0
s = deque(S)
for i in range(Q):
    q = input().split()
    if q[0]=='1':
        r += 1
    else:
        f = int(q[1])
        f += r
        f %= 2
        if f==0:
            s.append(q[2])
        else:
            s.appendleft(q[2])
ans = ''.join(s)
if r%2==1:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
a = input()
n = int(input())

hanten = -1
atama = ""
oshiri = ""
for i in range(n):
    List = list(input().split())
    if List[0] == "1":
        hanten = hanten * -1
    else:
        if List[1] == "1":
            if hanten == -1:
                atama = List[2] + atama
            else:
                oshiri = oshiri + List[2]
        else:
            if hanten == -1:
                oshiri = oshiri + List[2]
            else:
                atama = List[2] + atama

ans = atama + a + oshiri
if hanten == 1:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
revflag = 0
before = ""
after = ""

for i in query:
    if i[0] == '1':
        revflag = 1 - revflag
    else:
        if (revflag == 0 and i[1] == '1') or (revflag == 1 and i[1] == '2'):
            before += i[2]
        else:
            after += i[2]

if revflag == 0:
    print(before[::-1]+S+after)
else:
    print(after[::-1]+S[::-1]+before)

Submission status: Accepted

Solution code:
s = input()
q = int(input())

is_reverse = False
front_queue = []
back_queue = []
for i in range(q):
    t = input()
    if t == "1":
        is_reverse = not is_reverse
    else:
        t2, f, c = t.split()
        if (f == "1" and not is_reverse) or (f == "2" and is_reverse):
            front_queue.append(c)
        else:
            back_queue.append(c)

result = ""
if is_reverse:
    result = "".join(reversed(back_queue)) + s[::-1] + "".join(front_queue)
else:
    result = "".join(reversed(front_queue)) + s + "".join(back_queue)
print(result)

Submission status: Accepted

Solution code:
def solve(string):
    s, q, *qs = string.split("\n")
    pre = []
    post = []
    r = False
    for _q in qs[::-1]:
        if _q == "1":
            r = not r
            continue
        _, f, c = _q.split()
        if r and f == "2" or not r and f == "1":
            pre.append(c)
        else:
            post.append(c)
    pre, post = "".join(pre), "".join(post[::-1])
    return pre + s[::-1] + post if r else pre + s + post


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
from collections import deque
def main():
    s = deque(input())
    q = int(input())
    appendleft=s.appendleft
    append=s.append
    flag = False
    for i in range(q):
        temp = input().split()
        if temp[0] == "1":
            flag = not flag
        else:
            if temp[1] == "1":
                if flag:
                    append(temp[2])
                else:
                    appendleft(temp[2])
            else:
                if flag:
                    appendleft(temp[2])
                else:
                    append(temp[2])
    if flag:
        s.reverse()
    print("".join(list(s)))
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
Q_list = list()
for i in range(Q):
    Q_list.append(input().split())
 
from collections import deque
def operate(i):
    global count, str_list
    if Q_list[i][0] == '1':
        count += 1
    else:
        if count % 2 == 0:
            if Q_list[i][1] == '1':
                str_list.appendleft(Q_list[i][2])
            else:
                str_list.append(Q_list[i][2])
        else:
            if Q_list[i][1] == '1':
                str_list.append(Q_list[i][2])
            else:
                str_list.appendleft(Q_list[i][2])
 
check = 0
for i in range(Q):
    if Q_list[i][0] == '1':
        check += 1
 
S_list = [i for i in S]
if check % 2 == 1:
    S_list = list(reversed(S_list))
 
S_n = ''.join(S_list)
 
str_list = deque()
str_list.append(S_n)
count = check%2
for i in range(Q):
    operate(i)
print(''.join(str_list))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
A = [input().split() for i in range(Q)]

tmp = 0
s = S
t = ''

for li in A:
#     print(s,t,tmp)
    if li[0] == '1':
        tmp = (tmp+1)%2
    else:
        if (tmp+int(li[1]))%2 == 1:
            t += li[2]
        else:
            s += li[2]
            
if tmp == 0:
    print(t[::-1]+s)
else:
    print(s[::-1]+t)

Submission status: Accepted

Solution code:
s = input()
quantity = int(input())
L = [input().split() for i in range(quantity)]

hanten = -1
atama = ""
shiri = ""

for row in range(quantity):
  each = L[row]
  if each[0] == "1":
    hanten = hanten * -1
  else:
    if each[1] == "1":
      if hanten == -1:
        atama = atama + each[2]
      else:
        shiri = shiri + each[2]
    else:
      if hanten == -1:
        shiri = shiri + each[2]
      else:
        atama = atama + each[2]

atama = atama[::-1]
ans = atama + s + shiri

if hanten == 1:
  ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())
f = 0
for _ in range(Q):
  a, *b = input().split()
  f += int(a)
  if a == "2":
    if (int(b[0]) + f) % 2 == 1 :
      S.appendleft(b[1])
    else:
      S.append(b[1])
D = list(S)
print ("".join(D) if f % 2 == 0 else "".join(D[::-1]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
s=input().strip()
q=int(input())
from collections import deque
que=deque(s)
mae=1
for _ in range(q):
    query=list(input().split())
    if query[0]=="1":
        mae*= -1
    else:
        if mae==1:
            if query[1]=="1":
                que.appendleft(query[2])
            else:
                que.append(query[2])
        else:
            if query[1]=="1":
                que.append(query[2])
            else:
                que.appendleft(query[2])
    #print(que)
if mae==1:
    print("".join(que))
else:
    print("".join(list(que)[::-1]))

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
A= [input().split() for i in range(Q)]

tmp=0
s=S
t=''

for li in A:
    if li[0]=='1':
        tmp=(tmp+1)%2
    else:
        if (tmp+int(li[1]))%2 == 1:
            t += li[2]
        else:
            s += li[2]

if tmp == 0:
    print(t[::-1]+s)
else:
    print(s[::-1]+t)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
is_reverse = 0
left = ''
for i in range(q):
    query = input()
    if query[0] == '1':
        is_reverse = (is_reverse+1) % 2
    else:
        _, f, c = query.split()
        if (is_reverse+int(f))%2 == 0:
            s += c
        else:
            left += c
s = left[::-1] + s
if is_reverse:
    print(s[::-1])
else:
    print(s)

Submission status: Accepted

Solution code:
import collections


class Formatter:
    def __init__(self, s):
        self._d = collections.deque(s)
        self._reversed = False

    def reverse(self):
        self._reversed = not self._reversed

    def append(self, s):
        if self._reversed:
            self._d.appendleft(s)
        else:
            self._d.append(s)

    def appendleft(self, s):
        if self._reversed:
            self._d.append(s)
        else:
            self._d.appendleft(s)

    def __iter__(self):
        if self._reversed:
            return reversed(self._d)
        else:
            return iter(self._d)


S = input()
Q = int(input())

d = Formatter(S)

for _ in range(Q):
    Qi = input()
    if Qi == "1":
        d.reverse()
    else:
        T, F, C = Qi.split()
        if F == "1":
            d.appendleft(C)
        else:
            d.append(C)

print("".join(d))

Submission status: Accepted

Solution code:
import collections


class Formatter:
    def __init__(self, s):
        self._d = collections.deque(s)
        self._d_reverse = collections.deque(reversed(s))

    def reverse(self):
        self._d, self._d_reverse = self._d_reverse, self._d

    def append(self, s):
        self._d.append(s)
        self._d_reverse.appendleft(s)

    def appendleft(self, s):
        self._d.appendleft(s)
        self._d_reverse.append(s)

    def __iter__(self):
        return iter(self._d)


S = input()
Q = int(input())

d = Formatter(S)

for _ in range(Q):
    Qi = input()
    if Qi == "1":
        d.reverse()
    else:
        T, F, C = Qi.split()
        if F == "1":
            d.appendleft(C)
        else:
            d.append(C)

print("".join(d))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
hanten = 1
front = []
back = []
for _ in range(Q):
    q = list(map(str, input().split()))
    if q[0]=='1':
        hanten *= -1
    else:
        if hanten==1:
            if q[1]=='1':
                front.append(q[2])
            else:
                back.append(q[2])
        else:
            if q[1]=='1':
                back.append(q[2])
            else:
                front.append(q[2])


if hanten==1:
    print(''.join(list(reversed(front)) + list(S) + back))
else:
    print(''.join(list(reversed(list(S)+back))+front))

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
query = [input() for _ in range(Q)]

S = deque(S)

inv = 0
for q in query:
    if q[0] == "1":
        inv += 1
    else:
        _, f, c = q.split()
        if f == "1":
            if inv % 2 == 0:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if inv % 2 == 0:
                S.append(c)
            else:
                S.appendleft(c)
S = "".join(S)
print(S if inv % 2 == 0 else S[::-1])