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:
S1=S2=input()
N=int(input())
count=0
for _ in range(N):
    *Q,=input().split()
    if len(Q)==1:
        count+=1
    else:
        if (count+int(Q[1]))%2:
            S1=Q[2]+S1
            S2=S2+Q[2]
        else:
            S2=S2+Q[2]
            S1=Q[2]+S1
print(S2 if count%2 else S1)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
ans = 0
 
for i in range(q):
    c = list(map(str, input().split()))
    if c[0] == '1':
        ans +=1
        ans = ans&2
    if c[0] == '2':
        if (int(c[1])+ans)%2== 1 :
          s = c[2] + s
        if (int(c[1])+ans)%2== 0 :
          s = s + c[2]
 
if ans == 1:
  s = s[::-1]
 
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
l = s.split()

for i in range(q):
    c = list(map(str, input().split()))
    if c[0] == '1':
        l.reverse()
    if c[0] == '2':
        if c[1] == '1':
            l.insert(0,c[2])
        if c[1] == '2':
            l.append(c[2])

s =''.join(l)
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
l = [s]

for i in range(q):
    c = list(map(str, input().split()))
    if c[0] == '1':
        s = s[::-1]
    if c[0] == '2':
        if c[1] == '1':
            l.insert(0,c[2])
            s =''.join(l)
        if c[1] == '2':
            l.append(c[2])
            s =''.join(l)

print(s)

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

flag = False # True -> 反転
s = rr()
q = ri()
for _ in range(q):
    query = list(rs())
    if query[0] == '1':
        flag = not flag
        continue
    else:
        f = int(query[1])
        c = query[-1]
        if flag == True:
            if f == 1:
                s += c
            else:
                s = c+s
        else:
            if f == 1:
                s = c+s
            else:
                s += c
print(s)

Submission status: Wrong Answer

Solution code:
print("a")

Submission status: Wrong Answer

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

R = 0

for i in range(Q):
    Query = str(input())
    if len(Query) == 1:
        R = (R+1)%2
    else:
        T,F,C = map(str,Query.split())

        if (R + int(F) ) % 2 == 1:
            S.insert(0,C)
        else:
            S.append(C)

for i in range(len(S)):
    print(S[i],end="")

Submission status: Wrong Answer

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

cnt = 0
for _ in range(q):
    i = input().split()
    t = int(i[0])

    if t == 1:
        cnt += 1
    else:
        f = int(i[1])
        c = i[2]
        if (cnt % 2 == 0 and f == 1) or (cnt % 2 == 1 and f == 2):
            # s.insert(0, i[2])
            s.append(i[2])
        else:
            s.append(i[2])
print(''.join(s) if cnt % 2 == 0 else ''.join(s[::-1]))

Submission status: Wrong Answer

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

cnt = 0
for _ in range(q):
    i = input().split()
    t = int(i[0])

    if t == 1:
        cnt += 1
    else:
        f = int(i[1])
        c = i[2]
        if (cnt % 2 == 0 and f == 1) or (cnt % 2 == 1 and f == 2):
            s.insert(0, i[2])
        else:
            s.append(i[2])
print(''.join(s) if cnt % 2 == 0 else ''.join(s[::-1]))

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    s = input()
    q =int(input())
    ten = 0
    r = []
    l = []
    for _ in range(q):
        ls = tuple(map(str, input().split()))
        if len(ls)==1:
            ten +=1
        else:
            if ls[1] =='1':
                if ten%2 ==0:
                    l.append(ls[2])
                else:
                    r.append(ls[2])
            else:
                if ten%2 ==1:
                    l.append(ls[2])
                else:
                    r.append(ls[2])
    s = ''.join(reversed(l))+s+''.join(r)
    if ten%2 ==1:
        s = s[::-1]
    print(s)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = [input()]
q = int(input())
for _ in range(q):
    i = input().split()
    if int(i[0]) == 1:
        s = s[::-1]
    elif int(i[0]) == 2:
        if int(i[1]) == 1:
            s.insert(0, i[2])
        elif int(i[1]) == 2:
            s.append(i[2])

