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: Wrong Answer

Solution code:
s = input()
q = int(input())
add_head = ""
add_tail = ""
rev = False
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        rev = not rev
    else:
        if query[1] == "1":
            if rev:
                add_tail = add_tail + query[2]
            else:
                add_head = add_head + query[2]
        else:
            if rev:
                add_head = add_head + query[2]
            else:
                add_tail = add_tail + query[2]
                
if not rev:
    print(add_head[::-1] + s + add_tail)
else:
    print(add_tail[::-1] + s + add_head)

Submission status: Wrong Answer

Solution code:
S = input()

reverse = False

Q = int(input())

frontstack = ''
backstack = ''

for _ in range(Q):
    qstr = input()
    qlist = qstr.split()
    T = int(qlist[0])
    if T == 1:
        reverse = False if reverse == True else True
    elif T == 2:
        F = int(qlist[1])
        C = qlist[2]
        if F == 1:
            if reverse is False:
                frontstack += C
            else:
                backstack += C
        elif F == 2:
            if reverse is False:
                backstack += C
            else:
                frontstack += C

if reverse is False:
    ans = frontstack[::-1] + S + backstack
else:
    ans = frontstack[::-1] + S[::-1] + backstack

print(ans)

Submission status: Wrong Answer

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

cnt=1
for _ in range(q):
    query=list(input().split())
    if query[0]=='1':
        cnt=(cnt+1)%2
    else:
        if query[1]=='1':
            if cnt==1:
                s=s+query[2]
            else:
                s=query[2]+s
        else:
            if cnt==1:
                s=query[2]+s
            else:
                s=s+query[2]
print(s[::-1] if cnt==1 else s)

Submission status: Wrong Answer

Solution code:
a = input()
q = int(input())
key = [0]*len(a)
for i in range(len(a)):
    key[i] = a[i]

    
for i in range(q):
    qinput = [str(x) for x in input().split()]
    if len(qinput) == 1:
        reversed(key)
    else:
        k = qinput[1]
        if k == "1":
            key.append(qinput[2])
        else:
            reversed(key)
            key.append(qinput[2])
            reversed(key)

print("".join(key))

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
count=0
for i in range(q):
    a=list(input().split())
    if a[0]=="1":
        count+=1
    elif a[0]=="2":
        if (int(a[0]))%2==0:
            s=str(a[2])+s
        else:
            s=s+str(a[2])
        
if count%2==0:
  print(s)
else:
  p=list(s)
  p=p[::-1]
  print("".join(p))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque

SL = list(input())
Qn = int(input())

#%%
S = deque()
for s in SL:
    S.append(s)
    

for i in range(Qn):
    t = list(map(str,input().split()))
    if len(t)==1:
        top = S[0]
        bot = S[-1]
        S.popleft()
        S.appendleft(bot)
        S.pop()
        S.append(top)
    else:
        if t[1] == '1':
            S.appendleft(t[2])
        else:
            S.append(t[2])
print(''.join(S))

Submission status: Wrong Answer

Solution code:

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

import sys
input = sys.stdin.readline

qs = [list(input().split()) for _ in range(q)]

left = ''
right = ''


front = True
for i in qs:
    if(i[0] == '1'):
        front = not front
        continue

    f = int(i[1])
    c = i[2]
    if(front + f == 2):
        right = right + c
    else:
        left = left + c

if(not front):
    ans = left[::-1] + s + right
else:
    ans = right[::-1] + s + left

print(ans)

Submission status: Wrong Answer

Solution code:

S=list(input())

D={}
MIN=0
MAX=len(S)-1
for i in range(len(S)):
    D[i]=S[i]

Q=int(input())
for i in range(Q):
    q=list(input().split())
    if q[0]=="1":
        D[MIN],D[MAX]=D[MAX],D[MIN]
    else:
        if q[1]=="1":
            D[MIN-1]=q[2]
            MIN-=1
        else:
            D[MAX+1]=q[2]
            MAX+=1
    #print(D)
