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())

rev = False

forward = ''
back = ''

for i in range(q):
    tfc = input()
    if len(tfc) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        t, f, c = tfc.split()
        if f == '1':
            if rev:
                back += c
            else:
                forward += c
        else:
            if rev:
                forward += c
            else:
                back += c

if not rev:
    s = forward[::-1] + s + back
else:
    s = back[::-1] + s + forward

print(s)

Submission status: Wrong Answer

Solution code:
from sys import stdin
sin = stdin.readline().rstrip()
q = int(stdin.readline().rstrip())
li = [list(stdin.readline().rstrip().split()) for _ in range(q)]
point = 0
omote = ""
ura = ""
for i in li:
    if i[0] == "1":
        point += 1
    elif i[0] == "2":
        if i[1] == "1":
            if point%2 == 0:
                omote += i[2]
            else:
                ura += i[2]
        else:
            if point%2 == 0:
                ura += i[2]
            else:
                omote += i[2]
omote = omote[::-1]
if point%2 == 1:
    sin = sin[::-1]
    sin = omote + sin
    sin += ura
    sin = sin[::-1]
else:
    sin = omote + sin
    sin += ura
print(sin)

Submission status: Wrong Answer

Solution code:
s = input()
Q = int(input())
reverse = True

for _ in range(Q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        reverse = not reverse
    else:
        if (reverse and query[1] == "1") or (not reverse and query[1] == "2"):
            s = s + query[2]
        else:
            s = query[2] + s
if reverse:
    print(s[::-1])
else:
    print(s)

Submission status: Wrong Answer

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

Queries = []
#pop_flag = True => 正常
#pop_flag = False => 反転している
pop_flag = True
S_stack = S.split()
for i in range(Q):
  Querie = [str(j) for j in input().split()]
# 反転させるやつ
  if Querie[0] == "1":
    if pop_flag == True:
      pop_flag = False
    elif pop_flag == False:
      pop_flag = True

# 追加するやつ
  elif Querie[0] == "2":
    if Querie[1] == "1":
      if pop_flag == True:
        S_stack.insert(0,Querie[2])
      elif pop_flag == False:
        S_stack.append(Querie[2])
    elif Querie[1] == "2":
      if pop_flag == True:
        S_stack.append(Querie[2])
      elif pop_flag == False:
        S_stack.insert(0,Querie[2])

if pop_flag == False:
  S_stack.reverse()

ans = ''.join(S_stack)
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
S = [s for s in S]

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        tmp = S[0]
        S[0] = S[-1]
        S[-1] = tmp
    else:
        f_i = int(query[1])
        c_i = query[2]
        if f_i == 1:
            S.insert(0, c_i)
        else:
            S.append(c_i)
print("".join(S))

Submission status: Wrong Answer

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

r_flg = False
for i in range(Q):
    q = input()
    if q == '1':
        r_flg = True
        continue
    t,f,c = q.split()
    if f == '1':
        S = c + S if not r_flg else S + c
    else:
        S = S + c if not r_flg else c + S
if r_flg:
    print(S[::-1])
else:
    print(S)

Submission status: Wrong Answer

Solution code:
import math
s=input().strip()
st=s 
rev=s 
q=int(input())
cc=0
for i in range(q):
    ty=list(map(str,input().split()))
    if len(ty)==1:
        cc+=1 
        st,rev=rev,st
    else:
        if int(ty[1])==1:
            st=ty[2]+st  
            rev=rev+ty[2]
        else:
            st=st+ty[2]
            rev=ty[2]+rev 
print(st)

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
c = 0
s1 = []
s2 = []
for _ in range(q):
    t = input()
    if t != "1":
        t = t.split()
        if t[1] == "1":
            if c % 2 == 0:
                s1.append(t[2])
            else:
                s2.append(t[2])
        else:
            if c % 2 == 0:
                s2.append(t[2])
            else:
                s1.append(t[2])
    else:
        c += 1

if c % 2 == 0:
    s1.reverse()
    s = s1 + s + s2
else:
    s2.reverse()
    s = s2 + s + s1
print("".join(s))

Submission status: Wrong Answer

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

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

for i in range(q):
    qi=list(input().split())
    if qi[0]=="1":
        s=s[::-1]
    else:
        f,c=qi[1],qi[2]
        if f=="1":
            s=c+s
        else:
            s=s+c
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
S=input()
Q=int(input())
d = deque([S])
flag=0
for i in range(Q):
    x=input().split()
    if len(x)==1:
        flag=(flag+1)%2
    else:
        if int(x[1])%2==0:
            if flag==0:
                d.appendleft(x[2])
            else:
                d.append(x[2])
        else:
            if flag==0:
                d.append(x[2])
            else:
                d.appendleft(x[2])
        

if flag==1:
    print(*d, sep="")
    

else:
    d=list(reversed(d))
    print(*d, sep="")

Submission status: Wrong Answer

Solution code:
from collections import deque

S = deque(list(input()))
Q = int(input())
for _ in range(Q):
    inp = input()
    if len(inp) == 1:
        T = int(inp)
        if len(S) != 1:
            left_val = S.popleft()
            right_val = S.pop()
            S.append(left_val)
            S.appendleft(right_val)
        else:
            pass
    else:
        T, F = map(int, inp.split()[:2])
        C = inp.split()[-1]
        if F == 1:
            S.appendleft(C)
        else:
            S.append(C)

print("".join(S))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
ans = ["",S,""]
turncnt = 0
for i in range(Q):
    query = input().split()
    if (query[0] == "1"):
        turncnt = turncnt + 1
    if (query[0] == "2"):
        if (query[1] == "1"):
            if (turncnt % 2 == 0):
                ans[2] = ans[2] + query[2]
            else:
                ans[0] = ans[0] + query[2]
        if (query[1] == "2"):
            if (turncnt % 2 == 0):
                ans[0] = ans[0] + query[2]
            else:
                ans[2] = ans[2] + query[2]

if (turncnt % 2 == 1):
    ans[1] = ans[1][::-1]    

print("".join(ans))

Submission status: Wrong Answer

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

for i in range(Q):
    q=[i for i in input().split()]
    if int(q[0])==1:
        f = (f+1)%2
    if int(q[0])==2:
        if int(q[1])==1:
            if f:
                S=q[2]+S
            else:
                S=S+q[2]
        else:
            if f:
                S=S+q[2]
            else:
                S=q[2]+S
if f:
    print(S)
else:
    print(S[::-1])

Submission status: Wrong Answer

Solution code:
import sys
s = input()

flont = []
back = []

if len(s) != 1:
    flont = [s[0]]
    back = [s[-1]]
else:
    flont = [s[0]]

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

    if int(s[0]) == 1:
        if len(flont)==0:
            sw = back[-1]
            back[-1] = back[0]
            back[0] = sw
        elif len(back)==0:
            sw = flont[-1]
            flont[-1] = flont[0]
            flont[0] = sw
        else:
            sw = flont[-1]
            flont[-1] = back[-1]
            back[-1] = sw

    elif int(s[0]) == 2:
        f = int(s[1])
        c = s[2]
        if f == 1:
            flont.append(c)
        else:
            back.append(c)
flont.reverse()
[sys.stdout.write(i) for i in flont]
[sys.stdout.write(i) for i in back]
print("")

Submission status: Wrong Answer

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

heads_min_idx = 0
heads_max_idx = 0
tails_min_idx = 0
tails_max_idx = 0
heads = {}
tails = {}
rev_flag = 1  # 1 or -
for q in range(Q):
    tmp_q = input()
    if tmp_q == '1':
        rev_flag *= -1
    else:
        _, f, c = tmp_q.split()
        if rev_flag == 1:
            if f == '1':
                heads[heads_min_idx - 1] = c
                heads_min_idx -= 1
            else:
                tails[tails_max_idx + 1] = c
                tails_max_idx += 1
        else:
            if f == '1':
                tails[tails_max_idx - 1] = c
                tails_max_idx -= 1
            else:
                heads[heads_min_idx - 1] = c
                heads_min_idx -= 1

if rev_flag == 1:
    head_str = ''.join([s for _, s in sorted(heads.items(), key=lambda x: x[0])])
    tail_str = ''.join([s for _, s in sorted(tails.items(), key=lambda x: x[0])])
    print(head_str + S + tail_str)
else:
    head_str = ''.join([s for _, s in sorted(tails.items(), key=lambda x: x[0], reverse=True)])
    tail_str = ''.join([s for _, s in sorted(heads.items(), key=lambda x: x[0], reverse=True)])
    print(head_str + S[::-1] + tail_str)

Submission status: Wrong Answer

Solution code:
s = input().split()

q=int(input())

for _ in range(q):
    t = input().split()
    if t[0] == '1':
        s.reverse()
    elif t[0] == '2':
        if t[1] == '1':
            s.insert(0, t[2])
        else:
            s.append(t[2])
print(''.join(map(str,s)))

Submission status: Wrong Answer

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

an=[]
an.append(s)

for i in range(q):
    li=list(map(str,input().split()))
    
    if li[0]=="1":
        an.reverse()
    else:
        if li[1]=="1":
            an.insert(0,li[2])
        else:
            an.append(li[2])

print(*an,sep="")

Submission status: Wrong Answer

Solution code:
S=(input())

Q=int(input())

fro=""
back=""
for _ in range(Q):
    temp=list(input().split())
    if len(temp)==1:
        S=S[::-1]
        fro,back=back[::-1],fro[::-1]
    else:
        if temp[1]=="1":
            fro+=(temp[2])
        else:
            back+=temp[2]
print(fro+S+back)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
s=str(input())
q=int(input())
for i in range(q):
    que=list(input().split())
    if len(que)==1:
        s=s[::-1]
    else:
        if int(que[1])==1:
            s=que[2]+s
        elif int(que[1])==2:
            s=s+que[2]
print(s)

Submission status: Wrong Answer

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

ans_lst = deque([S])
rev = False
for i in range(Q):
  QUERY = list(map(str, input().split()))
  if QUERY[0] == '1':
    if rev:
      rev = False
    else:
      rev = True
  else:
    if rev:
      if QUERY[1] == '1':
        ans_lst.append(QUERY[2])
      else:
        ans_lst.appendleft(QUERY[2])
    else:
      if QUERY[1] == '1':
        ans_lst.appendleft(QUERY[2])
      else:
        ans_lst.append(QUERY[2])    

if rev:
  ans_lst.reverse()
  print(''.join(ans_lst))
else:
  print(''.join(ans_lst))

Submission status: Wrong Answer

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

ans = s
for i in range(q):
    now = query[i]
    if now == ['1']:
        if len(ans) != 1:
            ans = ans[-1] + ans[1:-1] + ans[0]
        else:
            continue
    else:
        f = now[1]
        c = now[2]
        if f == '1':
            ans = c + ans
        elif f == '2':
            ans = ans + c

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

S = input()
Q = int(input())
d = deque()
for i in S:
    d.append(i)
flag = True
for _ in range(Q):
    q = [i for i in input().split()]
    if int(q[0]) == 1:
        flag = not flag
    else:
        if (int(q[1]) == 1 and flag) or (int(q[0]) == 2 and not flag):
            d.appendleft(q[2])
        else:
            d.append(q[2])
S = ""
while(len(d)>0):
    if flag:
        S += d.popleft()
    else:
        S += d.pop()
print(S)

Submission status: Wrong Answer

Solution code:
#
import sys
import math
import numpy as np
import itertools

# ひとつ入力
s = list(input())
# ひとつ入力
q = int(input())

t=0
a=[]
b=[]

for i in range(q):
    query = input().split()
    print(query)
    if query[0]=='1':
        t = (t+1)%2
    else:
        if (t+int(query[1]))%2==0:
            b.append(query[2])
        else:
            a.append(query[2])
p=[]
print(a,s,b)
if t == 0:
    a.reverse()
    p.extend(a)
    p.extend(s)
    p.extend(b)
else:
    b.reverse()
    s.reverse()
    p.extend(b)
    p.extend(s)
    p.extend(a)

for i in p:
    print(i,end="")
print("")

Submission status: Wrong Answer

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

for _ in range(q):
    ql = list(input().split())
    if ql[0] == '1':
        s[0], s[-1] = s[-1], s[0]
    else:
        if ql[1] == '1':
            s.insert(0, ql[2])
        else:
            s.append(ql[2])

print(*s, sep='')

Submission status: Wrong Answer

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

Queries = []
#pop_flag = True => 正常
#pop_flag = False => 反転している
pop_flag = True
for i in range(Q):
  Queries.append([str(j) for j in input().split()])

S_stack = S.split()

for Querie in Queries:
# 反転させるやつ
  if Querie[0] == "1":
    if pop_flag == True:
      pop_flag = False
    elif pop_flag == False:
      pop_flag = True

# 追加するやつ
  elif Querie[0] == "2":
    if Querie[1] == "1":
      if pop_flag == True:
        S_stack.insert(0,Querie[2])
      elif pop_flag == False:
        S_stack.append(Querie[2])
    elif Querie[1] == "2":
      if pop_flag == True:
        S_stack.append(Querie[2])
      elif pop_flag == False:
        S_stack.insert(0,Querie[2])

if pop_flag == False:
  S_stack.reverse()

ans = ''.join(S_stack)
print(ans)

Submission status: Wrong Answer

Solution code:
S = input().split(' ')
Q = int(input())

for _ in range(Q):
    q = input()
    if q == '1':
        S = S[::-1]
    else:
        _, f, c = q.split(' ')
        if f == '1':
            S = [c] + S
        else:
            S = S + [c]

print(''.join(S))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
QUERY = []
for i in range(Q):
    # q = list(map(int, input().split()))
    q = input().split()
    if q[0] == '1':
        QUERY.append([int(q[0])])
    else:
        QUERY.append([int(q[0]), int(q[1]), q[2]])

st = []
en = []
rev = False
rev_c = 0
for q in QUERY:
    if q[0] == 1:
        rev = True
        rev_c += 1
    else:
        if q[1] == 1:
            if not rev:
                st.append(q[2])
            else:
                en.append(q[2])
        else:
            if not rev:
                en.append(q[2])
            else:
                st.append(q[2])

st_s = "".join(st)
st_s = st_s[::-1]
en_s = "".join(en)

ans = st_s + S + en_s
if rev_c % 2 == 0:
    pass
else:
    ans = ans[::-1]

print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
f=0
l,r='',''
for i in range(q):
    a=list(input().split())
    if a[0]=='1':
        f+=1
    else:
        if (int(a[1])-1)^(f%2)==0:
            l+=a[2]
        else:
            r+=a[2]
if f%2==1:
    print(r+s[::-1]+l)
else:
    print(l[::-1]+s+r[::-1])

Submission status: Wrong Answer

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

Q = []
for i in range(q):
    Q.append(input())


print(s)
for operation in Q:
    if (operation[0] == '1'):
        s = s[::-1]
    else:
        if (operation[2] == '1'):

            s = operation[4] + s
        else:
            s += operation[4]


print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    S = input()
    Q = int(input())

    s = deque(S)
    for _ in [0] * Q:
        T = input().split()
        if T[0] == '1':
            s[0], s[-1] = s[-1], s[0]
        else:
            f, c = T[1], T[2]
            if f == '1':
                s.appendleft(c)
            else:
                s.append(c)
    print(''.join(s))


main()

Submission status: Wrong Answer

Solution code:
from collections import deque

S = input()
Q = int(input())
d = deque()
d.append(S)
cnt = 0
for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        cnt += 1
    else:
        if q[1] == '1':
            if cnt & 1:
                d.append(q[2])
            else:
                d.appendleft(q[2])
        else:
            if cnt & 1:
                d.appendleft(q[2])
            else:
                d.append(q[2])
if cnt & 1:
    ans = reversed(list(d))
else:
    ans = list(d)
print(''.join(ans))

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()), maxlen=1000000)
    Q = ir()
    rev = 1
    for i in range(Q):
        T = list(sr().split())
        if int(T[0]) == 1:
            rev *= -1
        else:
            x, F, C = T
            if int(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:
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 = True
    for _ in range(q):
        query = inpls()
        if query[0] == '1':
            if flag:
                flag = False
            else:
                flag = True
        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)-1,-1,-1):
        ans.append(before[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()
Q=int(input())
str_list_ = []
for q in range(Q):
    str_list_.append( input().split(' ') )

for q in range(Q):
    #print(q)
    #str_list = input().split(' ')
    str_list = str_list_[q]
    #print(str_list)
    if str_list[0]=='1':
        head=S[0]
        tail=S[len(S)-1]
        S=S[1:]
        S=tail+S
        S=S[0:-1]
        S=S+head
        #print(S)
    elif str_list[0]=='2':
        if str_list[1]=='1':
            S=str_list[2]+S
        elif str_list[1]=='2':
            S=S+str_list[2]
        #print(S)
print(S)

Submission status: Wrong Answer

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

cur = 1
res = [S]
for _ in range(Q):
    xs = input().split()

    if len(xs) == 1:
        cur = 2 if cur == 1 else 1
    else:
        _, f, c = xs
        if f == cur == 1 or f == cur == 2:
            res = res + [c]
        else:
            res = [c] + res

if cur == 1:
    ans = "".join(res)
else:
    ans = "".join(res[::-1])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
s = readline().strip()
n = int(readline())
q = sys.stdin.readlines()
for qi in q:
    if qi[0] == '1':
        l = len(s)
        if l >= 3:
            s = s[-1] + s[1:-1] + s[0]
        elif l == 2:
            s = s[-1] + s[0]
    else:
        c = qi[4]
        if qi[2] == '2':
            s = s + c
        else:
            s = c + s
print(s)

Submission status: Wrong Answer

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from sys import stdin
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)

'''

'''

def solve(l):
	n = len(l)
	

S = input()
a,b = S,S
Q = int(input())
zz = 0
for _ in range(Q):
	l = list(map(str, input().split()))
	if(len(l) == 1):
		a,b = b,a
		zz += 1
	else:
		q,f,c = l
		f = int(f)
		if(f == 1):
			a += c
			b = c + b
		else:
			a = c + a
			b += c
	# print("*",a,b)

print(b)

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
front = []
end = []
flip = False
for i in range(q):
    x = input()
    if x == "1":
        flip = flip ^ True
    else:
        z,f,c = x.split()
        f = bool(int(f)-1)
        if flip ^ f:
            front.append(c)
        else:
            end.append(c)
if not flip:
    s = s[::-1]
    ans = end[::-1] + s + front
else:
    ans = front[::-1] + s + end
print("".join(ans))

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
f=0
l,r='',''
for i in range(q):
    a=list(input().split())
    if a[0]=='1':
        f+=1
    else:
        if (int(a[1])-1)^(f%2)==0:
            l+=a[2]
        else:
            r+=a[2]
if f%2==1:
    print(r+s+l)
else:
    print(l[::-1]+s+r[::-1])

Submission status: Wrong Answer

Solution code:
from collections import deque

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

for _ in range(q):
    query = input().split()
    if query[0] == '1':
        S[0], S[-1] = S[-1], S[0]
    else:
        if query[1] == '1':
            S.appendleft(query[2])
        else:
            S.append(query[2])
print(*S, sep='')

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
rev = 0
head = []
tail = []
is_rev = False


for _ in range(q):
    qq = input().split()
    t = int(qq[0])
    if t == 1:
        rev += 1
        is_rev = not is_rev
    else:
        f = int(qq[1])
        c = qq[2]
        if f == 1:
            if is_rev:
                tail.append(c)
            else:
                head.append(c)
        else:
            if is_rev:
                head.append(c)
            else:
                tail.append(c)

if is_rev:
    ls = tail
    ls.extend(reversed(s))
    ls.extend(head)
else:
    ls = list(reversed(head))
    ls.extend(list(s))
    ls.extend(list(tail))
print(''.join(ls))

Submission status: Wrong Answer

Solution code:
s = str(input())
q = int(input())
Q = []
for i in range(q):
    query = list(map(str, input().split()))
    Q.append(query)

flip = -1
strs = '/'
for query in Q:
    if query[0] == '1':
        flip = flip * -1
    elif query[0] == '2':
        if query[1] == '1':
            if flip == -1:
                strs = query[2] + strs
            else:
                strs = strs + query[2]
        else:
            if flip == -1:
                strs = strs + query[2]
            else:
                strs = query[2] + strs

strs_f = ''
for i in range(len(strs)):
    if strs[i] == '/':
        break
    strs_f += strs[i]

strs_b = ''
for i in range(len(strs)-1, -1, -1):
    if strs[i] == '/':
        break
    strs_b += strs[i]

if flip == -1:
    ans = strs_f + s + strs_b
else:
    ans = strs_b[::-1] + s[::-1] + strs_f[::-1]

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
qvery = [0 for _ in range(q)]
rev = [0 for _ in range(q)]
for i in range(q):
    qvery[i] = list(input().split(' '))
    if i > 0:
        rev[i] = rev[i-1]
    if len(qvery[i]) == 1:
        rev[i] += 1
for i in range(q):
    if len(qvery[i]) != 1:
        qvery[i][1] = int(qvery[i][1])
        if qvery[i][1] == 1:
            if rev[i] % 2 == 0:
                s = s + qvery[i][2]
            else:
                s = qvery[i][2] + s
        else:
            if rev[i] % 2 == 1:
                s = s + qvery[i][2]
            else:
                s = qvery[i][2] + s
if rev[q-1] % 2 == 0:
    s = s[::-1]
print(s)

Submission status: Wrong Answer

Solution code:
s=list(input())
q=int(input())
for i in range(q):
    query=input().split()
    print(query)
    if query==['1']:
        s.reverse()
    else:
        if query[1]=='1':
            s.insert(0,query[2])
        else:
            s.append(query[2])
print(''.join(s))

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
#n = int(input())
#a, b = map(int, input().split())
s = input()
#x = list(map(int, input().split()))
q = int(input())
l = [s]


rev = 1
for i in range(q):
    que = list(map(str, input().split()))
    if que[0] == "1":
        rev *= -1

    else:
        if rev == 1:
            if que[1] == "1":
                l.insert(0, que[2])
            else:
                l.append(que[2])

        else:
            if que[1] == "1":
                l.append(que[2])
            else:
                l.insert(0, que[2])

if rev == 1:
    for i in range(len(l)):
        print(l[i], end="")
else:
    for i in range(len(l)):
        print(l[len(l) - i - 1], end="")

Submission status: Wrong Answer

Solution code:
from collections import deque
s=input()
q=int(input())
rev=0
head=deque()
tail=deque()
for _ in range(q):
    Q=input().split()
    if len(Q)==1:
        rev=1-rev
    if len(Q)==3:
        if(rev==0 and Q[1]=="1" )or (rev==1 and Q[1]=="2"):
            head.appendleft(Q[2])
        else :
            tail.append(Q[2])
if rev==0:
    ans="".join(head)+s+"".join(tail)

else:
    ans="".join(tail)[::-1]+s+"".join(head)[::-1]

print(ans)

Submission status: Wrong Answer

Solution code:
S = input().split(' ')
Q = int(input())

for _ in range(Q):
    q = input()
    if q == '1':
        S[-1], S[0] = S[0], S[-1]
    else:
        _, f, c = q.split(' ')
        if f == '1':
            S = [c] + S
        else:
            S = S + [c]

print(''.join(S))

Submission status: Wrong Answer

Solution code:
# 先頭と末尾を保ちつつ
s = input()
q = int(input())
x, y = [], []
r = 0
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        r += 1
        # これ遅いか？
        x, y = y[::-1], x[::-1]
    elif query[0] == "2":
        f, c = query[1], query[2]
        if f == "1":
            x.append(c)
        elif f == "2":
            y.append(c)
if r % 2 == 1:
    s = s[::-1]
print("".join(x) + s + "".join(y))

Submission status: Wrong Answer

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

ans = s
for i in range(q):
    now = query[i]
    if now == ['1']:
        ans = ans[-1] + ans[1:-1] + ans[0]
    else:
        f = now[1]
        c = now[2]
        if f == '1':
            ans = c + ans
        elif f == '2':
            ans = ans + c

print(ans)

Submission status: Wrong Answer

Solution code:
a = list(input())
n = int(input())
for i in range(n):
  s = list(input().split())
  if s[0] == "1":
    a[0],a[-1] = a[-1],a[0]
  else:
    if s[1] == "1":
      a.insert(0,s[2])
    else: a.append(s[2])  
ans = "".join(a)      
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
from functools import reduce
from operator import add
import sys

input = sys.stdin.readline

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

rev_cnt = 0
for i in range(q):
    query = input().split()
    if query[0] == '2':
        if (query[1] == '1' and rev_cnt % 2 == 0) or ((query[1] == '2' and rev_cnt % 2 == 1)):
            s.appendleft(query[2])
        else:
            s.append(query[2])
    else:
        rev_cnt += 1

res = reduce(add, s)
if rev_cnt % 2 == 1:
    res = res[::-1]

print(res)

Submission status: Wrong Answer

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

for i in range(Q):
    if len(qlis[i]) == 1:
        mae = S[0]
        usiro = S[-1]
        S[0] = usiro
        S[-1] = mae
    else:
        if int(qlis[i][1]) == 2:
            S.append(qlis[i][2])
        else:
            S.insert(0,qlis[i][2])

answer = "".join(S)
print(answer)

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
q = int(input())
flag = True
atama = deque()
osiri = deque()
for i in range(q):
    inp = input()
    if inp == '1':
        flag = not flag

    else:
        _, f, c = map(str, inp.split())
        if f == '1':
            if flag:
                atama.appendleft(c)
            else:
                osiri.append(c)
        else:
            if flag:
                osiri.append(c)
            else:
                atama.appendleft(c)

if flag:
    while(atama):
        tmp = atama.popleft()
        print(tmp, end='')
    print(s, end='')
    while(osiri):
        tmp = osiri.popleft()
        print(tmp, end='')
else:
    while(osiri):
        tmp = osiri.pop()
        print(tmp, end='')
    print(s, end='')
    while(atama):
        tmp = atama.pop()
        print(tmp, end='')

Submission status: Wrong Answer

Solution code:
from collections import deque
Str = str(input())
Q = int(input())
S = deque()
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.appendleft(a[2])
    else:
        S.append(a[2])

print(''.join(S))