print(''.join(s))

Submission status: Wrong Answer

Solution code:

S=input()
Q=int(input())
top=0
for i in range(Q):
    li=input().split()
    if int(li[0])==1:
        if top==0:
            top=-1
        if top==-1:
            top=0
    elif int(li[0])==2:
        if int(li[1])==1:#後ろにくっつける
            if top==0:
                S=str(li[2])+S
            else:
                S=S+str(li[2])
        elif int(li[1])==2:#前にくっつける
            if top==0:
                S=S+str(li[2])
            else:
                S=str(li[2])+S
if top==0:
    print(S)
else:
    print(S[::top])

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
count = 0
a = []
b = []

for _ in range(q):
    Q = list(map(str, input().split()))
    if Q[0] == "1":
        count += 1
    else:
        if (int(Q[1])+count)%2 == 1:
            a.append(Q[2])
        else:
            b.append(Q[2])
        count = 0
a = "".join(a[::-1])
b = "".join(b)
s = a + s + b
if count%2 == 0 and count != 0:
    s = s[::-1]
print("".join(s))

Submission status: Wrong Answer

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

for _ in range(q):
    Q = list(map(str, input().split()))
    if Q[0] == "1":
        count += 1
    else:
        if (int(Q[1])+count)%2 == 1:
            s = Q[2] + s
        else:
            s = s + Q[2]
        count = 0

if count%2 == 0:
    s = s[::-1]
print("".join(s))

Submission status: Wrong Answer

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

for _ in range(q):
  que = input().split()
  if que[0] == "1":
    s = s[::-1]
  else:
    if que[1] == "1":
      s.insert(0, que[2])
    else:
      s.append(que[2])
print("".join(s))

Submission status: Wrong Answer

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
# -1がnotReverse, 1がReverser
isR = 1
for i in range(q):
    line = list(input().split())
    t = int(line[0])
    if t == 1:
        isR *= -1
    else:
        f = int(line[1])
        if isR + f == 0 or isR + f == 3:
            s.appendleft(line[2])
        else:
            s.append(line[2])
if isR == -1:
        s.reverse()
print("".join(s))

Submission status: Wrong Answer

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
# -1がnotReverse, 1がReverser
isR = 1
for i in range(q):
    line = list(input().split())
    t = int(line[0])
    if t == 1:
        isR *= -1
    else:
        f = int(line[1])
        if isR + f == 0 or isR + f == 3:
            s.appendleft(line[2])
        else:
            s.append(line[2])
if isR == 1:
        s.reverse()
print("".join(s))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
rev = 1
for _ in range(Q):
    query = input()
    if len(query) == 1:
        rev *= -1
    else:
        T, F, C = query.split()
        if rev == 1:
            if F == 1:
                S = C + S
            else:
                S = S + C
        else:
            if F == 1:
                S = C + S
            else:
                S = S + C

if rev == 1:
    S = S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque


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

for _ in range(Q):
    Query = input().split()
    if Query[0] == "1":
        if len(S) > 1:
            S[0], S[-1] = S[-1], S[0]
    else:
        F, C = Query[1], Query[2]
        if F == "1":
            S.appendleft(C)
        else:
            S.append(C)

print("".join(S))

Submission status: Wrong Answer

Solution code:
from collections import deque


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

for _ in range(Q):
    Query = input().split()
    if Query[0] == "1":
        S[0], S[-1] = S[-1], S[0]
    else:
        F, C = Query[1], Query[2]
        if F == "1":
            S.appendleft(C)
        else:
            S.append(C)

print("".join(S))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
reverse = 0
left = ""
right = ""
for _ in range(Q):
  q = input().split()
  if q[0]=="1":
    reverse += 1
    left, right = right, left
  if q[0]=="2":
    if q[1]=="1":
      left = q[2]+left
    if q[1]=="2":
      right = right+q[2]
