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 = str(input())
n = int(input())
num = [str(input()).split() for _ in range(n)]

count=0
ans = [s]
for i in num:
    if int(i[0]) == 1:
        ans=ans[count+1:]+[s]+ans[:count]
        count=len(ans[count:])-1

    else:
        if int(i[1]) == 1:
            count=+1
            ans[0:0]=[i[2]]
        else:
            ans.append(i[2])
print("".join(ans))

Submission status: Wrong Answer

Solution code:
def main():

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

    q_list = [0]*Q
    for i in range(Q):
        q_list[i] = (input().split())

    for q in q_list:
        if len(q) == 1:
            t = q[0]
        else:
            t, f, c = q

        if t == '1' or t == 1:
            S.reverse()
        else:
            if f == '1' or f == 1:
                S.insert(0, c)
            else:
                S.append(c)
    print("".join(S))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0
chs = ""
flip = False
ins = 0
while idx < q:
    if t[idx] == 1:
        while idx + 1 < q and t[idx + 1] == 1:
            idx += 1
            flip = not flip
        if flip:
            chs = chs[::-1]
            ins = len(chs) - ins
    else:
        if f[idx] == 1:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 1:
                idx += 1
                ch = c[idx] + ch
            chs = ch + chs
            ins += len(ch)
        else:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 2:
                idx += 1
                ch = ch + c[idx]
            chs = chs + ch
    idx += 1

ans = chs[:ins]
ans += s[::-1] if flip else s
ans += chs[ins:]
print(ans)

Submission status: Wrong Answer

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


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

for i in range(q):
    query = list(input().split())
    if len(query) == 1:
        s = ''.join(list(reversed(s)))
    elif query[1] == '1':
        s = query[2] + s
    elif query[1] == '2':
        s = s + query[2]
        
print(s)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import collections
#input = sys.stdin.buffer.readline

def inn(func=int):
    return list(map(func,input().split()))

def ins():
    return input().split()

s = input()
q = int(input())
d = collections.deque()
d.append(s)
r = False
for i in range(q):
    que = input().split()
    if que[0] == "1":
        if r:
            r = False
        else:
            r = True
    else:
        if que[1] == "1":
            if not r:
                d.appendleft(que[2])
            else:
                d.append(que[2])
        else:
            if not r:
                d.append(que[2])
            else:
                d.appendleft(que[2])

if r:
    d.reverse()
print("".join(list(d)))

Submission status: Wrong Answer

Solution code:
from collections import deque
s = deque(input())
q = int(input())
r = 0
z = 0
to = 0
for i in range(q):
    
    try:
        t,f,c = input().split()
        if (r + int(f)) % 2 == 1:
            s.appendleft(c)
    
        else:
            s.append(c)
        r = 0
        z = 1
    except:
        if z == 0:
            pass
        else:
            r += 1
        z = 1
        to += 1
if to % 2 == 1:
    s = reversed(s)
print("".join(s))

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 rev:
            bef.append(query[2])
        else:
            aft.append(query[2])
    else:
        if rev:
            aft.append(query[2])
        else:
            bef.append(query[2])

if 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:
import numpy as np 

S = str(input())

Q = int(input())

q = np.ones(Q,dtype = object)


flag=0
fla=0
for i in range(Q):
    q[i] = [i for i in input().split()]
    if q[i][0]=='1':
        fla+=1
fla=fla%2+1

for i in range(Q):
    if q[i][0] == '1':
        flag+=1
    else:
        
        if q[i][1] == '1':
            if flag%2==0:
                if fla==0:
                    S = S+str(q[i][2])
                else:
                    S = str(q[i][2])+S
            else:
                if fla==0:
                    S = str(q[i][2])+S
                else:
                    S = S+str(q[i][2])
        elif q[i][1] == '2':
            if flag%2==1:
                if fla==0:
                    S = S+str(q[i][2])
                else:
                    S = str(q[i][2])+S
            else:
                if fla==0:
                    S = str(q[i][2])+S
                else:
                    S = S+str(q[i][2])
print(S)

Submission status: Wrong Answer

Solution code:
s = input()
Q = int(input())
count = 0
right = []
left = []
for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        count += 1
    else:
        if count%2 == 0:
            if int(q[1]) == 1:
                left.append(q[2])
            else:
                right.append(q[2])
        else:
            if int(q[1]) == 1:
                right.append(q[2])
            else:
                left.append(q[2])

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

