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 = list(map(int,input().split()))[0]

S1=S
S2=S
flg=1
count=0
for i in range(Q):
    Query = input().split()
    if Query[0]=='1':
        flg*=-1
        count+=1
        continue
    if flg==1:
        if Query[1]=='1':
            S2+=Query[2]
        else:
            S1+=Query[2]
    else:
        if Query[1]=='1':
            S1+=Query[2]
        else:
            S2+=Query[2]
# print(S1, S2)
if count%2==1:
    print((S1[::-1])[:-len(S)]+S2)
else:
    print((S2[::-1])[:-len(S)]+S1)

Submission status: Wrong Answer

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


for i in range(Q):
  if len(q[i]) == 3:
    if q[i][1] == "1":
      S = q[i][2] + S
    else: 
      S += q[i][2]
      
  else:
    S = S[::-1]
    
  if i == Q:
    print(S)

Submission status: Wrong Answer

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

list = [s] #単語のリスト
cnt = 0 #反転する回数

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

    if int(t[0]) == 1:
        cnt += 1
    else:
        if cnt % 2 == 1:
            list = list[::-1]

        cnt = 0
        
        if int(t[1]) == 1:
            list.insert(0,t[2])
        else:
            list.append(t[2])

            
if cnt % 2 == 1:
    list = list[::-1] 

for i in range(len(list)):
    print(list[i],end='')

print()

Submission status: Wrong Answer

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

for i in range(Q):
    X = list(input().split(" "))
    if int(X[0]) == 1:
        A = S[0]
        S[0] = S[-1]
        S[-1] = A
    elif int(X[0]) == 2 and int(X[1]) == 1:
        S.insert(0, X[2])
    elif int(X[0]) == 2 and int(X[1]) == 2:
        S.append(X[2])

print(*S, sep="")

Submission status: Wrong Answer

Solution code:
import sys
import math
sys.setrecursionlimit(int(1e6))

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

bef = list()
aft = list()
rev = False
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        rev = not rev
    elif query[1] == '1':
        if not rev:
            bef.append(query[2])
        else:
            aft.append(query[2])
    else:
        if not rev:
            aft.append(query[2])
        else:
            bef.append(query[2])

if not rev:
    if len(bef) > 0:
        print(''.join(reversed(bef)), end="")

    print(S, end="")

    if len(aft) > 0:
        print(''.join(aft), end="")
else:
    if len(aft) > 0:
        print(''.join(reversed(aft)), end="")

    print(S, end="")
    
    if len(bef) > 0:
        print(''.join(bef), end="")

print("")

Submission status: Wrong Answer

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

n = 0

for i in range(Q):
  q = list(input().split())
  if int(q[0]) == 1:
    n += 1
  else:
      if int(q[1]) == 1:
        S_l.append(q[2])
        S_r = S
        n += 1
        break
      else:
        S_r.append(q[2])
        S_l = S
        n += 1
        break

  if i == Q-1:
    print(''.join(S_l[::-1]), end="")
    print(''.join(S), end="")
    print(''.join(S_r))

for i in range(Q - n):
  q = list(input().split())
  if int(q[0]) == 1:
    temp = S_r[-1]
    S_r[-1] = S_l[-1]
    S_l[-1] = temp
  else:
    if int(q[1]) == 1:
      S_l.append(q[2])
    else:
      S_r.append(q[2])

print(''.join(S_l[::-1]), end="")
print(''.join(S_r))

Submission status: Wrong Answer

Solution code:
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

S = input()
Q = int(input())
reverse = False
tail = []
head = []
for _ in range(Q):
    line = input()
    print("h", head)
    print("t", tail)
    if line == "1":
        reverse = not reverse
    else:
        T, F, C = line.split()
        T = int(T)
        F = int(F)
        if T == 2:
            if F == 1:
                if not reverse:
                    head.insert(0, C)
                else:
                    tail.append(C)
            elif F == 2:
                if not reverse:
                    tail.append(C)
                else:
                    head.insert(0, C)

if reverse:
    answer = "".join(tail)[::-1] + S[::-1] + "".join(head)[::-1]
else:
    answer = "".join(head) + S + "".join(tail)

