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()
n=int(input())
str=[""]*(n*2+1)
str[n]=s
start=n-1
end=n+1
dir=1


for _ in range(n):
    h=input()
    if h[0]=="1":
        if end-start!=2:
            dir*=-1
    else:
        if h[2]=="1":
            if dir==1:
                str[start]=h[4]
                start-=1
            else:
                str[end]=h[4]
                end+=1
        else:
            if dir==1:
                str[end]=h[4]
                end+=1
            else:
                str[start]=h[4]
                start-=1
if dir==-1:
    str.reverse()
print("".join(str))

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
str=[""]*(n*2+1)
str[n]=s
start=n-1
end=n+1
dir=1

h=input()
if h[0]=="1":
    pass
else:
    if h[2]=="1":
        if dir==1:
            str[start]=h[4]
            start-=1
        else:
            str[end]=h[4]
            end+=1
    else:
        if dir==1:
            str[end]=h[4]
            end+=1
        else:
            str[start]=h[4]
            start-=1

for _ in range(n-1):
    h=input()
    if h[0]=="1":
        dir*=-1
    else:
        if h[2]=="1":
            if dir==1:
                str[start]=h[4]
                start-=1
            else:
                str[end]=h[4]
                end+=1
        else:
            if dir==1:
                str[end]=h[4]
                end+=1
            else:
                str[start]=h[4]
                start-=1
if dir==-1:
    str.reverse()
print("".join(str))

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
str=[""]*(n*2+1)
str[n]=s
start=n-1
end=n+1
dir=1

h=input()
if h[0]=="1":
    pass
else:
    if h[2]=="1":
        if dir==1:
            str[start]=h[4]
            start-=1
        else:
            str[end]=h[4]
            end+=1
    else:
        if dir==1:
            str[end]=h[4]
            end-=1
        else:
            str[start]=h[4]
            start+=1

for _ in range(n-1):
    h=input()
    if h[0]=="1":
        dir*=-1
    else:
        if h[2]=="1":
            if dir==1:
                str[start]=h[4]
                start-=1
            else:
                str[end]=h[4]
                end+=1
        else:
            if dir==1:
                str[end]=h[4]
                end+=1
            else:
                str[start]=h[4]
                start-=1
if dir==-1:
    str.reverse()
print("".join(str))

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
str=[""]*(n*2+1)
str[n]=s
start=n-1
end=n+1
dir=1

h=input()
if h[0]=="1":
    pass
else:
    if h[2]=="1":
        if dir==1:
            str[start]=h[4]
            start-=1
        else:
            str[end]=h[4]
            end+=1
    else:
        if dir==1:
            str[end]=h[4]
            end-=1
        else:
            str[start]=h[4]
            start+=1

for _ in range(n-1):
    h=input()
    if h[0]=="1":
        dir*=-1
    else:
        if h[2]=="1":
            if dir==1:
                str[start]=h[4]
                start-=1
            else:
                str[end]=h[4]
                end+=1
        else:
            if dir==1:
                str[end]=h[4]
                end-=1
            else:
                str[start]=h[4]
                start+=1
if dir==-1:
    str.reverse()
print("".join(str))

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
str=[""]*(n*2+1)
str[n]=s
start=n-1
end=n+1
dir=1

h=input()
if h[0]=="1":
    pass
else:
    if h[2]=="1":
        if dir==1:
            str[start]=h[4]
            start-=1
        else:
            str[end]=h[4]
            end+=1
    else:
        if dir==-1:
            str[start]=h[4]
            start-=1
        else:
            str[end]=h[4]
            end+=1

for _ in range(n-1):
    h=input()
    if h[0]=="1":
        dir*=-1
    else:
        if h[2]=="1":
            if dir==1:
                str[start]=h[4]
                start-=1
            else:
                str[end]=h[4]
                end+=1
        else:
            if dir==-1:
                str[start]=h[4]
                start-=1
            else:
                str[end]=h[4]
                end+=1
if dir==-1:
    str.reverse()