Submission status: Wrong Answer

Solution code:
S=str(input())
Q=int(input())
for i in range(Q):
    T=str(input().split())
    if T[0]=="1":
        S.reverse()
    elif T[0]=="2":
        if T[1]=="1":
            S=T[2:]+S
        elif T[1]=="2":
            S=S+T[2:]
print(S)

Submission status: Wrong Answer

Solution code:
import math

s = input()
q = int(input())
reverse_flag = 0
head = ""
tail = ""

for i in range(q):
  query = input().split(" ")
  if(len(query) == 1):
    reverse_flag += 1
  else:
    if((reverse_flag + int(query[1]) % 2 == 0)):
      tail += query[2]
    else:
      head = query[2] + head

ans_str = head + s + tail

if(reverse_flag % 2 == 1): print(ans_str[::-1])
else: print(ans_str)

Submission status: Wrong Answer

Solution code:
s = input()

q = int(input())
r = 0
b = ""
c = ""
for i in range(q):
    A = list(input().split())
    if A[0] == '1':
        r += 1
    else:
        if (A[1] == '1' and r % 2 == 0) or (A[1] != '1' and r % 2 != 0):
            s = A[2] + s
        else:
            s = s+ A[2]
if r % 2 == 0:
    print(s)
else:
    s = reversed(s)
    print(s)

Submission status: Wrong Answer

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

S = input()
Q = int(input())
Flag = True
 
for i in range(Q):
    Query = input()
    if Query[0] == "1":
        Flag = not Flag
    else:
        QueryList = Query.split()
        if Flag:
            if QueryList[1] =="1":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]
        else:
            if QueryList[1] =="2":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]

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

Submission status: Wrong Answer

Solution code:
S = str(input())
Q = int(input())
Qu = []

for i in range(Q):
    x = list(map(str, input().split()))
    if x == ['1']:
        Qu.append([1,1,1])
    else:
        Qu.append(x)
count = []
C = 1
for i in range(Q):
    if Qu[i][0] == 1:
        C += 1
        count.append('m')
        map(lambda x: x+1, count)
    else:
        if Qu[i][1] == 1:
            count.append(1)
        else:
            count.append(2)
if C % 2 == 0:
    S = S[::-1]

for i in range(Q):
    if count[i] == 'm':
        continue
    elif count[i] % 2 == 1:
        S = S + str(Qu[i][2])
    elif count[i] % 2 == 0:
        S = str(Qu[i][2]) + S