ans=""
for i in range(MIN,MAX+1):
    ans+=D[i]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy

# If you use recursive call, uncomment this code
#sys.setrecursionlimit(10**6)

def main():
    S = input()
    Q = int(input())
    head = ""
    tail = ""
    is_vert_normal = True
    for _ in range(Q):
        query = input().split()
        T = int(query[0])
        if T == 1:
            is_vert_normal = not is_vert_normal
        else:
            F = int(query[1])
            C = query[2]
            if F == 1:
                if is_vert_normal:
                    head += C
                else:
                    tail += C
            else:
                if not is_vert_normal:
                    head += C
                else:
                    tail += C
    if is_vert_normal:
        print(head[::-1] + S + tail)
    else:
        print(tail[::-1] + S + head)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
def resolve():
    S = deque(list(sr().split()))
    Q = ir()
    rev = 1
    for i in range(Q):
        T = list(sr().split())
        if T[0] == '1':
            rev *= -1
        else:
            x, F, C = T
            if F == '1':
                if rev == 1:
                    S.appendleft(C)
                else:
                    S.append(C)
            else:
                if rev == 1:
                    S.append(C)
                else:
                    S.appendleft(C)
    if rev == -1:
        S.reverse()
    print(*S, sep='')
resolve()

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
query = [input().split() for _ in range(q)]

add_texts = ["", ""]
add_direction = -1
for q_i in query:
    if q_i[0] == "1":
        add_direction = ~add_direction
    else:
        f, c = q_i[1:]
        if f == "1":
            add_texts[add_direction] += c
        else:
            add_texts[~add_direction] += c

print((add_texts[0][::-1] + s + add_texts[-1])[::[1, -1][add_direction]])

Submission status: Wrong Answer

Solution code:
S = [str(input())]
for Q in range(1,int(input())+1):
    t = 0
    query = list(map(str, input().split()))
    if len(query)==1:
        S.reverse()
    else:
        if int(query[1])==1:
            S = [query[2]] + S
        else:
            S = S + [query[2]]
print("".join(S))

Submission status: Wrong Answer

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

for i in range(Q):
    query = input()
    if len(query.split()) == 1:
        S = S[-1] + S[1: -1] + S[0]
    else:
        t_i, f_i, c_i = query.split()
        f_i = int(f_i)
        if f_i == 1:
            S = c_i + S
        else:
            S = S + c_i
print(S)

Submission status: Wrong Answer

Solution code:
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

S= one_str()

Q = one_int()

flg=True

for i in range(Q):
    temp = input()
    if len(temp)>=2:
        T, F, C = temp.split()
        if F=="1":
            S = S+C
        else:
            S = C+S
    else:        
        S=S[::-1]

print(S[::-1])

Submission status: Wrong Answer

Solution code:
from collections import deque

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

reverseFlag = 1

for _ in range(q):
    query = input().strip()
    if query[0] == "1":
        reverseFlag = 1 - reverseFlag
    else:
        lf = reverseFlag + int(query[2])
        if lf % 2 == 0:
            s.append(query[4])
        else:
            s.appendleft(query[4])

if reverseFlag == 1:
    s.reverse()
print("".join(s))

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
for i in range(q):
  t = input().split()
  if t[0] == "1":
    h = s[-1]
    s[-1] = s[0]
    s[0] = h
  else:
    if t[1] == "1":
      s = [t[2]]+s
    else:
      s = s+[t[2]]
print("".join(s))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
top = ""
end = ""
count = 0
for i in range(q):
    a = list(input().split())
    if len(a) == 1:
        count += 1
    else:
        if (count + int(a[1])) % 2 == 0:
            end = a[2] + top
        else:
            top = a[2] + top
ans = top + s + end
if count % 2 == 1:
    ans = ans[::-1]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
S=list(input())

D={}
MIN=0
MAX=len(S)-1
for i in range(len(S)):
    D[i]=S[i]