print("".join(str))

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
str=[""]*(n*2+3)
str[n//2+1]=s
start=n//2
end=start+2
dir=1

h=input()
if h[0]=="1":
    pass
else:
    if h[2]=="1":
        if dir==1:
            str[start]=h[4]
            start-=1
        else:
            str[end]=h[4]
            end+=1
    else:
        if dir==-1:
            str[start]=h[4]
            start-=1
        else:
            str[end]=h[4]
            end+=1

for _ in range(n-1):
    h=input()
    if h[0]=="1":
        dir*=-1
    else:
        if h[2]=="1":
            if dir==1:
                str[start]=h[4]
                start-=1
            else:
                str[end]=h[4]
                end+=1
        else:
            if dir==-1:
                str[start]=h[4]
                start-=1
            else:
                str[end]=h[4]
                end+=1
if dir==-1:
    str.reverse()
print("".join(str))

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
str=[""]*(n*2+1)
str[n]=s
start=n-1
end=start+2
dir=1

h=input()
if h[0]=="1":
    pass
else:
    if h[2]=="1":
        if dir==1:
            str[start]=h[4]
            start-=1
        else:
            str[end]=h[4]
            end+=1
    else:
        if dir==-1:
            str[start]=h[4]
            start-=1
        else:
            str[end]=h[4]
            end+=1

for _ in range(n-1):
    h=input()
    if h[0]=="1":
        dir*=-1
    else:
        if h[2]=="1":
            if dir==1:
                str[start]=h[4]
                start-=1
            else:
                str[end]=h[4]
                end+=1
        else:
            if dir==-1:
                str[start]=h[4]
                start-=1
            else:
                str[end]=h[4]
                end+=1
if dir==-1:
    str.reverse()
print("".join(str))

Submission status: Wrong Answer

Solution code:
#初期入力
import sys
input = sys.stdin.readline
S = input()
Q = int(input())
FQ_dic =tuple()
rev =0
for i in range(Q):
    FQ_dic =tuple(input().split())

    if      FQ_dic[0] =="1": #逆転する
                rev +=1
    else:                   #逆転なし   
        if      FQ_dic[1]=="1":     #後ろにつける
            if rev %2 ==0:
                    S = FQ_dic[2] + S
            else:
                    S = S +FQ_dic[2] 
        else: #                      #前につける
            if rev %2 ==0:
                    S = S + FQ_dic[2]
            else:        
                    S = FQ_dic[2] + S
if rev %2 ==0:
    print(S)
else:
    print(S[::-1])

Submission status: Wrong Answer

Solution code:
import collections
s = input()
q = int(input())
back = 1
t = collections.deque()
t.append(s)
for i in range(q):
    query = input().split()
    if query[0] == '1':
        back *= -1
    else:
        if query[1] == '1':
            if back == 1:
                t.append(query[2])
            else:
                t.appendleft(query[2])
        else:
            if back == 1:
                t.appendleft(query[2])
            else:
                t.append(query[2])
ans = ''
if back == -1:
    for i in range(len(t)):
        ans += t.popleft()
else:
    for i in range(len(t)):
        ans += t.pop()
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
T = ""
n = int(input())
for i in range(n):
    # print(s,T)
    t = list(map(str,input().split()))
    if t[0] == "1":
        s,T = T,s
    else:
        if t[1] == "1":
            s += t[2]
        else:
            T += t[2]

print(T+s[::-1])

Submission status: Wrong Answer

Solution code:
s = input()
T = ""
n = int(input())
c = 0
for i in range(n):
    # print(s,T)
    t = list(map(str,input().split()))
    if t[0] == "1":
        s,T = T,s
    else:
        if t[1] == "1":
            s += t[2]
        else:
            T += t[2]

print(s[::-1]+T)

Submission status: Wrong Answer

Solution code:

s = input()
T = ''
n = int(input())
c = 0
for i in range(n):
    # print(s,T)
    t = list(map(str,input().split()))
    if t[0] == '1':
        s,T = T,s
    else:
        if t[1] == '1':
            s += t[2]
        else:
            T += t[2]

print(s[::-1]+T)

Submission status: Wrong Answer

Solution code:

s = input()
n = int(input())
tmp  =['',s]
c = 1
for i in range(n):
    t = list(map(str,input().split()))
    if t[0] == '1':
        c+=1
    else:
        C = (c%2==0)
        
        if t[1] == '1':
            C+=1
            C%=2
            tmp[C] = tmp[C] +t[2]
        else:
            tmp[C] = tmp[C] + t[2]
    # print(tmp)
print(tmp[0][::-1]+tmp[1])

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
judge = 1
front = []
back = []
for i in range(q):
  d = input()
  if d == '1':
    judge *= -1
  else:
    a, b, c = list(d.split())
    b = int(b)
    if (b == 1 and judge ==1) or (b == 2 and judge == -1):
      front.append(c)
    else:
      back.append(c)
ans = front + s + back
if judge == 1:
  print(*ans,sep='')
else:
  print(*ans[::-1], sep='')

Submission status: Wrong Answer

Solution code:

s = input()
n = int(input())
tmp  =['','']
c = 1
for i in range(n):
    t = list(map(str,input().split()))
    if t[0] == '1':
        c+=1
    else:
        C = (c%2==0)
        
        if t[1] == '1':
            C+=1
            C%=2
            tmp[C] = tmp[C] +t[2]
        else:
            tmp[C] = tmp[C] + t[2]
    # print(tmp)
print(tmp[0][::-1]+s+tmp[1])

Submission status: Wrong Answer

Solution code:
s = input()
n = int(input())
tmp  =['','']
t = 0
for i in range(n):
    t = list(map(str,input().split()))
    if t[0] == '1':
        a = tmp[1]
        tmp[1] = tmp[0]
        tmp[0] = a

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

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
judge = 1
for i in range(q):
  d = input()
  if d == '1':
    judge *= -1
  else:
    a, b, c = list(d.split())
    b = int(b)
    if (b == 1 and judge ==1) or (b == 2 and judge == -1):
      s.insert(0, c)
    else:
      s.append(c)
print(*s,sep='')

Submission status: Wrong Answer

Solution code:
s = list(input())
Q = int(input())
a = 0
b = 0
for i in range(Q):
    t = list(input().split())
    if t[0] == "1":
        a += 1
    if t[0] == "2":
        if (a + int(t[1])) %2 ==0:
            s.append(t[2])
        else:
            s.insert(0,t[2])
if a % 2 == 1:
    ans = "".join(reversed(s))
else:
    ans = s
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
Q = int(input())
a = 0
b = 0
for i in range(Q):
    t = list(input().split())
    if t[0] == "1":
        a += 1
    if t[0] == "2":
        if a + int(t[1]) %2 ==0:
            s += str(t[2])
        else:
            s = str(t[2])+s
if a % 2 == 1:
    ans = "".join(list(reversed(s)))
else:
    ans = s
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
S=str(input())
Q=int(input())
A=np.array([])
m=0
T=""
for j in range(Q):
  L=list(map(str,input().split()))
  if L[0]=="1":
    A=np.array(A*(-1))
  else:
    S=S+L[2]
    if L[1]==1:
      A=np.append(A,-1)
    else:
      A=np.append(A,1)
for i in range(len(T)):
  if A[i]==-1:
    S=T[i]+S
  else:
    S=S+T[i]
print(S)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    q=int(input())
    inv=False
    front_inv=""
    back=""
    for i in range(q):
        que=input()
        if que[0]=='1':
            inv=not(inv)
        else:
            f=que[2]
            if (f=='1' and not(inv)) or (f=='2' and inv):
                front_inv+=que[4]
            else:
                back+=que[4]
    if inv:
        print(back[::-1]+s+front_inv)
    else:
        print(front_inv[::-1]+s+back)



if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    q=int(input())
    inv=False
    front=""
    back=""
    for i in range(q):
        que=input()
        if que[0]=="1":
            inv=not(inv)
        else:
            f=que[2]
            if (f=='1' and not(inv)) or (f=='2' and inv):
                front=que[4]+front
            else:
                back+=que[4]
    if inv:
        print(back[::-1]+s+front[::-1])
    else:
        print(front+s+back)



if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
s = input()
q = int(input())

rev = 0
for i in range(q):
    inp = list(input().split())
    if inp[0] == '1':rev = rev ^ 1
    if inp[0] == '2':
        if (inp[1] == '1' and rev == 0) or (inp[1] == '2' and rev == 1):
            s = inp[2] + s
        else:
            s = s + inp[2]

print(s if rev == 0 else s[::-1])

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
cnt1=0
s_mae=[]
s_ushiro=[]
for i in range(q):
    query=list(input().split())
    if int(query[0])==1:
        cnt1+=1
    else:
        if (int(query[1])+cnt1)%2==0:
            s_ushiro.append(query[2])
        else:
            s_mae=[query[2]]+s_mae
ans="".join(s_mae)+s+"".join(s_ushiro)
if cnt1%2==0:
    print(ans)
else:
    ans2=""
    for i in range(len(ans)):
        ans2=ans2+ans[-1-i]
        print(ans2)

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
cnt1=0
for i in range(q):
    query=list(input().split())
    if int(query[0])==1:
        cnt1+=1
    else:
        if (int(query[1])+cnt1)%2==0:
            s="".join([s,query[2]])
        else:
            s="".join([query[2],s])
if cnt1%2==0:
    print(s)
else:
    s2=""
    for i in range(len(s)):
        s2.join(s[-1-i])
    print(s2)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
num_1 = 0
tot_1 = 0
for i in range(q):
    l = list(input().split())
    if l[0] == '1':
        num_1 += 1
        tot_1 += 1
    elif l[0] == '2':
        if (num_1 + int(l[1])) % 2 == 0:
            s = s + l[2]
        else:
            s = l[2] + s
        num_1 = 0
if tot_1 % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
num_1 = 0
tot_1 = 0
for i in range(q):
    l = list(input().split())
    if l[0] == '1':
        num_1 += 1
        tot_1 += 1
    elif l[0] == '2':
        if (num_1 + int(l[1])) % 2 == 0:
            s = s + l[2]
        else:
            s = l[2] + s
        num_1 = 0
if tot_1 % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
q = int(input())
d = deque()
d.append(s)
flag = 1
for i in range(q):
    query = input().split()
    if query[0] == '1':
        flag = - flag
    elif query[0] == '2':
        if flag == 1:
            if query[1] == '1':
                d.appendleft(query[2])
            elif query[1] == '2':
                d.append(query[2])
        else:
            if query[1] == '1':
                d.append(query[2])
            elif query[1] == '2':
                d.appendleft(query[2])
if flag == -1:
    d.reverse()
ans = ''
l = len(d)
for i in range(l):
    ans += d[i]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
q = int(input())
d = deque()
d.append(s)
flag = 1
for i in range(q):
    query = input().split()
    if query[0] == '1':
        flag = - flag
    elif query[0] == '2':
        if flag == 1:
            if query[1] == '1':
                d.appendleft(query[2])
            elif query[1] == '2':
                d.append(query[2])
        else:
            if query[1] == '1':
                d.append(query[2])
            elif query[1] == '2':
                d.appendleft(query[2])
    ans = ''
    l = len(d)
for i in range(l):
    ans += d[i]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
q = int(input())
d = deque()
d.append(s)
for i in range(q):
    query = input().split()
    if query[0] == '1':
        d.reverse()
    elif query[0] == '2':
        if query[1] == '1':
            d.appendleft(query[2])
        elif query[1] == '2':
            d.append(query[2])
ans = ''
l = len(d)
for i in range(l):
    ans += d[i]
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
for x in range(200000):
    try:
        q = input().split()
        if len(q) == 1:
            S = S[::-1]

        elif len(q) == 3:
            f = int(q[1])
            c = q[2]
            if f == 1:
                S = c + S
            elif f == 2:
                S += c
    except EOFError:
        break
print(S)

Submission status: Wrong Answer

Solution code:
S = input()
for x in range(200000):
    try:
        q = input().split()
        if len(q) == 1:
            new_S = ''
            for n in list(range(len(S))):
                new_S = S[n] + new_S
            S = new_S
        elif len(q) == 3:
            f = int(q[1])
            c = q[2]
            if f == 1:
                S = c + S
            elif f == 2:
                S += c
    except EOFError:
        break
print(S)

Submission status: Wrong Answer

Solution code:
s_list = list(input())
q = int(input())
reverse = False

for _ in range(q):
	query= input().split()
	if int(query[0]) == 1:
		reverse = True
	elif int(query[0]) == 2:
		if int(query[1]) == 1:
			if reverse:
				s_list.append(query[2])
			else:
				s_list.insert(0, query[2])
		elif int(query[1]) == 2:
			if reverse:
				s_list.insert(0, query[2])
			else:	
				s_list.append(query[2])


if reverse:
	s_list.reverse()
for s in s_list:
  print(s, end ="")

Submission status: Wrong Answer

Solution code:




s_list = list(input())
q = int(input())
reverse = False

for _ in range(q):
	query= input().split()
	if int(query[0]) == 1:
		reverse = True
	elif int(query[0]) == 2:
		if int(query[1]) == 1:
			if reverse:
				s_list.append(query[2])
			else:
				s_list.insert(0, query[2])
		elif int(query[1]) == 2:
			if reverse:
				s_list.insert(0, query[2])
			else:	
				s_list.append(query[2])


if reverse:
	s_list.reverse()
for s in s_list:
  print(s, end ="")

Submission status: Wrong Answer

Solution code:

s_list = list(input())
q = int(input())
reverse = False

for _ in range(q):
	query= input().split()
	if int(query[0]) == 1:
		reverse = True
	elif int(query[0]) == 2:
		if int(query[1]) == 1:
			if reverse:
				s_list.append(query[2])
			else:
				s_list.insert(0, query[2])
		elif int(query[1]) == 2:
			if reverse:
				s_list.insert(0, query[2])
			else:	
				s_list.append(query[2])



for s in s_list:
  print(s, end ="")

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
rev_flag = False
for i in range(Q):
    query = input()
    if query == "1":
        rev_flag = not rev_flag
    else:
        if not rev_flag:
            if query[2] == "1":
                S = query[4] + S
            else:
                S = S + query[4]
        else:
            if query[2] == "2":
                S = query[4] + S
            else:
                S = S + query[4]
    #print(query)

if rev_flag:
    S = reversed(S)
print(S)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
rev_flag = False
for i in range(Q):
    query = input()
    if query == "1":
        rev_flag = not rev_flag
    else:
        if not rev_flag:
            if query[2] == 1:
                S = query[4] + S
            else:
                S = S + query[4]
        else:
            if query[2] == 2:
                S = query[4] + S
            else:
                S = S + query[4]

if not rev_flag:
    S = S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
queries = []
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        queries.append(query[0])
    if query[0] == "2":
        queries.append([query[0],query[1],query[2]])
for i in queries:
    if i == "1":#もしTiが１ならば先頭と末尾の文字を交換します
        slist = list(s)
        x = slist[0]
        slist[0] = slist[len(slist)-1]
        slist[len(slist)-1] = x
        s = "".join(slist)
    else:
        if i[1] == "1":#もしFiが１ならば先頭にi[2]を追加します
            s = i[2] + s
        if i[1] == "2":#もしFiが２ならば末尾にi[2]を追加します
            s = s + i[2]
print(s)

Submission status: Wrong Answer

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

is_reversed = False
head = ""

for i in range(Q):
    tmp_Q = input().split()

    if len(tmp_Q) == 1:
        is_reversed = not is_reversed
        
    else:
        if tmp_Q[1] == "1":
            if is_reversed:
                head = head + tmp_Q[2]
            else:
                S = S + tmp_Q[2]
        else:
            if is_reversed:
                S = S + tmp_Q[2]
            else:
                head = head + tmp_Q[2]

if is_reversed:
    print(head[::-1] + S)
else:
    print(S[::-1] + head)

Submission status: Wrong Answer

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

is_reversed = False
head = ""

for i in range(Q):
    tmp_Q = input().split()

    if len(tmp_Q) == 1:
        is_reversed = not is_reversed
        
    else:
        if tmp_Q[1] == "1":
            if is_reversed:
                head = head + tmp_Q[2]
            else:
                S = S + tmp_Q[2]
        else:
            if is_reversed:
                S = S + tmp_Q[2]
            else:
                head = head + tmp_Q[2]

S = head[::-1] + S

if is_reversed:
    print(S)
else:
    print(S[::-1])

Submission status: Wrong Answer

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

S = deque(input().strip())
Q = int(input())
flag = 0

for i in range(Q):
    q = input().split()
    if q[0] == '1':
        flag = 1 if not flag else 0
    elif q[0] == '2':
        if q[1] == '1':
            S.appendleft(q[2]) if not flag else S.append(q[2])
        elif q[1] == '2':
            S.append(q[2]) if not flag else S.appendleft(q[2])

print(''.join(S))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = lambda: sys.stdin.readline().rstrip() 
def main():
    moji=input()
    d=deque(moji)
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    d.appendleft(ci)
                else:
                    d.append(ci)
            else:
                if fi=="1":
                    d.append(ci)
                else:
                    d.appendleft(ci)
    if flont_or_back%2==0:
        print("".join(d))
    else:
        d.rotate()
        print("".join(d))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
q=[]
for _ in range(n):
  q.append(input().split())
sw=True #奇数回文字列の前後を入れ替えているときFalseになる
sl=''
sr=''
for i in range(n):
  if q[i][0]=='1':
    sw = not sw
  else:
    if q[i][1]=='1' and sw==True:
      sl += q[i][2]
    elif q[i][1]=='2' and sw==False:
      sl += q[i][2]
    else:
      sr +=q[i][2]

ans=''
if sw:
  for i in range(len(sl)):
    ans+=sl[len(sl)-1-i]
  print(ans+s+sr)
else:
  for i in range(len(sr)):
    ans+=sr[len(sr)-1-i]
  print(ans+s+sl)

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
q=[]
for _ in range(n):
  q.append(input().split())
sw=True #奇数回文字列の前後を入れ替えているときFalseになる
s0=''
for i in range(n):
  if q[i][0]=='1':
    sw = not sw
    if s0!='':
      s0+=s
      s=s0
      s0=''
  else:
    if q[i][1]=='1' and sw==True:
      s0 += q[i][2]
    elif q[i][1]=='2' and sw==False:
      s0 += q[i][2]
    else:
      s+=q[i][2]
if s0!='':
  s0+=s
  s=s0
  s0=''
if sw:
  print(s)
else:
  for i in range(len(s)):
    s0+=s[len(s)-1-i]
  print(s0)

Submission status: Wrong Answer

Solution code:

url = "https://atcoder.jp//contests/abc158/tasks/abc158_d"

def main():
    s = input()
    front_c = []
    c = int(input())
    for i in range(c):
        l = list(input().split())
        if l[0] == '1':
            s = s[::-1]
            if front_c:
                s += ''.join(front_c[::-1])
                front_c = []
        elif l[0] == '2':
            if l[1] == '1':
                front_c.append(l[2])
            else:
                s += l[2]
    if front_c:
        s = ''.join(front_c) + s
    print(s)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def main():
    moji=input()
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    flont=ci+flont
                else:
                    back+=ci
            else:
                if fi=="1":
                    back+=ci
                else:
                    flont=ci+flont
    moji=flont+moji+back
    if flont_or_back%2==0:
        print(moji)
    else:
        print(moji[::-1])
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    s = input()
    n = int(input())
    start = s
    end = ""

    for i in range(n):
        query = input().split()
        q = int(query[0])
        if q == 1:
            start,end = end,start
        else:
            f = int(query[1])
            if f == 1:
                start += query[-1]
            else:
                end += query[-1]
    print(start[::-1]+end)
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
# import math
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product                # product(iter, repeat=n)
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4


def main():
    mod = 1000000007                  # 10^9+7
    inf = float('inf')
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    rev = False
    s = input()
    dq = deque(s)
    n = ii()
    for _ in range(n):
        query = list(input().split())
        if len(query) == 1:
            rev = not rev
        else:
            _, num, char = query
            num_flag = True if '1' else False
            if (num_flag and not rev) or (not num_flag and rev):
                dq.appendleft(char)
            else:
                dq.appendleft(char)
    L = list(reversed(dq)) if rev else list(dq)
    print(''.join(L))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
S = input().split()
Q = int(input())

def pop(left, right, rev, f, c):
    left_ = left
    if rev == 1:
        if f == '1':
            #left_ = [c] + left
            left.append(c)
        else:
            right.append(c)
    else:
        if f == '1':
            right.append(c)
        else:
            #left_ = [c] + left
            left.append(c)
    return left_, right


left, right = [], []
rev = 1
for _ in range(Q):
    que = input().split()
    if que[0] == '1':
        rev *= -1
        continue
    if que[0] == '2':
        left, right = pop(left, right, rev, *que[1:])
print(''.join(left+S+right)[::rev])