print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    S = deque(input().split())
    Q = int(input())

    for i in range(Q):
        QList = deque(input().split())
    
        if QList[0] == '1':
            continue
            #S.reverse()
        else:
            if QList[1] == '1':
                S.appendleft(QList[2])
            else:
                S.append(QList[2])
    print("".join(list(S)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

for _ in range(q):
    a = list(input().split())
    if len(a) == 1:
        s.reverse()
    else:
        if a[1] == "1":
            s.reverse()
            s += a[-1]
            s.reverse()
        else:
            s += a[-1]
print("".join(s))

Submission status: Wrong Answer

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

right=[]
left=[]
flag=0

for i in range(q):
    a = input()
    if int(a[0]) == 1:
        if flag == 0:
            flag=1
        else:
            flag=0
    else:
        if int(a[2]) == 1:
            if flag == 0:
                left.insert(0,a[4])
            else:
                right.append(a[4])
        elif int(a[2]) ==2:
            if flag ==0:
                right.append(a[4])
            else:
                left.insert(0,a[4])

right_result="".join(right)
left_result="".join(left)

print("結果"+str(flag))
if flag==0:
    print(str(left_result+s+right_result))
else:
    print(str(right_result[::-1]+s[::-1]+left_result[::-1]))

Submission status: Wrong Answer

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------


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

r=0

a=""
z=""

for i in range(q):
	t = input()
	
	if t=='1':
		if r==0:
			r=1
		else:
			r=0
#		p("t,r,s",t,r,s)
		continue
	
	_,f,c = t.split()

	if f=='1':
		if r==0: a = a + c
		if r==1: z = z + c
	else:
		if r==0: z = z + c
		if r==1: a = a + c


#	p("t,r,_,f,c,x,s","[",t,"]",r,_,f,c,x,s)

ans=""
if (r==0):
	ans = a[::-1] + s + z
else:
	ans = z[::-1] + s + a

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()

q = int(input())
r = 0
b = ""
c = ""
for i in range(q):
    A = list(input().split())
    if A[0] == '1':
        r += 1
    else:
        if (A[1] == '1' and r % 2 == 0) or (A[1] != '1' and r % 2 != 0):
            b = A[2] + b
        else:
            c = c + A[2]
s = b+s+c
if r % 2 == 0:
    print(s)
else:
    s = reversed(s)
    print(s)

Submission status: Wrong Answer

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

S = input().strip()
Q = int(input().strip())
Q_list = [input().strip() for _ in range(Q)]
#-----

# order = 1:  左→右
# order =-1:  左←右
order=1

dq = deque()
dq.append(S)

for line in Q_list:
    if line == "1":
        order *= -1
    else:
        t,f,c = line.split()
        
        if order == 1 and f == "1":
            dq.appendleft(c)
        elif order == 1 and f == "2":
            dq.append(c)
        elif order == -1 and f == "1":
            dq.append(c)
        elif order == -1 and f == "2":
            dq.appendleft(c)

if order == 1:
    print( *list(dq), sep="" )
else:
    print( *list(reversed(dq)), sep="" )

Submission status: Wrong Answer

Solution code:
true_letter = input()
print(true_letter)
Q= int(input())
letter = ['*']
reversed = False
for i in range(Q):
    query = input().split()
    if len(query)==1:
        reversed = not bool(reversed)
    else:
        if int(query[1])==1: #まえに追加
            if reversed == False:
                letter.insert(0,query[2])
            else:
                letter.append(query[2])
        else:
            if reversed == False:
                letter.append(query[2])
            else:
                letter.insert(0,query[2])
if reversed==True:
    letter.reverse()

output = ''.join(letter)
output = output.replace('*',true_letter)
print(output)

Submission status: Wrong Answer

Solution code:
from collections import deque

S  = deque()
S.append(input().strip())

N = int(input().strip())

asc = True
for _ in range(N):
    ope = input().strip()
    if ope[0] == "1":
        asc = not asc
        continue
    i, s, c = ope.split(" ")
    if s == "1":
        if asc:
            S.appendleft(c)
        else:
            S.append(c)
    else:
        if asc:
            S.append(c)
        else:
            S.appendleft(c)
if not asc:
    S.reverse()
print(''.join(S))

Submission status: Wrong Answer

Solution code:
import sys

S = input().split()
Q = int(input())
QUERY = sys.stdin.readlines()

state = 0
q1 = []
q2 = []
for i in range(Q):
    query = QUERY[i]
    if query[0] == '1':
        state ^= 1
    else:
        _, F, C = query.split()
        if (state + int(F)) % 2:
            q1.append(C)
        else:
            q2.append(C)

ans = []
if state == 0:
    if len(q1):
        print(*reversed(q1), end='', sep='')
    print(*S, end='', sep='')
    if len(q2):
        print(*q2, sep='', end='')
    print()
else:
    if len(q2):
        print(*reversed(q2), end='', sep='')
    print(*reversed(S), end='', sep='')
    if len(q1):
        print(*q1, end='', sep='')
    print()

Submission status: Wrong Answer

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

from collections import deque
count = 0
ans = 0
 
S = list(S)
S = deque(S)
 
for i in range(Q):
    if Que[i][0] == '1':
        #S.reverse()
        count += 1
        ans += 1
    elif Que[i][0] == '2':
        if Que[i][1] == '1' and count%2 == 0:
            S.appendleft(Que[i][2])
            count = 0
        elif Que[i][1] == '1' and count%2 == 1:
            S.append(Que[i][2])
            count = 0
        elif Que[i][1] == '2' and count%2 == 0:
            S.append(Que[i][2])
            count = 0
        elif Que[i][1] == '2' and count%2 == 1:
            S.appendleft(Que[i][2])
            count = 0

if ans%2 == 0:
    print("".join(S))
else:
    S.reverse()
    print("".join(S))

Submission status: Wrong Answer

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

query_list = [input().split() for _ in range(Q)]

T_cnt_list = collections.deque([0])
left = ''
right = ''

for i, query in enumerate(query_list):
    if query[0] == '1':
        T_cnt_list.append(1+T_cnt_list[-1])

    else:
        T_cnt_list.append(T_cnt_list[-1])

        if query[1] == '1' and T_cnt_list[i+1]%2 == 0:
            left = query[2] + left

        elif query[1] == '1' and T_cnt_list[i+1]%2 == 1:
            right = right + query[2]

        elif query[1] == '2' and T_cnt_list[i+1]%2 == 0:
            right = right + query[2]

        elif query[1] == '2' and T_cnt_list[i+1]%2 == 1:
            left = query[2] + left

if T_cnt_list[-1]%2 == 0:
    print(left + S + right)
else:
    print(right + S[::-1] + left[::-1])

Submission status: Wrong Answer

Solution code:

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

# print(S, Q)

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

from collections import deque
slist = deque()
slist.append(S)
isReversed = False
for query in queries:
    if query == "1":
        isReversed = not isReversed
        # print("reverse", S)
        # S = S[::-1]
    else:
        t, f, c = query.split()
        # print(t, f, c)
        d = f=="1"
        if isReversed: d = not d
        if d:
            # S = "".join([c, S])
            slist.appendleft(c)
        else:
            # print("append c to tail of ", S)
            # S = S + c
            # S = "".join([S, c])
            slist.append(c)
slist = list(slist)
if isReversed:
    slist = slist[::-1]

print("".join(slist))
# print(S)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

for _ in range(q):
    q_list = input().split()
    if q_list[0] == '1':
        a = a[::-1]
    else:
        if q_list[1] == '1':
            a.insert(0, q_list[2])
        else:
            a.append(q_list[2])
result = ''.join(a)
print(result)

Submission status: Wrong Answer

Solution code:

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

while(q > 0):
    a=input().split()
    if len(a)&1:
        s = s[::-1]
    else:
        if int(a[1])&1:
            s = a[2] + s
        else:
            s = s + a[2]
    q-=1
print(s)

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
flag = True
front = []
back = []
for i in range(Q):
    q = list(input().split())
    # print(i,flag)
    if q[0] == "1":
        flag = not flag
    else:
        if flag:
            if q[1] == "1":
                front.insert(0,q[2])
            else:
                back.append(q[2])
        else:
            if q[1] == "1":
                back.append(q[2])
            else:
                front.insert(0,q[2])
if not flag:
    S.reverse()
S = "".join(S)
front = "".join(front)
back = "".join(back)

if not flag:
    a = "".join(list(reversed(front+S+back)))
    print(a)
else:
    print(front+S+back)

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
is_reversed = False
append_first = []
my_first = append_first.append
append_end = []
my_end = append_end.append

for i in range(Q):
  q = list(input())
  if q[0] == '1':
    is_reversed = True if is_reversed == False else False
  else:
    if q[2] == '1' and is_reversed:
      append_end.append(q[4])
    elif q[2] == '1': 
      append_first.append(q[4])
    elif is_reversed:
      append_first.append(q[4])   
    else:
      append_end.append(q[4])
      
append_first.extend(S) 
append_first.extend(append_end)

if is_reversed:
  append_first.reverse()
print(''.join(append_first))

Submission status: Wrong Answer

Solution code:
class Node_s():
  def __init__(self, ch, next=None, prev=None):
    self.data = ch
    self.next = next
    self.prev = prev

a = input()
qn = int(input())

head = Node_s(a)
current = head
last = head
dir = 1

for q in range(qn):
  q = input()
  if q[0]=="1":
    dir = 1-dir
    #tmp = head
    #head = last
    #last = tmp
  else:
    d, f, c = q.split()
    if f=="1":
      if dir==1:
        head.prev = Node_s(c)
        head.prev.next = head
        head = head.prev
      else:
        last.next = Node_s(c)
        last.next.prev = last
        last = last.next
    else:
      if dir==1:
        last.next = Node_s(c)
        last.next.prev = last
        last = last.next
      else:
        head.prev = Node_s(c)
        head.prev.next = head
        head = head.prev

ans = []

while(True):
  ans.append(head.data)
  if head.next==None:
    break
  head = head.next

  
if dir==0:
  ans = reversed(ans)

print(''.join(ans))

Submission status: Wrong Answer

Solution code:
S = input()
Q = input()
flag = -1
for i in range(int(Q)):
    TFC = input().split()
    if TFC[0] == "1":
        flag *= -1
    else:
        if TFC[1] == "1":
            if flag == -1:
                S = '' . join([S, TFC[2]])
            else:
                S = '' . join([TFC[2], S])
        else:
            if flag == -1:
                S = '' . join([TFC[2], S])
            else:
                S = '' . join([S, TFC[2]])
if flag == -1:
    S = S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():

    input = stdin.readline

    S = input()[:-1]
    Q = int(input())
    
    ans_list = [S]

    for _ in range(Q):
        query = input().split()
        if(query[0] == '1'):
            ans_list = ans_list[::-1]
        else:
            if(query[1] == '1'):
                ans_list.insert(0,query[2])
            else:
                ans_list.append(query[2])
    
    print(''.join(ans_list))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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



for i in range(Q):
    command = list(input().split())
    if command[0] == "1":
        S[0], S[-1] = S[-1], S[0]
    elif command[0] == "2":
        if command[1] == "1":
            S.insert(0, command[2])
        elif command[1] == "2":
            S.append(command[2])
    #print(i+1, S)
print("".join(S))

Submission status: Wrong Answer

Solution code:
# input1 = list(map(int, (input().split())))

S = input()
N = int(input())
c = False
for i in range(N):
    Q = list(input().split())
    a = int(Q[0])
    if a == 1:
        c = not c
    elif a == 2:
        if c == True:
            S = S[::-1]
        b = int(Q[1])
        if b == 1:
            S = Q[2] + S
        elif b == 2:
            S = S + Q[2]
if c == True:
    S = S[::-1]
print(S)

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]
    if rev_flg%2 == 1:
        S = S[::-1]
    print(S)

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

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]