Q=int(input())
for i in range(Q):
    q=list(input().split())
    if q[0]=="1":
        D[MIN],D[MAX]=D[MAX],D[MIN]
    else:
        if q[1]=="1":
            D[MIN-1]=q[2]
            MIN-=1
        else:
            D[MAX+1]=q[2]
            MAX+=1
ans=""
for i in range(MIN,MAX+1):
    ans+=D[i]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

for _ in range(q):
  query = input()
  if query == '1':
    reverse = 1 - reverse
  else:
    _,f,c = query.split()
    if f == '1':
      if reverse:
        s.appendleft(c)
      else:
        s.append(c)
    else:
      if reverse:
        s.append(c)
      else:
        s.appendleft(c)
if not  reverse:
  s.reverse()

print(''.join(s))

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    import sys
    input = sys.stdin.readline

    S = input().rstrip()
    Q = int(input())

    dq = deque(S)

    for _ in range(Q):
        Query = iter(input().rstrip().split())
        T = next(Query)
        if T == '1':
            # Sの前後を反転する
            if len(dq) >= 2:
                head = dq.popleft()
                tail = dq.pop()
                dq.appendleft(tail)
                dq.append(head)

        else:
            F, C = Query
            if F == '1':
                # Sの先頭にC
                dq.appendleft(C)

            else:
                # Sの末尾にC
                dq.append(C)

    res = ''.join(dq)
    print(res)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
hanten=0
mae=""
ato=""

for i in range(q):
    query=input()
    if query=="1":
        hanten=(hanten+1)%2
    else:
        query=query.split()
        if query[1]=="1":
            if hanten==0:
                mae+=query[2]
            else:
                ato+=query[2]
        else:
            if hanten==0:
                ato+=query[2]
            else:
                mae+=query[2]

answer=""
if hanten==0:
    lem=len(mae)
    for i in range(lem):
        answer+=mae[lem-i-1]
    answer+=s
    answer+=ato
else:
    lem=len(ato)
    for i in range(lem):
        answer+=ato[lem-i-1]
    answer+=s
    answer+=mae
    
print(answer)

Submission status: Wrong Answer

Solution code:

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

front = []
back = []
cnt = 0
for i in range(q):
    t = input()
    if t[0] == '1':
        cnt ^= 1
    else:
        f = int(t[2])
        c = t[4]
        if cnt ^ f:
            front.append(c)
        else:
            back.append(c)

ans = ''.join(front)[::-1] + s + ''.join(back)
print(ans[::-1] if cnt else ans)
# print(cnt)

Submission status: Wrong Answer

Solution code:
a = list(input().split())

q = int(input())

queries = [list(input().split()) for i in range(q)]

# print(a)

for query in queries:
    if len(query) == 1:
        a.reverse()
    else:
        command, f, chara = query
        if f == '1':
            a.insert(0, chara)
        else:
            a.append(chara)

# print(a)
print(''.join(a))

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    s = str(input())
    q = inp()
    before = []
    after = []
    flag = 1
    for _ in range(q):
        query = inpls()
        if query[0] == '1':
            flag = 1-flag
        else:
            f = query[1]
            S = query[2]
            if (flag and f=='2') or ((not flag) and f=='1'):
                after.append(S)
            else:
                before.append(S)
    ans = []
    for i in range(len(before)):
        ans.append(before[-1-i])
    ans.append(s)
    for i in range(len(after)):
        ans.append(after[i])
    if not flag:
        ans.reverse()
    print(''.join(ans))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
s=input()
q=int(input())
sign=1
F,B="",""
for i in range(q):
  Q=[str(i) for i in input().split()]
  if Q[0]=="1":
    sign*=-1
  else:
    f,c=Q[1:]
    if f=="1" and sign==1:
      F=c+F
    elif f=="2" and sign==-1:
      F=c+F
    else:
      B=B+c