print(answer)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
    
# for _ in range(q):
#     tmp = input()
#     if tmp == "1":
#         ans = ans[::-1]
#     else:
#         tmp = tmp.split(" ")
#         anstmp = list(ans)
#         if tmp[1] == "1":
#             anstmp.insert(0, tmp[2])
#         elif tmp[1] == "2":
#             anstmp.append(tmp[2])
#         ans = "".join(anstmp)

# print(ans)
mojis, rev = [s], [0]
for _ in range(q):
    tmp = input()
    if tmp == "1":
        rev = list(map(lambda x: x+1, rev))
    else:
        tmp = tmp.split(" ")
        if tmp[1] == "1":
            mojis.insert(0, tmp[2])
            rev.append(0)
        else:
            mojis.append(tmp[2])
            rev.append(0)
ans = []
for m, r in zip(mojis, rev):
    if r % 2 == 0:
        ans.append(m) 
    else:
        ans.insert(0, m)
print("".join(ans))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
def rev_string(original_string):
  reverse_string = ""
  for a in original_string:
    reverse_string = a + reverse_string
  return reverse_string
ans = ""
l = ""
r = ""
c = 0
for _ in range(q):
  t = input().split()
  if t[0] == '1':
    c += 1
    a = l
    l = r
    r = a
  else:
    if t[1] == '1':
      l = t[2] + l
    else:
      r += t[2]
if c%2 ==0:
  print(l+ s + r)
else:
  print(l + rev_string(s) + r)

Submission status: Wrong Answer

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

reverse = False
head = [""] * Q
tail = [""] * Q

headc = 0
tailc = 0

for i in range(Q):
    operation = input().split()
    if operation[0] == '1':
        reverse = not reverse
        continue

    if operation[1] == '1':
        if reverse:
            tail[tailc] = operation[2]
            tailc += 1
        else:
            head[Q-headc-1] = operation[2]
            headc += 1
    else:
        if reverse:
            head[Q-headc-1] = operation[2]
            headc += 1
        else:
            tail[tailc] = operation[2]
            tailc += 1

ans = "".join(head[headc-1:Q]) + S + "".join(tail[:tailc])
if reverse:
    ans = ans[::-1]

print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
n=int(input())
l = [input().split(" ") for _ in range(n)]

for i in range(n):
    if l[i][0]=='1':
        s=s[::-1]
    elif l[i][0]=='2':
        if l[i][1]==1:
            s = s+[l[i][2]]
        else:
            s.append(l[i][2])
print(''.join(s))

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
proc = [input() for i in range(q)]

for i in proc:
    if i == "1":
        s.reverse
        continue
    if i[2] == "1":
        s.insert(0,i[4])
    else:
        s.append(i[4])
print(s)

Submission status: Wrong Answer

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

cnt_reverse = 0
for i in range(q):
  query = input()
  
  if query[0] == '1':
    cnt_reverse += 1
  elif (query[2] == '1' and cnt_reverse % 2==0) or (query[2] == '1' and cnt_reverse % 2==1):
    s = query[4] + s
  else:
    s  = s + query[4]
if cnt_reverse%2==1:
  s = s[::-1]
print(s)

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
i = 0
for i in range(q):
    arr = list(input().split(' '))
    if int(arr[0]) == 2:
        f, c = int(arr[1]), arr[2]
        if f == 1:
            s.insert(0, c)
        else:
            s.append(c) 
    else:
        if len(s) == 1:
            pass
        else:
            s[0], s[-1] = s[-1], s[0]
    i += 1

text = "".join(s)
print(text)

Submission status: Wrong Answer

Solution code:
S = input().strip()
Q = int(input())
is_reverse = False
A = ""
B = ""
for i in range(Q):
  query = [j for j in input().strip().split()]
  T = int(query[0])
  if T == 1:
    is_reverse = not is_reverse
  else:
    F, C = int(query[1]), query[2]
    if F == 1:
      if not is_reverse:
        A = A + C
      else:
        B = B + C
    else:
      if not is_reverse:
        B = C + B
      else:
        A = C + A

if not is_reverse:
  print(A + S + B)
else:
  print(B + S + A)

Submission status: Wrong Answer