if reverse%2==1:
  left = left[::-1]
  right = right[::-1]
  S = S[::-1]
print(left+S+right)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
reverse = 0
left = ""
right = ""
for _ in range(Q):
  q = input().split()
  if q[0]=="1":
    reverse += 1
    left, right = right, left
  if q[0]=="2":
    if q[1]=="1":
      left = left + q[2]
    if q[1]=="2":
      right = q[2] + right
if reverse%2==1:
  left = left[::-1]
  right = right[::-1]
  S = S[::-1]
print(left+S+right)

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
QL = []

for _ in range(Q):
    QL.append(input())

inversed = False
invc = 0

prefix = []
suffix = []

for query in reversed(QL):
    # print("qi:", query)
    if query == '1':
        #1
        inversed = not inversed
        invc += 1
    
    else:
        # print("2:inv:", inversed)
        #2
        __, f, c = query.split()

        if f == '1':
            if inversed:
                suffix.append(c)
            else:
                prefix.append(c)
        else:
            if not inversed:
                suffix.append(c)
            else:
                prefix.append(c)

# print("prefix:", prefix)
# print("suffix:", suffix)

if inversed:
    S = list(reversed(S))

print("".join(prefix) + "".join(S) + "".join(suffix))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
query = [list(input().replace(' ','')) for _ in range(q)]

r = False
for i in range(q):
    if r:
        if len(query[i]) == 1:
            r = False
        elif query[i][1] == 1:
            s = s + query[i][2]
        else:
            s = query[i][2] + s
    else:
        if len(query[i]) == 1:
            r = True
        elif query[i][1] == 1:
            s = s + query[i][1]
        else:
            s = query[i][2] + s

if r:
    print(s[::-1])
else:
    print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
reverse = 0
Tlist = []
for i in range(Q):
    Tlist.append(list(input().split()))
    if Tlist[i][0] == str(2):
        if int(Tlist[i][1])+reverse % 2 == 0:
            S.append(Tlist[i][2])
        else:
            S.appendleft(Tlist[i][2])
    else:
        if reverse == 0:
            reverse = 1
        else:
            reverse = 0
if reverse == 0:
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = input()

result = deque(S)


def input_str(line):
    r = line.split()
    if len(r) == 1:
        return int(r[0]), None, None
    else:
        return int(r[0]), int(r[1]), r[2]


for _ in range(int(input())):
    Q, F, C = input_str(input())
    if Q == 1:
        if len(result) > 1:
            top = result.popleft()
            last = result.pop()

            result.append(top)
            result.appendleft(last)

    if Q == 2:
        if F == 1:
            result.appendleft(C)
        else:
            result.append(C)

print(''.join(result))

Submission status: Wrong Answer

Solution code:
S=input()
q=int(input())
s=[]
for i in S:
    s.append(i)
t=1
for i in range(q):
    qv=list(input().split())
    if qv[0]=='1':
        t^=1
    else:
        if int(qv[1])^t==0:
            s.insert(0,qv[2])
        else:
            s.append(qv[2])
            
if t==0:
    ss=s.copy()
    s=[]
    for j in range(len(s)):
        s.append(ss[-1-j])
ans=''.join(s)
print(ans)

Submission status: Wrong Answer

Solution code:
s = str(input())
q = int(input())
for i in range(q):
    query = list(map(str, input().split()))
    query[0]=int(query[0])
    if len(query)!=1:
        query[1]=int(query[1])
    if query[0] == 1:
        if len(s) != 1:
            s = s[len(s)-1]+s[1:len(s)-1]+s[0]
    else:
        if query[1] == 1:
            s = query[2] + s
        else:
            s += query[2]

print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
q = int(input())
Q = [list(input().split()) for _ in range(q)]

ans = deque([s])
ok = True
for v in Q:
    if v[0] == "1":
        if ok: ok = False
        else: ok = True
    else:
        if v[1] == "1":
            if ok: ans.appendleft(v[2])
            else: ans.append(v[2])
        else:
            if ok: ans.append(v[2])
            else: ans.appendleft(v[2])