ans=F+s+B
print(ans if sign==1 else ans[::-1])

Submission status: Wrong Answer

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
def resolve():
    S = deque(list(sr().split()))
    Q = ir()
    for i in range(Q):
        T = list(sr().split())
        if T[0] == '1':
            S.reverse()
        else:
            x, F, C = T
            if F == '1':
                S.appendleft(C)
            else:
                S.append(C)
    print(*S, sep='')
resolve()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

inv_cnt = 0


li = []
r = []

for i in range(q):
    d = [x for x in input().split()]
    if d[0] == '1':
        inv_cnt += 1
        inv_cnt %= 2
    else:
        if inv_cnt == 0:
            if d[1] == '1':
                li.append(d[2])
            else:
                r.append(d[2])
        else:
            if d[1] == '1':
                r.append(d[2])
            else:
                li.append(d[2])

if inv_cnt == 0:
    print(''.join(li[::-1]) + s + ''.join(r[::-1]))
else:
    print(''.join(r) + s[::-1] + ''.join(li))

Submission status: Wrong Answer

Solution code:
# ABC158d


def main():
    import sys
    sys.setrecursionlimit(10**6)
    # 再帰関数を使わない限りPypyで出すこと

    def dump(*args):
        sys.stderr.write(str(args))

    s = input()
    q = int(input())
    query = [input().split() for _ in range(q)]

    mode = 0
    from collections import deque
    que = deque()
    que.append(s)
    for i in query:
        if len(i) == 1:
            mode = 1-mode
            continue
        f = int(i[1])
        f -= 1
        if f ^ mode == 0:
            que.appendleft(i[2])
        else:
            que.append(i[2])
    mode = 1 if mode == 0 else - 1
    qq = list(que)
    print(*qq[::mode], sep='')


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S=ST()
    Q=I()
    query = []
    for q in range(Q):
        *s, = ST().split()
        query.append(s)
    left = []
    right = deque()
    rev = False
    for q in query[::-1]:
        if q[0] == "1":
            rev ^= True
        else:
            to_right = int(q[1]) - 1
            s = q[2]
            if( rev ):
                if( to_right ):
                    left.append(s)
                else:
                    right.appendleft(s)
            else:
                if( to_right ):
                    right.appendleft(s)
                else:
                    left.append(s)
    # ans = str(left) + S + str(right)
    ans = "".join(left) + S + "".join(right)
    
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
for i in range(q):
  t = input().split()
  if t[0] == "1":
    h = s[-1]
    s[-1] = s[0]
    s[0] = h
  elif t[1] == "1":
    s = list(t[2])+s
  else:
    s = s+list(t[2])
print("".join(s))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
Qu= [list(input().split()) for _ in range(Q)]

r = 0

B = []
A = []

for i in range(Q):
  if int(Qu[i][0]) == 1:
    r = 1 - r
  else:
    F = int(Qu[i][1])
    C = Qu[i][2]
    F -= 1
    if F == r:
      B.append(C)
    else:
      A.append(C)

if r == 0:
  B.reverse()
  print("".join(B + [S] + A))
else:
  A.reverse()
  print("".join(A + [S] + B))

Submission status: Wrong Answer

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

prefix = ''
suffix = ''
mode = 0
for _ in range(Q) :
  Query = list(input().split())
  if Query[0] == '1' :
    mode ^= 1
  else :
    _, F, C = Query
    F = int(F) - 1
    
    if mode != F :
      suffix += C
    else :
      prefix += C
      

if mode == 0 :
  print(prefix[::-1] + S + suffix)