if count%2==1:
    print((S1[::-1])[:-1]+S2)
else:
    print((S2[::-1])[:-1]+S1)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

conr = 0

for i in range(Q):
  q = list(input().split())
  if q[0] == "1":
    conr += 1
  else:
    s = q[-1]
    f = q[1]
    if f == "1" or conr % 2 == 1:
      D.appendleft(s)
      C.append(s)
    elif f == "2" or conr % 2 == 0:
      D.append(s)
      C.appendleft(s)


if conr % 2 == 0:
  ans = "".join(D)
else:
  ans = "".join(C)

print(ans)

Submission status: Wrong Answer

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

    for i in range(Q):
        # print(S)
        q = list(input().split())

        if q[0] == '1':
            S = S[-1] + S[1:-1] + S[0]
        elif q[0] == '2':
            if q[1] == '1':
                S = q[2] + S
            elif q[1] == '2':
                S = S + q[2]
            else:
                raise ValueError(
                    'F({}) is {}, which is invalid!'.format(i, q[1]))
        else:
            raise ValueError('T({}) is {}, which is invalid!'.format(i, q[0]))

    print(''.join(S))


##########
d()

Submission status: Wrong Answer

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

r = 0
sort = 0
for i in range(q):
  List=list(input().split())
  if len(List) > 1 :
    if List[1] == '1' and r == 0 :
      s = List[2] + s 
    elif List[1] == '1' and r == 1 :
      s = s + List[2] 
    elif List[1] == '2' and r == 0 :
      s = s + List[2] 
    elif List[1] == '2' and r == 1 :
      s = List[2] + s 
    r = 0
  else:
    sort += 1
    if r == 1 :
      r=0
    else:
      r=1