if not ok:
    ans = list(ans)[::-1]
print("".join(ans))

Submission status: Wrong Answer

Solution code:
from collections import deque
import numpy as np

S=deque(input())
Q=int(input())
count = 0
L=np.array([input().split() for _ in range(Q)])

for i in range(Q):
  l=L[i]
  if len(l)==1:
    count+=1
    continue
  elif len(l)==3:
    F=int(l[1])
    C=l[2]
    if count%2==0:
      if F==1:
        S.appendleft(C)
      else:
        S.append(C)
    else:
      if F==2:
        S.appendleft(C)
      else:
        S.append(C)

if count%2==1:
  S.rotate()
print(''.join(S))

Submission status: Wrong Answer

Solution code:
import numpy as np

S=[input()]
Q=int(input())
count = 0
L=np.array([input().split() for _ in range(Q)])

for i in range(Q):
  l=L[i]
  if len(l)==1:
    count+=1
    continue
  elif len(l)==3:
    T,F,C=zip(l)
    F=int(F[0])
    C=C[0]
    if count%2==0:
      if F==1:
        S.insert(0,C)
      else:
        S.append(C)
    else:
      if F==2:
        S.insert(0,C)
      else:
        S.append(C)

    
print(''.join(S[::-1]) if count%2==1 else ''.join(S))

Submission status: Wrong Answer

Solution code:
import numpy as np

S=[input()]
Q=int(input())
count = 0
L=np.array([input().split() for _ in range(Q)])

for l in L:
  if len(l)==3:
    T,F,C=zip(l)
    F=int(F[0])
    C=C[0]
    if count%2==0:
      if F==1:
        S.insert(0,C)
      else:
        S.append(C)
    else:
      if F==2:
        S.insert(0,C)
      else:
        S.append(C)
  else:
    count+=1
else:
  if count%2==1:
    S=S[::-1]
    
print(''.join(S))

Submission status: Wrong Answer

Solution code:
# D - String Formation
from collections import deque
S = input()
Q = int(input())
now = 1
tmp = [deque([]),deque([])]
for _ in range(Q):
    t = list(input().split())
    if t[0]=='1':
        now = 1+(now==1)
    else:
        if int(t[1])==now:
            tmp[0].append(t[2])
        else:
            tmp[1].append(t[2])
ans = ''
while len(tmp[0])>0:
    ans += tmp[0].pop()
if now==2:
    S = S[::-1]
ans += S
while len(tmp[1])>0:
    ans += tmp[1].popleft()
if now==2:
    ans = ans[::-1]
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
mae=""
usr=""
flg=True
for i in range(q):    
    a=list(map(str,input().split()))
    if a[0]=="1":
        if flg:
            flg=False
        elif flg==False:
            flg=True
    if a[0]=="2":
        if a[1]=="1":
            if flg:
                mae=a[2]+mae
            else:
                usr=usr+a[2]
        if a[1]=="2":
            if flg:
                usr=usr+a[2]
            else:
                mae=a[2]+mae
if flg:
    ans=mae+s+usr
else:
    ans=mae+s[::-1]+usr
print(ans)

Submission status: Wrong Answer

Solution code:
s=str(input())
s=list(s)
q=int(input())
sb=[0]*(q+1)
sa=[0]*(q+1)
sbi=0
sai=0
ta=0
for i in range(q):
  temp=str(input())
  temp=list(temp)
  if temp[0]=="1":
    ta=ta+1
  else:
    if (temp[2]=="1" and ta%2==0) or (temp[2]=="2" and ta%2!=0):
      sb[sbi]=temp[4]
      sbi=sbi+1
    else:
      sa[sai]=temp[4]
      sai=sai+1
lsb=sb.index(0)
lsa=sa.index(0)
sa.reverse()
ans=sb[0:lsb]+s+sa[len(sa)-lsa:len(sa)]
if ta%2!=0:
  ans.reverse()