else :
  print(suffix[::-1] + S + prefix)

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    s = str(input())
    q = inp()
    before = []
    after = []
    flag = 1
    for _ in range(q):
        query = inpls()
        if query[0] == '1':
            flag = 1-flag
        else:
            f = query[1]
            S = query[2]
            if (flag and f==2) or ((not flag) and f==1):
                after.append(S)
            else:
                before.append(S)
    ans = []
    for i in range(len(before)):
        ans.append(before[-1-i])
    ans.append(s)
    for i in range(len(after)):
        ans.append(after[i])
    if not flag:
        ans.reverse()
    print(''.join(ans))
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s=input()
a=[]
b=[]
d=0
for i in range(int(input())):
  q=input()
  if q=="1":d^=1
  else:
    _,f,c=q.split()
    f=int(f)-1
    if d==0:
      if f==0:a.append(c)
      else:b.append(c)
    else:
      if f==1:a.append(c)
      else:b.append(c)
if d==0:print("".join(a)[::-1]+s+"".join(b))
else:print("".join(b)[::-1]+s+"".join(a))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
s = input().rstrip('\n')
q = int(input())
s1 = []
s2 = []
k = 0
for i in range(q):
    r = input().rstrip("\n")
    if r == "1":
        k = (k + 1) % 2
    else:
        _, f, c = r.split()
        f = int(f) - 1
        if (k + f) % 2:
            s2.append(c)
        else:
            s1.append(c)
s1.reverse()
ans = s1 + [s] + s2

if k:
    ans.reverse()
print("".join(ans))

Submission status: Wrong Answer

Solution code:
import sys
#input = sys.stdin.buffer.readline
from collections import deque

def main():
    s = list(input())
    s = deque(s)
    q = int(input())
    for _ in range(q):
        x = list(input().split())
        if x[0] == "1":
            s[0],s[-1] = s[-1],s[0]
        else:
            if x[1] == "1":
                s.appendleft(x[2])
            else:
                s.append(x[2])
    
    print("".join(s))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


s = list(input())
q = int(input())
query = [list(input().split()) for i in range(q)]

deq = deque(s)

flag = 1
for i in range(q):
    if query[i][0] == "1":
        flag ^= 1
    else:
        _, f, c = query[i]
        f = int(f) % 2
        if flag ^ f:
            deq.appendleft(c)
        else:
            deq.append(c)

ans = "".join(deq)
if not flag:
    print(ans)
else:
    print(ans[::-1])

Submission status: Wrong Answer

Solution code:
#B
Str = str(input())
Q = int(input())
S = []
S.append(Str)
for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        S.reverse()
    elif a[1] == '1':
        S.insert(0, a[2])
    else:
        S.append(a[2])
print(''.join(S))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    S=input()
    Q=int(input())
    f=False
    ans=deque([S])
    for _ in range(Q):
        Query=list(input().split())
        if Query[0]=='1':
            f=not f
        else:
            F,C=Query[1],Query[2]
            if F=='1':
                if not f:
                    ans.appendleft(C)
                else:
                    ans.append(C)
            else:
                if not f:
                    ans.append(C)
                else:
                    ans.appendleft(C)
    if f:
        ans=list(reversed(ans))
    print(''.join(ans))
        
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
count=0
for i in range(q):
    a=list(input().split())
    if a[0]=="1":
        count+=1
    elif a[0]=="2":
        if (int(a[1])+count)%2!=0:
            s=str(a[2])+s
        else:
            s=s+str(a[2])
print(s)

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    import sys
    input = sys.stdin.readline

    S = input().rstrip()
    Q = int(input())

    dq = deque(S)

    for _ in range(Q):
        Query = iter(input().rstrip().split())
        if next(Query) == '1':
            # Sの前後を反転する
            if len(dq) >= 2:
                head = dq.popleft()
                tail = dq.pop()
                dq.appendleft(tail)
                dq.append(head)

        else:
            if next(Query) == '1':
                # Sの先頭にC
                C = next(Query)
                dq.appendleft(C)

            else:
                # Sの末尾にC
                C = next(Query)
                dq.append(C)

    res = ''.join(dq)
    print(res)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Wrong Answer

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

head = []
tail = []
flip = 0