ans = ''
if sort % 2 ==1:
  for j in range(len(s)):
    ans += s[len(s)-j-1:len(s)-j]
else:
  ans = s

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque


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

ans = deque()
ans.append(s)

for i in op:
    if i[0] == "1":
        ans.reverse()
    if i[0] == "2":
        if i[1] == "1":
            ans.appendleft(i[2])
        elif i[1] == "2":
            ans.append(i[2])

print("".join(ans))

Submission status: Wrong Answer

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

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0
chs = ""
flip = True
ins = 0
while idx < q:
    if t[idx] == 1:
        while idx + 1 < q and t[idx + 1] == 1:
            idx += 1
            flip = not flip
        if flip:
            chs = chs[::-1]
            ins = len(chs) - ins
    else:
        if f[idx] == 1:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 1:
                idx += 1
                ch = c[idx] + ch
            chs = ch + chs
            ins += len(ch)
        else:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 2:
                idx += 1
                ch = ch + c[idx]
            chs = chs + ch
    idx += 1

ans = chs[:ins]
ans += s[::-1] if flip else s
ans += chs[ins:]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

def solve():
    S = list(input())
    M = int(input())
    Q = [list(input().split()) for _ in range(M)]
    
    bef = deque([])
    s = deque(S)
    aft = deque([])
    c = 0
    
    for q in Q:
        if q[0] == '1':
            c += 1
        else:
            ad = q[2]
            if q[1] == '1':
                if c % 2 == 0:
                    bef.appendleft(ad)
                else:
                    aft.append(ad)
            else:
                if c % 2 == 0:
                    aft.appendleft(ad)
                else:
                    bef.append(ad)
    
    ans = ''
    if c % 2 == 0:
        ans = ''.join(reversed(bef)) + ''.join(s) + ''.join(reversed(aft))
    else:
        ans = ''.join(aft) + ''.join(s) + ''.join(bef)

    return ans