print("".join(ans))

Submission status: Wrong Answer

Solution code:
s=str(input())
s=list(s)
st=["A"]
q=int(input())
t=0
ta=0
for i in range(q):
  temp=str(input())
  temp=list(temp)
  if temp[0]=="1":
    t=t+1
    ta=ta+1
  else:
    if t%2!=0:
      st.reverse()
      t=0
    if temp[2]=="1":
      st.insert(0,temp[4])
    else:
      st.append(temp[4])
if t%2!=0:
  st.reverse()
if ta%2!=0:
  s.reverse()
l=st.index("A")
ans=st[0:l]+s+st[l+1:len(st)-1]
print("".join(ans))

Submission status: Wrong Answer

Solution code:
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
S = readline().decode().rstrip()
Q = int(readline())
cnt = 0
start = ''
end = ''
for i in range(Q):
    A = list(map(str,readline().decode().split()))
    if A[0] == '1':
        cnt +=1
    else:
        if (cnt + int(A[1])) % 2:
            start += A[2]
        else:
            end += A[2]
ans = start + S + end
if cnt % 2:
    print(ans[::-1])
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
S = readline().decode().rstrip()
Q = int(readline())
cnt = 0
start = ''
end = ''
for i in range(Q):
    A = list(map(str,readline().decode().split()))
    if A[0] == '1':
        cnt +=1
    else:
        if (cnt + int(A[1])) % 2:
            end += A[2]
        else:
            start += A[2]
ans = start + S + end
if cnt % 2:
    print(ans)
else:
    print(ans[::-1])

Submission status: Wrong Answer

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

for i in range(Q):
    a = input().split()
    if a[0]=="1":
        flag = not flag

    else:
        f = a[1]
        c = a[2]
        if f=="1":
            if flag:
                que.appendleft(c)
            else:
                que.append(c)
        else:
            if flag:
                que.append(c)
            else:
                que.appendleft(c)


    
print("".join(que))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = str(input())
Q = int(input())
t = 0

d = deque()
d.append(S)

for _ in range(Q):
    A = list(input().split())
    if A[0] == "1":
        if t == 0:
            t = 1
        else:
            t = 0

    elif (A[1] == "1" and t == 0) or (A[1] == "2" and t == 1):
        d.appendleft(A[2])
    else:
        d.append(A[2])

if t == 1:
    S = S[::-1]

l = list(d)
ans = "".join(l)
print(ans)

Submission status: Wrong Answer

Solution code:
print("xy")

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
q = [list(input().split()) for _ in range(Q)] + [[0]]*2
l = []
j = 0
#print(q)

while j<Q:
    if q[j][0] == "1":
        if q[j+1][0] == "1":
            j += 2
            continue
        elif q[j+1][0] == "2":
            if q[j+2][0] == "1":
                if q[j+1] == "1":
                    a = ["2", "2", q[j+1][2]]
                else:
                    a = ["2", "1", q[j+1][2]]
                l.append(a)
                j += 3
                continue

    l.append(q[j])
    j += 1

#print(l)


for i in range(len(l)):
    if l[i][0] == "1":
        S = S[::-1]
    else:
        if l[i][1] == "1":
            S = l[i][2] + S
        else:
            S = S + l[i][2]

print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
q = int(input())
inversion_flag = False
front = deque()
back = deque()
for _ in range(q):
    inp =  list(input().split())
    if inp[0] == '1':
        inversion_flag = not inversion_flag
    else:
        if inp[1] == '1':
            if not inversion_flag:
                front.appendleft(inp[2])
            else:
                back.append(inp[2])
        else:
            if not inversion_flag:
                back.append(inp[2])
            else:
                front.appendleft(inp[2])
front_str = ''.join(front)
back_str = ''.join(back)
print(front_str + s + back_str)

Submission status: Wrong Answer

Solution code:
from collections import deque
S=input()
N=int(input())
q = deque()
A = [input().split()]
for n in range(N-1):
  A.append(input().split())