for i in range(q):
    query = input().split()
    if query[0] == "1":
        flip ^= 1
    else:
        f = int(query[1]) - 1
        c = query[2]
        if (f ^ flip) == 0:
            head.append(c)
        else:
            tail.append(c)

if flip:
    head, tail = tail, head

head = "".join(head)
tail = "".join(tail)

print(head[::-1] + s + tail)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
s = input()
q = int(input())
hanten = 0
mae = ""
usiro = ""
for _ in range(q):
    a = list(input().split())
    if a[0] == "1":
        hanten += 1
    else:
        if hanten % 2 == 0:
            if a[1] == "1":
                mae += a[2]
            if a[1] == "2":
                usiro += a[2]
        else:
            if a[1] == "1":
                usiro += a[2]
            if a[1] == "2":
                mae += a[2]
if hanten % 2 == 0:
    ans = str(mae[::-1]) + s + (usiro)
else:
    ans = usiro[::-1] + s[::-1] + mae
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


from collections import deque
reverse = -1
S = deque(input())
Q = int(input())
for _ in range(Q):
    Query = input().split()
    if Query[0] == "1":
        reverse *= -1
    else:
        if Query[1] == "1":
            if reverse == 1:
                S.append(Query[2])
            else:
                S.appendleft(Query[2])
        else:
            if reverse == 1:
                S.appendleft(Query[2])
            else:
                S.append(Query[2])

ans = "".join(S)
if reverse == 1:
    ans = ans[::-1]
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
flag = 1
for i in range(q):
    A = list(input().split())
    if A[0] == '1':
        flag *= -1
    else:
        if flag * int(A[1]) == 1 or -2:
            s = A[2]+s
        else:
            s = A[2]+s
if flag == -1:
    print(s[::-1])
else:
    print(s)

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    import sys
    input = sys.stdin.readline

    S = input().rstrip()
    Q = int(input())

    dq = deque(S)

    for _ in range(Q):
        Query = iter(input().rstrip().split())
        if next(Query) == '1':
            # Sの前後を反転する
            head = dq.popleft()
            tail = dq.pop()
            dq.appendleft(tail)
            dq.append(head)

        else:
            if next(Query) == '1':
                # Sの先頭にC
                C = next(Query)
                dq.appendleft(C)

            else:
                # Sの末尾にC
                C = next(Query)
                dq.append(C)

    res = ''.join(dq)
    print(res)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
s=input()
q=int(input())
sign=1
for i in range(q):
  Q=[str(i) for i in input().split()]
  if Q[0]=="1":
    sign*=-1
  else:
    f,c=Q[1:]
    if f=="1" and sign==1:
      s=c+s
    elif f=="2" and sign==-1:
      s=c+s
    else:
      s=s+c
print(s if sign==1 else s[::-1])

Submission status: Wrong Answer

Solution code:
from collections import deque


s = list(input().split())
q = int(input())
query = [list(input().split()) for i in range(q)]

deq = deque(s)


flag = 1
for i in range(q):
    if query[i][0] == "1":
        flag ^= 1
    else:
        _, f, c = query[i]
        f = int(f) % 2
        if flag ^ f:
            deq.appendleft(c)
        else:
            deq.append(c)

ans = "".join(deq)
if not flag:
    print(ans)
else:
    print(ans[::-1])

Submission status: Wrong Answer

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

r_addition = ""
l_addition = ""
is_rev = False

for i in range(q):
    qs = input().split()

    if len(qs) == 1:
        is_rev = not is_rev
    else:
        if int(qs[1]) == 1:
            if is_rev:
                r_addition += qs[2]
            else:
                l_addition += qs[2]
        else:
            if is_rev:
                l_addition += qs[2]
            else:
                r_addition += qs[2]

# print(l_addition)
# print(r_addition)
#
# print(l_addition[::-1])
# print(r_addition[::-1])

if is_rev:
    print(r_addition[::-1]+s+l_addition)
else:
    print(l_addition[::-1]+s+r_addition)

Submission status: Wrong Answer

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