Solution code:
from collections import deque
def main():
    S = input().split()
    Q = int(input())
    ir = False
    ans = deque(S)
    for _ in range(Q):
        Query = input()
        if Query == '1':
            ir = not ir
        else:
            Query = Query.split()
            if (Query[1] == '1' and (not ir)) or (Query[1] == '2' and ir):
                ans.appendleft(Query[2])
            else:
                ans.append(Query[2])

    if ir:
        print(''.join(S[::-1]))
    else:
        print(''.join(S))

main()

Submission status: Wrong Answer

Solution code:
from collections import deque

S = list(input())
d = deque(S)
# print (d)

import sys
input = sys.stdin.readline

Q = int(input())
flag = True #向きの保持　Trueで順
for _ in range(Q):
    tmp = tuple(input())
    if len(tmp) == 2:
        flag = not flag
    else:
        if flag:
            if tmp[2] == '1':
                d.appendleft(tmp[4])
            else:
                d.appendleft(tmp[4])
        else:
            if tmp[2] == '2':
                d.appendleft(tmp[4])
            else:
                d.appendleft(tmp[4])

if flag:
    print (*d, sep = '')
else:
    while d:
        print (d.pop(), end = '')
    print ()

Submission status: Wrong Answer

Solution code:
def main():
    s = input()
    q = int(input())
    r = s[::-1]
    buffer_first = ""
    buffer_last = ""
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            s = buffer_first + s + buffer_last[::-1]
            r = buffer_last[::-1] + r + buffer_first
            s,r = r,s
            buffer_first = ""
            buffer_last = ""
        else:
            if query[1] == "1":
                buffer_first += query[2]
            if query[1] == "2":
                buffer_last += query[2]
    s = buffer_first + s + buffer_last
    r = buffer_last + r + buffer_first
    print(s)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
tfc=[]
for i in range(q):
  tfc.append(list(map(str,input().split())))
t1=0
front=[]
back=[]
# print(tfc[0][1])
for i in range(q):
  if tfc[i]==["1"]:
    tfc[i].append(0)
    tfc[i].append(0)
print(tfc)
for i in range(q):
  if tfc[i]=="1":
    t1+=1
  else:
    if t1%2!=0:
      if tfc[i][1]=="1":
        back.append(tfc[i][2])
      elif tfc[i][1]=="2":
        front.append(tfc[i][2])
    elif t1%2==0:
      # print(tfc[i][1])
      if tfc[i][1]=="1":
        back.append(tfc[i][2])
      elif tfc[i][1]=="2":
        front.append(tfc[i][2])
center=[]
if t1%2!=0:
  for i in range(len(s)):
    center.append(s[len(s)-i-1])
else:
  for i in range(len(s)):
    center.append(s[i])
front2=[]
for i in range(len(front)):
  front2.append(front[len(front)-i-1])
for i in range(len(front2)):
  print(front2[i], end="")
for i in range(len(center)):
  print(center[i], end="")
for i in range(len(back)):
  print(back[i], end="")
print(front2)
print(center)
print(back)

Submission status: Wrong Answer

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

for i in range(1,Q+1):
    T = input()
    if len(T) == 1:
        L = ''
        for j in range(len(S)):
            L += S[-j-1]
    else:
        if T[2] == '1':
            S = T[4] + S
        elif T[2] == '2':
            S = S + T[4]
print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque
s=str(input())
c=0
n=1
q=int(input())
for i in range(q):
    a=input().strip()
    a=list(a)
    #print(a)
    if int(a[0])==1:
        #print("DFGHJ")
        c=+1
        n+=1
    if c%2==1:
        if int(a[0])==2:
            #print(a[2])
            s=list(s)
            if int(a[2])==2:
                s=deque(s)
                #print(s)
                s.appendleft(str(a[4]))
                #print(s)
                s=list(s)
                #print("DFGH",s)
            else:
                s.append(str(a[4]))
            s=''.join(s)
            #print(s)  
            for i in range(n%2):
                s=s[::-1]
            n=0
    else:    
        if int(a[0])==2:
        #print(a[2])
            s=list(s)
            if int(a[2])==1:
                s=deque(s)
                #print(s)
                s.appendleft(str(a[4]))
                #print(s)
                s=list(s)
                #print("DFGH",s)
            else:
                
                s.append(str(a[4]))
            s=''.join(s)
            #print(s) 
            for i in range(n%2):
                s=s[::-1]
            n=0