for s in S:
  q.append(s)
top = True
for n in range(N):
  if A[n][0]=="1":
    top = not top
  elif A[n][1]=="1" and top or A[n][1]=="2" and not top:
    q.appendleft(A[n][2])
  else:
    q.appendleft(A[n][2])
T = ""
while len(q)>0:
  if top:
    T+=q.popleft()
  else:
    T+=q.pop()
print(T)

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
qq = int(input())
q = []
for _ in range(qq):
  q.append(input())
d = deque()
d.append(s)
h = 1
for i in q:
  if i[0] == '1':
    h*=-1
  else:
    if (h == 1 and i[2] == '1') or (h == -1 and i[2] == '2'):
      d.appendleft(i[4])
    else:
      d.append(i[4])
p = ''
if h == -1:
  for i in reversed(d):
    p += i
else:
  for i in d:
    p += i
print(p)

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    d = deque(input().split())
    Q = int(input())
    reverse = False

    for i in range(Q):
        q = input().split()
        if q[0] == "1":
            reverse = not reverse
        else:
            if (q[1] == "1" and not reverse) or (q[1] == "2" and reverse):
                d.appendleft(q[2])
            else:
                d.append(q[2])
    
    if reverse:
        d.reverse()
    
    print("".join(d))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
n = int(input())
q = deque()
q.append(s)
Reverse = 0
for i in range(n):
    Input = input().split()
    if int(Input[0]) == 1:
        Reverse = abs(Reverse-1)
        continue
    else:
        if int(Input[1]) == 1:
            if Reverse == 0:
                q.appendleft(Input[2])
            else:
                q.append(Input[2])
        else:
            if Reverse == 0:
                q.append(Input[2])
            else:
                q.appendleft(Input[2])
if Reverse==1:
    q.reverse()
print(''.join(q))

Submission status: Wrong Answer

Solution code:
from collections import deque

s=list(input())
q=int(input())
d = deque(s)
reverse=False

for i in range(q):
  qs=list(input().split())
  if qs[0]=='1':
    reverse=not reverse
  else:
    if qs[1]=='1':
      d.appendleft(qs[2]) if reverse else d.append(qs[2])
    else:
      d.append(qs[2]) if reverse else d.appendleft(qs[2])
#  print(d)
print("".join(d))

Submission status: Wrong Answer

Solution code:
S = input().rstrip()
Q = int(input())
Que = [input().rstrip() for _ in range(Q)]

flag = 0 #0:そのまま 1:反転
for elem in Que:
  if len(elem) == 1:
    flag ^= 1
  else:
    s = int(elem[2])
    if s == 1 and flag == 0:
      S = elem[4] + S
    elif s == 2 and flag == 0:
      S += elem[4]
    elif s == 1 and flag == 1:
      S += S + elem[4]
    else:
      S = elem[4] + S

if flag == 1:
  S = S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
#D
s_list=list(input())
q=int(input())
r=0
for i in range(q):
    print(''.join(s_list))
    ope=input().split()
    if ope[0]=='1':
        r+=1
    else:
        if (int(ope[1])+r)%2 == 1:
            s_list.insert(0,ope[2])
        else:
            s_list.append(ope[2])
if r%2 !=0:
    s_list.reverse()
print(''.join(s_list))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
left = ""
right = ""
flag_swap = False
for i in range(q):
  query = list(input().split())
  if query[0] == "1":
    flag_swap = not flag_swap
  else:
    if (query[1] == "1" and not flag_swap) or (query[1] == "2" and flag_swap):
      left += query[2]
    else:
      right += query[2]

if flag_swap:
  print(right[::-1]+s+left)
else:
  print(left[::-1]+s+right)

Submission status: Wrong Answer