print(solve())

Submission status: Wrong Answer

Solution code:
from collections import deque
d=deque()
S=list(input())
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]
flag=0  #反転なし
#print(query)
s=S
s2=""
d.extend(S)
print(d)
for i in range(Q):
    #print(i,len(d))
    if (len(query[i])==1):
        if len(d)>1:
            flag=(flag+1)%2 #反転フラグをひっくり返す
    else:
        if flag==0:
            if query[i][1]=="1":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
        else:
            if query[i][1]=="2":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
    #print(d)
if flag==0:
    print("".join(list(d)))
else:
    d.reverse()
    print("".join(list(d)))

Submission status: Wrong Answer

Solution code:
import math

s=input()
S=[]
S.append(s)
q=input()
Q=int(q)

#print(S,Q)
count=0

for i in range(Q):
  qu = input()
  #print(qu)
  T = int(qu[0])
  
  if T == 1:
    #S.reverse()
    count+=1
  else:
    F = int(qu[2])
    if count % 2 == 0:
      if F == 1:
        S.insert(0,qu[4])
      else:
        S.append(qu[4])
    else:
      if F == 1:
        S.append(qu[4])
      else:
        S.insert(0,qu[4])

      
if count % 2 == 1:
  S.reverse()

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

Submission status: Wrong Answer

Solution code:
# input1 = list(map(int, (input().split())))

S = input()
N = int(input())
c = False
for i in range(N):
    Q = list(input().split())
    a = int(Q[0])
    if a == 1:
        c = not c
    elif a == 2:
        if c == True:
            S = S[::-1]
        b = int(Q[1])
        if b == 1:
            S = Q[2] + S
        elif b == 2:
            S = S + Q[2]
print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
t = s[::-1]
ss = deque(s)
tt = deque(s)
isS = True
n = int(input())
query = []

# print(q1, q2)

for i in range(n):
    query.append(list(input().split()))

# print(query)

for i in range(n):
    # Reverse
    if query[i][0] == '1':
        # s = s[::-1]
        isS = not isS
    # Add
    else:
        # To Head
        if query[i][1] == '1':
            if isS:
                # s = query[i][2] + s
                # t += query[i][2]
                ss.appendleft(query[i][2])
                tt.append(query[i][2])
            else:
                # s += query[i][2]
                # t = query[i][2] + t
                ss.append(query[i][2])
                tt.appendleft(query[i][2])
        # To Tail
        else:
            if isS:
                # s += query[i][2]
                # t = query[i][2] + t
                ss.append(query[i][2])
                tt.appendleft(query[i][2])
            else:
                # s = query[i][2] + s
                # t += query[i][2]
                ss.appendleft(query[i][2])
                tt.append(query[i][2])

if isS:
    # print(s)
    # print(str(s))
    # ans = ''
    temp = list(ss)
    ans = ''.join(temp)
    # for i in range(len(ss)):
    #     ans += ss.popleft()
    print(ans)
else:
    # print(t)
    # print(str(tt))
    # ans = ''
    # for i in range(len(tt)):
    #     ans += tt.popleft()

    temp = list(tt)
    ans = ''.join(temp)
    print(ans)