print(s)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

S=raw_input()
Q=input()
Query=[ sys.stdin.readline().split() for _ in range(Q) ]

s1=deque()
s2=deque()
judge={}
judge["front"]="s1"
judge["end"]="s2"

cnt=0
for x in Query:
    if x[0]=="1":
        n=x[0]
        cnt+=1
    elif x[0]=="2":
        n,f,c=x[0],x[1],x[2]
    if n=="1":
        judge["front"],judge["end"]=judge["end"],judge["front"]
    elif n=="2":
        if f=="1":
            if judge["front"]=="s1":
                s1.appendleft(c)
            elif judge["front"]=="s2":
                s2.appendleft(c)
        elif f=="2":
            if judge["end"]=="s1":
                s1.append(c)
            if judge["end"]=="s2":
                s2.append(c)

str1=""
for x in s1:
    str1+=x
str2=""
for x in s2:
    str2+=x

if cnt%2==0:
    print str1[::-1]+S+str2
elif cnt%2==1:
    print str2+S[::-1]+str1[::-1]

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

def abc158d(s_init, n, query):
    ans = s_init
    reverse = 0
    head = ""
    tail = ""
    for i in range(n):
        tgt = query[i]
        if tgt[0] == "1":
            head, tail = tail, head
        elif tgt[0] == "2":
            if tgt[1] == "1":
                if reverse == 0:
                    head = head + tgt[2]
                else:
                    tail = tail + tgt[2]
            elif tgt[1] == "2":
                if reverse == 0:
                    tail = tail + tgt[2]
                else:
                    head = head + tgt[2]
            else:
                print("invalid query!!:" + str(tgt[0]) + str(tgt[1]))
        else:
            print("invalid query!!:" + str(tgt[0]))

    return head[::-1] + ans + tail

###
# main
if(__name__ == '__main__'):
    # input
    s_init = input()
    n = int(input())
    query = []
    for i in range(n):
        query.append(input().split(" "))
    
    accomplishment = abc158d(s_init, n, query)
    
    # output
    print(accomplishment)
# else:
    # do nothing

Submission status: Wrong Answer

Solution code:
s = list(input())
n = int(input())
q = [list(map(str, input().split())) for _ in range(n)]

c = 0
for i in range(n):
    if q[i][0] == "1":
        c += 1
    if q[i][0] == "2":
        if q[i][1] == "1" and c % 2 == 0 or q[i][1] == "2" and c % 2 == 1:
            s.insert(0, q[i][2])
        elif q[i][1] == "2" and c % 2 == 0 or q[i][1] == "1" and c % 2 == 1 :
            s.append(q[i][2])

if c % 2 == 1:
    s[::-1] 

print("".join(s))

Submission status: Wrong Answer

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

mae = ''
ato = ''

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

if hanten:
    print(ato + s[::-1] + mae)
else:
    print(mae + s + ato)

Submission status: Wrong Answer

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

for _ in range(Q):
  T = list(input().split())

  if T[0] == '1':
    reverse_flag = not reverse_flag

  else:
    if (T[1] == '1' and reverse_flag) or (T[1] != '2' and (not reverse_flag)):
      S = T[2]+S

    else:
      S = S+T[2]

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

Submission status: Wrong Answer

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

l_empty = []

for i in range(q):
    l_empty = []
    #t
    t=input().split()
    if t[0] == "1":
        s = s[::-1]
    elif t[1] == "1":
        l_empty.append(s)
        l_empty.append(t[2])
        s = "".join(l_empty)
    else:
        l_empty.append(t[2])
        l_empty.append(s)
        s = "".join(l_empty)
print(s)

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
rev=1
bef=[]
aft=[]

for i in range(q):
    qry=input().split()
    if qry[0] == "1":
        rev *= -1
    else:
        if qry[1] == "1":
            if rev == 1:
                bef.append(qry[2])
            else:
                aft.append(qry[2])
        else:
            if rev == 1:
                aft.append(qry[2])
            else:
                bef.append(qry[2])