Solution code:
string = input()
inp = []
n = int(input())
flag = 1
for i in range(n):
    j = input().split()
    if len(j) == 3 and j[1] == "1" and flag == 1:
        inp.insert(0,j[2])
    elif len(j) == 3 and j[1] == "2" and flag == 1:
        inp.append(j[2])
    elif len(j) == 3 and j[1] == "2" and flag == -1:
        inp.insert(0,j[2])
    elif len(j) == 3 and j[1] == "1" and flag == -1:
        inp.append(j[2])
    else:
        flag *= -1
if flag == -1:
    inp = inp[::-1]
s = ""
for j in inp:
    s = s + j
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
if('A' in s and 'B' in s):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
count=0
while(q):
    q-=1
    t=list(input().split())
    if t[0]=='2':
        f=t[1]
        c=t[2]
        if f=='1':
            s=c+s
        else:
            s=s+c
    else:
        count+=1
if count%2==1:
    s=s[::-1]
print(s)

Submission status: Wrong Answer

Solution code:
S=list(input())
Q=int(input())
ls=[list(input().split()) for _ in range(Q)]
a=1
for i in range(Q):
  if ls[i][0]=="1":
    a*=(-1)
  else:
    if (ls[i][1]=="1" and a==1) or (ls[i][1]=="2" and a==-1):
      S.insert(0,ls[i][2])
    else:
      S.append(ls[i][2])
print("".join(S) if a==1 else reversed("".join(S)))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

ANS = deque(S)

rev = 0

for _ in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        rev = rev ^ 1
    else:
        f, c = int(query[1]), query[2]
        if f ^ rev:
            ANS.appendleft(c)
        else:
            ANS.append(c)

if rev:
    ANS.reverse()

print(''.join(ANS))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
s_s = []
s_e = []
sw = 0
for i in range(q):
    Q = list(map(str,input().split()))
    if int(Q[0]) == 1:
        sw += 1
    else:
        if (int(Q[1]) + sw)%2 == 0:
            s_s.append(Q[2])
        else:
            s_e.append(Q[2])
if sw%2 != 0:
    ans = s_s[::-1] + list(s) + s_e
    print("".join(ans))
else:
    ans = s_e[::-1] + list(s[::-1]) + s_s
    print("".join(ans))

Submission status: Wrong Answer

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

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = readline().decode().strip()
q = int(readline())
is_reversed = False
qs = read().decode().split("\n")
left = []
right = [] 
for i in range(q):
    query = qs[i].split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                right.append(c)
            else:
                left.append(c)
        elif f == 2:
            if is_reversed:
                left.append(c)
            else:
                right.append(c)
if is_reversed:
    s = "".join(reversed(right)) + s + "".join(left)
    print(s)
else:
    s = "".join(reversed(left)) + s + "".join(right)
    print(s)

Submission status: Wrong Answer

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

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = readline().decode().strip()
q = int(readline())
is_reversed = False
qs = read().decode().split("\n")
left = []
right = [] 
for i in range(q):
    query = qs[i].split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                right.append(c)
            else:
                left.append(c)
        elif f == 2:
            if is_reversed:
                left.append(c)
            else:
                right.append(c)
if is_reversed:
    s = "".join(reversed(right)) + s + "".join(left)
    print(s[::-1])
else:
    s = "".join(reversed(left)) + s + "".join(right)
    print(s)

Submission status: Wrong Answer

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

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = input()
q = int(input())
is_reversed = False
for i in range(q):
    query = input().split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                s += c
            else:
                s = c + s
        elif f == 2:
            if is_reversed:
                s = c + s
            else:
                s += c
# if is_reversed:
#     print(s[::-1])
# else:
print(s)

Submission status: Wrong Answer

Solution code:
S=list(input())
#print(S)
q=int(input())
x=0
appends=[[] for i in range(2)]
for i in range(q):
    s=list(input().split())
    if int(s[0])==1:
        x+=1
    else:
        appends[(x+int(s[1])+1)%2].append(s[2])
        #print(appends)
S=appends[x%2][::-1]+S+appends[(x+1)%2]
#print(S)
for k in S:
    print(k,end='')