ans=""
for i in range(len(bef)):
    ans += bef[-(i+1)]
        
if rev == 1:
    ans += s
else:
    s = s[::-1]
    ans += s

for i in range(len(aft)):
    ans += aft[i]
if rev == 1:
	print(ans)
else:
    print(ans[::-1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
ad=[[],[]]
add=[[],[]]
count=0
s=input()
q=int(input())
for i in range(q):
    t=input().split()
    if t[0]=='1':
        count+=1
        ad[0].reverse()
        ad[1].reverse()
        add=[add[0]+ad[1],add[1]+ad[0]]
        ad=[[],[]]
    elif t[1]=='1':
        ad[0].insert(0,t[2])
    else:
        ad[1].append(t[2])


if count%2==0:
    print(''.join(add[0]+ad[0])+s+''.join(add[1]+ad[1]))
else:
    print(''.join(add[0]+ad[0])+s[::-1]+''.join(add[1]+ad[1]))

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

def main():
    S = readline().strip().decode('utf-8')
    Q = int(readline())
    Query = [tuple(readline().decode('utf-8').split()) for _ in range(Q)]
    rev_flg = 0
    for q in Query:
        if q[0] == '1':
            rev_flg += 1
        else:
            if int(q[1])-(rev_flg%2) == 1:
                S = q[2] + S
            else:
                S += q[2]
    print(rev_flg)
    if rev_flg%2 == 1:
        S = S[::-1]
    print(S)

    return
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8

a = input().split()
q = int(input())

for _ in range(q):
    q_list = input().split()

    try:
        if q_list[1] == '1':
            a.insert(0, q_list[2])
        else:
            a.append(q_list[2])
    except:
        a = a[::-1]
result = ''.join(a)
print(result)

Submission status: Wrong Answer

Solution code:
def main():
    s = input()
    q = int(input())
    r = s[::-1]
    buffer_first = ""
    buffer_last = ""
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            s = buffer_first[::-1] + s + buffer_last
            r = buffer_last + r + buffer_first[::-1]
            s,r = r,s
            buffer_first = ""
            buffer_last = ""
        else:
            if query[1] == "1":
                buffer_first += query[2]
            if query[1] == "2":
                buffer_last += query[2]
    s = buffer_first + s + buffer_last
    r = buffer_last + r + buffer_first
    print(s)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S =input()
Q = int(input())
T = [0]*Q
for i in range(0,Q):
  T[i] = list(map(str, input().split()))

check = 0
for i in reversed(range(0,Q)):
  if(T[i][0] != "1"):
    check = i
    break

  
  
nS = ""
for i in range(0,check):
  if(i != Q-1 and T[i][0] == "1" and T[i+1][0] == "1"):
    T[i][0] = 0
    T[i+1][0] = 0
  elif(T[i][0] == "2" and T[i][1] == "1"):
      S = T[i][2]+S
  elif(T[i][0] == "2" and T[i][1] =="2"):
      S = S+T[i][2]
  elif(T[i][0] == "1"):
    S = S[::-1]
  elif(T[i][0] == 0):
    pass

for i in range(check, Q):
  if(i != Q-1 and T[i][0] == "1" and T[i+1][0] == "1"):
    T[i][0] = 0
    T[i+1][0] = 0
  elif(T[i][0] == "1"):
    S = S[::-1]
  elif(T[i][0] == 0):
    pass


print(S)

Submission status: Wrong Answer

Solution code:
raw_input = input()
S = raw_input.split()


Q_n = int(input())
Q=[]

for x in range(Q_n):
    x = input()
    tmp = x.split()
    q = tmp[0]

    if q == "1":
        S_len = len(S)
        if len(S) > 1:
            S_dash = S_len - 1
            tmp_2 = S[S_dash]
            S[S_dash]=S[0]
            S[0]=tmp_2
        else:
            S = S
        
    else:
        f = tmp[1]
        c = tmp[2]
        if f == "1":
            S.insert(0,c)
        else:
            S.append(c)

str = ''.join(S)
print(str)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
hanten = 0
check = 0
for i in range(q):
    l = list(map(str, input().split()))
    if l[0] == "1":
        hanten += 1
        check += 1
    else:
        if l[1] == "1":
            if check%2 == 1:
                s = l[2] + s
            else:
                s = s + l[2]
        else:
            if check%2 == 1:
                s = s + l[2]
            else:
                s = l[2] + s

if hanten%2 == 0:
    s = s[::-1]

print(s)

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
flg = 0
s1 = s
s2 = s
 
for i in range(q):
    tmp = input()
    if tmp == '1':
        if flg == 0:
            flg = 1
        else:
            flg = 0
    else:
        t1,t2,t3 = tmp.split()
        if int(t2) == 1:
            if flg == 0:
                s1 = t3 + s1
                s2 = s2 + t3
            else:
                s1 = s1 + t3
                s2 = t3 + s2
        if int(t2) == 2:
            if flg == 0:
                s1 = s1 + t3
                s2 = t3 + s2
            else:
                s1 = t3 + s1
                s2 = s2 + t3
 
if flg == 1:
    print(s2)
else:
    print(s1)

Submission status: Wrong Answer

Solution code:
s = input()
Q = int(input())
query = list(list(input().split()) for _ in range(Q))
mode = 1
x,y = '',''

for q in query:
    if len(q)==1:
        mode *= -1
    else:
        f,c = q[1], q[2]
        if (mode>0 and f=='1'):
            x += c
        elif (mode>0 and f=='2'):
            y += c
        elif (mode<0 and f=='2'):
            x = c+x
        else:
            y = c+y
res = x+s+y
print(res if mode>0 else res[::-1])

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

s = deque([sys.stdin.readline().rstrip()])
q = int(input())
cnt = 0
for i in range(q):
    query = sys.stdin.readline().split()
    if query[0] == '1':
        cnt = (cnt + 1)%2
    else:
        if (int(query[1]) + cnt)%2 == 1:
            s.appendleft(query[2])
        else:
            s.append(query[2])

if cnt:
    s.reverse()
print("".join(s))

Submission status: Wrong Answer

Solution code:
from collections import deque
import copy
S = list(input())
S = deque(S)
Q = int(input())
qlis = []
cnt = 0
for _ in range(Q):
    qlis.append(input().split())

for i in range(Q):
    if len(S) >= 3:
        break
    if len(qlis[i]) == 1:
        S.reverse()
    else:
        if int(qlis[i][1]) == 2:
            S.append(qlis[i][2])
        else:
            S.appendleft(qlis[i][2])

    cnt += 1

SS = copy.deepcopy(S)
saigo = SS.pop()
saisyo = SS.popleft()
kotei = SS
reverse_kotei = copy.deepcopy(kotei)
reverse_kotei.rotate(1)
appendlist = [saisyo,saigo]
append_rev = [0,0]
mae_usiro = [0,1]
cnt_rev = 0

for i in range(cnt,Q):
    if int(qlis[i][0]) == 1:
        cnt_rev += 1
    else:
        if int(qlis[i][1]) == 2:
            mae_usiro.append(1)
            appendlist.append(qlis[i][2])
            append_rev.append(cnt_rev)
        else:
            mae_usiro.append(0)
            appendlist.append(qlis[i][2])
            append_rev.append(cnt_rev)

if cnt_rev % 2 == 1:
    kotei = reverse_kotei

if kotei:
    kotei = deque(kotei)



for j in range(len(mae_usiro)):
    if (cnt_rev - append_rev[j] + mae_usiro[j]) %2 == 1:
        kotei.append(appendlist[j])
    else:
        kotei.appendleft(appendlist[j])
if kotei:
    kotei = list(kotei)
    answer = "".join(kotei)
    print(answer)
else:
    answer = "".join(S)
    print(answer)

Submission status: Wrong Answer

Solution code:
#coding:utf-8

import queue



class Node:

    # ノードは前方向、後方向へのリンクを持つ。
    def __init__(self, data, next = None, previous = None):
        self.data = data
        self.next = next
        self.previous = previous
    # データを設定する。
    def set_data(self, data):
        self.data = data
    
    # データを取得する。
    def get_data(self):
        return self.data
    
    # 次のノードを設定する。
    def set_next(self, next):
        self.next = next
    
    # 次のノードを取得する。
    def get_next(self):
        return self.next
    # 前のノードを設定する。
    def set_previous(self, previous):
        self.previous = previous
    
    # 前のノードを取得する。
    def get_previous(self):
        return self.previous
    
    
class DoublyLinkedList():

    def __init__(self):
        self.head = None
        self.tail = None
        
    def print_doubly_linked_list(self):
        temp = self.head
        while(temp is not None):
            print(temp.data, end='')
            temp = temp.get_next()
    def print_doubly_linked_list_reverse(self):
        temp = self.tail
        while(temp is not None):
            print(temp.data, end='')
            temp = temp.get_previous()

 # 先頭に挿入する
    def insert_at_start(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            self.tail = new_node
        else:
            new_node.set_previous(None)
            new_node.set_next(self.head)
            self.head.set_previous(new_node)
            self.head = new_node
            
                # 最後に挿入する
    def insert_at_end(self, data):
        if self.head is None:
            self.head = Node(data)
            self.tail = self.head
        else:
            temp = self.head
            while temp.get_next() is not None:
                temp = temp.get_next()
            temp.set_next(Node(data, None, temp))
            self.tail = temp.get_next()
            
            
            
def main():
    s = input()
    q = int(input())
    rev = False
    
    d = DoublyLinkedList()
    d.insert_at_start(s)
    for _ in range(q):
        a = input()
        if a == '1':
            #arr = arr[::-1]
            rev = rev != True
        else:
            tmp, f, c = a.split(' ')
            if f == '1':
                if rev == True:
                    d.insert_at_end(c)
                else:
                    d.insert_at_start(c)
            elif f == '2':
                if rev == True:
                    d.insert_at_start(c)
                else:
                    d.insert_at_end(c)
    if rev == True:
        d.print_doubly_linked_list_reverse()
    else:
        d.print_doubly_linked_list()
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
x = 1
for i in range(Q):
    a, *b = input().split()
    if a == "1":
        x *= -1
    elif a == "2":
        c, d = b
        if c == "2":
            x *= -1 
        if x == 1:
            S.insert(0, d)
        elif x == -1:
            S.append(d)
if x == 1:
    S = S[::-1]
print(*S, sep="")

Submission status: Wrong Answer

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

for _ in range(Q):
  T = list(input().split())

  if T[0] == '1':
    reverse_flag = not reverse_flag

  else:
    if (T[1] == '1' and reverse_flag) or (T[1] != '1' and (not reverse_flag)):
      S = T[2]+S

    else:
      S = S+T[2]

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

Submission status: Wrong Answer

Solution code:
s = input()
q_number = int(input())
 
queries = [input().split() for dummy in range(q_number)]
flag = 1
count = 0
for i in range(q_number):
  if queries[i][0] == "1": 
    flag = flag * -1
    count += 1
  else:
    if int(queries[i][1])*flag == 1 or -2:
      s = queries[i][2] + s
    else:
      s = s + queries[i][2]      

      
if count % 2 == 1:
  s = s[::-1]

print(s)

Submission status: Wrong Answer

Solution code:
# 入力文字列
get_str = list(input())

# 操作回数
N = int(input())

# リバースフラグ
r_flag = 0

# 操作
for i in range(N):
    # 取得操作
    inp_cmd = list(input().split())
    # inp_cmd[0] = 1 ならリバースフラグ更新
    if(inp_cmd[0] == '1'):
        r_flag = (r_flag+1) % 2
    else:
        # inp_cmd[1]=1、かつリバースフラグなし
        # inp_cmd[1]=2、かつリバースフラグあり
        # 先頭に文字追加
        if((inp_cmd[1] == '1' and r_flag == 0) or (inp_cmd[1] == '2' and r_flag == 1)):
            #get_str = inp_cmd[2] + get_str
            get_str.insert(0, inp_cmd[2])
        # 上記以外は後ろに追加
        else:
            get_str.insert(-1, inp_cmd[2])
# リバースフラグあり
if(r_flag == 1):
    get_str = get_str[::-1]

out_str = ''.join(get_str)
print(out_str)