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: Runtime Error

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.append(0,a[4])
            else:
                right.append(a[4])
        elif int(a[2]) ==2:
            if flag ==0:
                right.append(a[4])
            else:
                left.append(0,a[4])

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

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

Submission status: Runtime Error

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: Runtime Error

Solution code:
# N, A, B = map(int, input().split())
# strb="b"*A
# stra="a"*B
# str=strb+stra
# bcount=0
# lenstr=len(str)
# if N <= lenstr:
#     bcount=str[0:N].count('b')
#     print(bcount)
# else:
#     bcount=str.count('b')
#     bcount=bcount*int(N/lenstr)
#     if N%lenstr>0:
#         tmp=str.count("b",0,int(N%lenstr))
#         bcount=bcount+tmp
#     print(bcount)

# ans=[]
# A, B = map(int, input().split())
# for a in range(1,101):
#     for b in range(1,101):
#         tmp1=int(a/0.08)
#         tmp2=int(b/0.1)
#         if tmp1==tmp2:
#             ans.append(tmp1)
# print(ans)
# if len(ans)==0:
#     print("-1")
# else:
    # print(min(ans))

from collections import deque

S = input()
Q = int(input()) #数値
d = deque(S)
for i in range(Q):
    L = list(map(str, input().split()))
    if L[0]=="1":
        d.rotate()
        # d = d[::-1]
        print(d)
    elif L[0]=="2":
        if L[1]=="1":
            d.appendleft(L[2])
        elif L[1]=="2":
            d.append(L[2])
        print(d)
s = "".join(list(d))
print(s)

Submission status: Runtime Error

Solution code:
s = str(input())
n = int(input())
q = [input().split() for x in range(n)]
for i in range(n):
    if int(q[i][0]) == 1:
        s = "".join(list(reversed(s)))
    else:
        if int(q[i][1]) == 1:
            s = "{}{}".format(q[i][2], s)
            print(s)
        else:
            s = "{}{}".format(s, q[i][2])
            print(s)
print(s)

Submission status: Runtime Error

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


def formation():
    global S, Q

    ls = list(S)
    do_rev = False
    for _ in range(Q):
        t_str = input()

        if t_str[0] == '1':
            do_rev = not do_rev
        else:
            splited = t_str.split(' ')
            f = splited[1]
            c = splited[2]
            if f == '1':
                if do_rev:
                    ls.append(c)
                else:
                    ls.insert(0, c)
            else:
                if do_rev:
                    ls.insert(0, c)
                else:
                    ls.append(c)
        print(ls)

    if do_rev:
        ls.reverse()

    return ''.join(ls)


print(formation())

Submission status: Runtime Error

Solution code:
# coding: utf-8
import numpy as np


def main():
    a = input()
    n = int(input())

    order = True
    reverse_count = 0
    for _ in range(n):
        input_ = input().split(' ')
        if input_[0] == '2':
            if order:
                if input_[1] == '1':
                    a = input_[2] + a
                if input_[1] == '2':
                    a = a + input_[2]
            else:
                if input_[1] == '1':
                    a = a + input_[2]
                if input_[1] == '2':
                    a = input_[2] + a
        elif input_[0] == '1':
            order = not order
            reverse_count += 1

    if reverse_count % 2 == 1:
        a = np.array(a)
        a = a[::-1]

    print(a)

    return 0


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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: Runtime Error

Solution code:
import sys
 
 
S = input()
Q = int(input())
h = 0
 
for _ in range(Q):
    A = sys.stdin.readline()
    if A[0] == '1' and len(S)!=1:
        h = h + 1
    else:
        if h % 2 == 0:
            if A[2] == '1':
                S.append(A[4])
            else:
                S = S+A[4]
        else:
            if A[2] == '1':
                S = S+A[4]
            else:
                S = A[4]+S
 
if h % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Runtime Error

Solution code:
from collections import deque
s=input()
q=int(input())
query=[]
Q=[]
for i in range(q):
  a=list(map(str,input().split()))
  if len(a)==3:
    a[0]=int(a[0])
    a[1]=int(a[1])
    if a[1]==1:
      Q.append('l')
    else:
      Q.append('a')
  else:
    a[0]=int(a[0])
    Q.append('1')
  query.append(a)
  
def RLE(lst):
    l=len(lst)
    s=''
    count=1
    for i in range(l-1):
        if lst[i+1]==lst[i]:
            count+=1
        else:
            s+=(lst[i]+str(count))
            count=1
    if lst[l-2]!=lst[l-1]:
        s+=(lst[l-1]+'1')
    else:
        s+=(lst[l-1]+str(count))
    return(s)

rle=RLE(Q)
  
S=deque(list(s))

chk=True
idx=0
for i in range(0,q,2):
  if rle[i]=='1':
    if int(rle[i+1])%2==1:
      chk=False
    idx+=int(rle[i+1])
  if rle[i]=='l':
    if chk==True:
      S.appendleft(query[idx][2])
    else:
      S.append(query[idx][2])
    idx+=int(rle[i+1])
  else:
    if chk==True:
      S.append(query[idx][2])
    else:
      S.appendleft(query[idx][2])
    idx+=int(rle[i+1])
    
if chk==True:
  S.reverse()
T=list(S)
t=''.join(T)
print(t)

Submission status: Runtime Error

Solution code:
S=str(input())
Q=int(input())
for i in range(Q):
    T=input().split()
    if T[0]=="1":
        S=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: Runtime Error

Solution code:
def main():
    S = list(input().split())
    Q = int(input())

    for i in range(Q):
        QList = list(input().split())
    
        if QList[0] == '1':
            S = S[::-1]
        else:
            if QList[1] == '1':
                S = QList[2] + S
            else:
                S = S + QList[2]
    print(S)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# Problem D - String Formation

# input process
S = input()
Q = int(input()) # query number

# initialization
sento = 1
mae_str = []
usiro = []

# process
for i in range(Q):
    query_num = input()
    # クエリが１の時
    if query_num=="1" and sento==1:
        sento = 10
    elif query_num=="1":
        sento = 1
    else: # query num 2
        n, f, c = query_num.split()
        f = int(f)
        if f==1: # 先頭追加処理
            # 先頭の位置によって変わる
            if sento==1:
                mae_str.append(c)
            else:
                usiro.append(c)
        else: # 末尾追加処理
            if sento==1:
                usiro.append(c)
            else:
                mae_str.append(c)

# output process
if sento==1:
    ans = ""
    for m in mae_str[::-1]:
        ans = ans + m
    ans = ans + S
    for u in usiro:
        ans = ans + s
    print(ans)
else:
    ans = ""
    for u in usiro[::-1]:
        ans = ans + u
    ans = ans + S[::-1]
    for m in mae_str:
        ans = ans + m
    print(ans)

Submission status: Runtime Error

Solution code:
def resolve():
    from collections import deque
    d=deque()
    s=list(input())
    d.extend(s)
    #print(d)
    q=int(input())
    Q=[list(map(str,input().split())) for i in range(q)]

    #print(Q)
    rev=0
    for i in range(q):
        if int(Q[i][0])==1:
            if i!=q-1 and int(Q[i+1][0])==2 and int(Q[i+1][1])==1:
                d.append(int(Q[i+1][2]))
                d.reverse()
                i=i+1
            else:
                d.reverse()
            #print(s)
        elif int(Q[i][0])==2:
            if int(Q[i][1])==1:
                d.appendleft(Q[i][2])
                #print(s)
            else:
                d.append(Q[i][2])
                #print(s)
    print(''.join(list(d)))






resolve()

Submission status: Runtime Error

Solution code:
S=str(input())
Q=int(input())
for i in range(Q):
    T=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: Runtime Error

Solution code:
s = input()
q = int(input())
for _ in range(q):
    l = input()
    if ipt == "1":
        s = s[::-1]
    else:
        t, f, c = l.split()
        if f == "1":
            st = c + st
        else:
            st += c
print(st)

Submission status: Runtime Error

Solution code:
from collections import deque
S = input()
Q = int(input())
R = 1
Top = deque()
Bot = deque()
for _ in range(Q):
  inp = input()
  if inp[0]=='1':
    R *= -1
  else:
    o, f, c = inp.split()
    if f=='1' and R==-1 or f=='2' and R==1:
      Bot.append(c)
    else:
      Top.appendleft(c)
Top.append(S)
if not len(Bot)==0:
  Top.append(*Bot)
S = ''.join(Top)
S = S if R==1 else S[::-1]
print(S)

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
for i in range(q):
    line=list(input().split())
    if line[0]==1:
        s=s[::-1]
    else:
        t=line[2]
        if line[1]==1:
            s=t+s 
        else:
            s=s+t
print(s)

Submission status: Runtime Error

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 = s[::-1]
print("".join(s))

Submission status: Runtime Error

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

serch = True
index = []
for i in range(Q-1):
    if serch == True:
        if len(Q_list[i])==len(Q_list[i+1])==1:
            serch = False
        else:
            pass
            index.append(i)
    else:
        serch = True
if index[-1]==Q-2:
    index.append(Q-1)

for j in index:
    i = Q_list[j]
    if i[0] == "1":
        S = S[::-1]
    else:
        if i[1] == "1":
            S = i[2] + S
        else:
            S = S +i[2]
print(S)

Submission status: Runtime Error

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':
            S = S[::-1]
        else:
            if QList[1] == '1':
                S.appendleft(QList[2])
            else:
                S.append(QList[2])
    print("".join(list(S)))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

for i in range(q):
    if int(query[i][0]) == 1:
        if int(query[i-1][0]) != 1:
            rev = 1
        else:
            rev += 1
        if int(query[i+1][0]) != 1:
            if rev %2 == 0:
                pass
            else:
                s = s[::-1]
    else:
        if int(query[i][1]) == 1:
            s = query[i][2] + s
        else:
            s += query[i][2]
            
print(s)

Submission status: Runtime Error

Solution code:
s=list(input())
q=int(input())
t = []
for _ in range(q):
    t.append(input())
for i in range(0,q):
    if t[i][0]=="1":
        s = s.reverse()
    elif t[i][2]=="1":
        s.insert(0,t[i][4])
    else:
        s.append(t[i][4])
print("".join(s))

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
count = 0
for i in range (q):
    t = input().split()
    if(t[0]=='2'):
        if((t[1] == '1' and count % 2 == 0 )or(t[1] == '2' and count % 2 == 1)):
            s = t[2] + s
        else:
            s = s + t[2]
    else:
        count+=1
    print(s)
if(count%2 == 1):
    print(s)
else:
    print(s[::-1])

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
A = deque(input())
B = int(input())
for i in range(B):
    C = sys.stdin.readline().split()
    if C[0] == "1":
        A.reverse()
    else:
		X = C[2]
        if C[1] == "1":
            A.appendleft(X)
        else:
            A.append(X)
print("".join(A))

Submission status: Runtime Error

Solution code:
import math,sys
from collections import Counter, defaultdict, deque
from sys import stdin, stdout
input = stdin.readline
li = lambda:list(map(int,input().split()))

def case():
    s=input().strip()
    d=deque()
    for i in s:
        d.append(s)
    q=int(input())
    c=0
    for i in range(q):
        a=input()
        if(a[0]==1):
            c+=1
        else:
            if(c%2=="0"):
                if(a[1]==1):
                    d.appendleft(a[2])
                else:
                    d.append(a[2])
            else:
                if(a[1]==1):
                    d.append(a[2])
                else:
                    d.appendleft(a[2])
    s="".join(map(d,str))
    if(c%2==0):
        print(s)
    else:
        print(s[::-1])


for _ in range(1):
    case()

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
re = False
front = []
back = []

for i in range(Q):
    t = input()
    
    if len(t) == 1:
        re = not re
        
    else:
        q, f, c = t.split()
        if (re == True and int(f) == 2) or (re == False and int(f) == 1):
            front.append(c)
        elif (re == True and int(f) == 1) or (re == False and int(f) == 2):
            back.append(c)

if re == False:
    front.reverse()
    F = "".join(front)
    B = "".join(back)
    print(F + S + B)
else:
    back.reverse()
    S.reverse()
    F = "".join(front)
    B = "".join(back)
    print(B + S + F)

Submission status: Runtime Error

Solution code:
from collections import deque
S = input()
Q = int(input())
R = 1
Top = deque()
Bot = deque()
for _ in range(Q):
  inp = input()
  if inp[0]=='1':
    R *= -1
  else:
    o, f, c = inp.split()
    if f=='1' and R==-1 or f=='2' and R==1:
      Bot.append(c)
    else:
      Top.appendleft(c)
Top.append(S)
Top.append(*Bot)
S = ''.join(Top)
S = S if R==1 else S[::-1]
print(S)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
count = 0
lr = [[], []]
for i in range(Q):
    TFC = input()
    if TFC[0] == '1':
        count += 1
    elif TFC[2] == '1':
        lr[count % 2].append(TFC[4])
    else:
        lr[(count+1) % 2].append(TFC[4])
if count % 2 == 0:
    print(''.join([*lr[0][::-1], S, *lr[1]]))
else:
    print(''.join([*lr[1][::-1] , S[::-1], *lr[0]]))

Submission status: Runtime Error

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
    print(s)
if to % 2 == 1:
    s = reversed(s)
print("".join(s))

Submission status: Runtime Error

Solution code:
from collections import deque

S = deque(input())

Q = int(input())

for i in range(Q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        S = S[::-1]
    elif int(q[0]) == 2:
        if int(q[1]) == 1:
            S.appendleft(q[2])
        elif int(q[1]) == 2:
            S.append(q[2])

print("".join(S))

Submission status: Runtime Error

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

def main():
    s = input()[:-1]
    q = int(input())
    tmp = [input().split() for _ in range(q)]

    for i in range(len(tmp)):
        if len(tmp[i]) == 1:
            s = s[::-1]
        else:
            if int(tmp[i][1]) == 1:
                s = tmp[i][2] + s
            else:
                s = tmp[i][2] + c
                
    print(s)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#coding:utf-8
import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7
    S = input()
    Slist = [S]
    Q = II()
    Slist = ['']*Q+Slist+['']*Q
    left = Q-1
    right = Q+len(S)
    r = False
    for _ in range(Q):
        query = input().split()
        if query[0] == '1':
            r ^= True
        else:
            if query[1] == '1':
                    if r:
                        # S = ''.join([S,query[2]])
                        Slist[right] = query[2]
                        right += 1

                    else:
                        # S = ''.join([query[2],S])
                        Slist[left] = query[2]
                        left -=1
            else:
                if r:
                    Slist[left] = query[2]
                    left -=1
                else:
                    # S = ''.join([S,query[2]])
                    Slist[right] = query[2]
                    right += 1
    # print(S[::-1] if r else S)
    print(''.join(Slist[::-1]) if r else ''.join(Slist))
                



    

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
a = input()
if 'A' in a and 'B' in a:
    print("Yes")
else:
    print("No")
--------------------------------
n, a, b = map(int, input().split())
period = a+b
times = n//period
k = n%period
n =  a if k >a else k
print(times*a + n)
---------------------------
a, b = [int(x) for x in input().split()]
 
if (b < a):
    temp = b
    b = a
    a = temp
 
 
x = 1
while True:
    if (int(x*0.08) == a and int(x*0.1) == b):
        print(x)
        break
    else:
        if (x > 1500):
            print(-1)
            break
        else:
            x += 1

1, 2 and 3
------------------------
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

Submission status: Runtime Error

Solution code:
a = input()
if 'A' in a and 'B' in a:
    print("Yes")
else:
    print("No")
--------------------------------
n, a, b = map(int, input().split())
period = a+b
times = n//period
k = n%period
n =  a if k >a else k
print(times*a + n)
---------------------------
a, b = [int(x) for x in input().split()]
 
if (b < a):
    temp = b
    b = a
    a = temp
 
 
x = 1
while True:
    if (int(x*0.08) == a and int(x*0.1) == b):
        print(x)
        break
    else:
        if (x > 1500):
            print(-1)
            break
        else:
            x += 1

1, 2 and 3
------------------------
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
r = False
F = []
B = []
for i in range(Q):
    q = input()
    if q[0] == "1":
        if r == False:
            r = True
        else:
            r = False
    elif q[2] == "1":
        if r == False:
            F.append(q[4])
        else:
            B.append(q[4])
    else:
        if r == False:
            B.append(q[4])
        else:
            F.append(q[4])
if r == False:
    S = F[::-1] + S + B
else:
    S = B[::-1] + S[::-1] + F
print(S)

Submission status: Runtime Error

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

for _ in range(Q):
    q = input()
    if len(q) == 1:
        S = list(S)
        S.reverse()
        S = "".join(S)
    else:
        T, F, C = q.split()
        if int(F) == 1:
            S = C + S
        else:
            S = S + C

print(S)

Submission status: Runtime Error

Solution code:
        S = S[::-1]

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
 = 0
ans = []
T = []

for i in range(Q):
  TS = input()
  if TS == "1":
    revcount += 1
  else:
    T.append(TS.rstrip().split(" "))
    T[len()-1][1] = str((int(T[len(T)-1][1]) + revcount % 2 + 1) % 2 + 1)
if revcount % 2 == 1:
  ans.append(S[::-1])
else:
  ans.append(S)
for i in range(len(T)):
  if (int(T[iinput()
Q = int(input())
revcount = 0
ans = []
T = []

for i in range(Q):
  TS = input()
  if TS == "1":
    revcount += 1
  else:
    T.append(TS.rstrip().split(" "))
    T[len(T)-1][1] = str((int(T[len(T)-1][1]) + revcount % 2 + 1) % 2 + 1)
if revcount % 2 == 1:
  ans.append(S[::-1])
else:
  ans.append(S)
            
for i in range(len(T)):
  if (int(T[i][1]) == 1 and revcount % 2 == 0) or (int(T[i][1]) == 2 and revcount % 2 == 1):
    ans.insert(0,T[i][2])
  else:
    ans.append(T[i][2])
ans2 = "".join(ans2  1]) == 1 and revcount % 2 == 0) or (int(T[i][1]) == 2 and revcount % 2 == 1)
    ans.insert(0,T[i][2])
  else:
    ans.append(T[i][2])
ans2 = "".join(ans)
print(ans2)

Submission status: Runtime Error

Solution code:
ｓ

Submission status: Runtime Error

Solution code:
import sys
s = sys.stdin.readlines()

s = s[0]
q = int(s[1]) #int(input())
inv = 1

for t in s[2:q]:
  # t = input()
  if t[0] == '1':
    inv *= -1 #s = s[::-1]
  else:
    _, f, c = map(str, t.split())
    if (f =='1' and inv == 1) or (f =='2' and inv == -1):
      s = c+s
    else:
      s = s+c

print(s[::inv])

Submission status: Runtime Error

Solution code:
import sys
s = sys.stdin.readlines()

s = s[0]
q = int(s[1]) #int(input())
inv = 1

for t in s[2:q+1]:
  # t = input()
  if t[0] == '1':
    inv *= -1 #s = s[::-1]
  else:
    _, f, c = map(str, t.split())
    if (f =='1' and inv == 1) or (f =='2' and inv == -1):
      s = c+s
    else:
      s = s+c

print(s[::inv])

Submission status: Runtime Error

Solution code:
S = list(input())
N = int(input())
t = [0] * N
f = [0] * N
c = [0] * N
for i in range(N):
    li = list(input().split())
    if len(li) == 1:
        t[i] = li[0]
        f[i] = 0
        c[i] = 0
    if len(li) == 2:
        t[i], f[i] = li[0]
        c[i] = 0
    if len(li) == 3:
        t[i], f[i], c[i] = li
    
    if t[i] == '1':
        S = S[::-1]
    else:
        if f[i] == '1':
            S[0:0] = c[i]
        elif f[i] == '2':
            S.extend(c[i])
        
 
 
print(''.join(S))

Submission status: Runtime Error

Solution code:
s = list(input())
n = int(input())
import numpy as np

s = np.array(s)

L = []
for i in range(n):
    L = (list(map(str,input())))
    #print(L)
    if len(L) >1:
        if int(L[2]) == 1:
            #print(s)
            s = np.append(L[4],s)
        elif int(L[2]) == 2:
            s = np.append(s,L[4])
    
    elif len(L) ==1:
        s = np.rot90(s, 2)
        
print("".join(s))

Submission status: Runtime Error

Solution code:
import sys
a = sys.stdin.read().split('\n')[:-1]
s = list(a[0])
q = int(a[1])
f = 1
for i in range(2, q + 2):
    if a[i][0] == '1':
        f = 1 - f
    else:
        l = len(s)
        s.insert(a[i][2] == '21'[f] ? 0 : l, a[i][4])
if f == 1:
    print(''.join(s))
else:
    print(''.join(reversed(s)))

Submission status: Runtime Error

Solution code:
S = list(input())
Q = int(input())
query = [input().split() for i in range(Q)]
print(query)
for i in query:
    # print(i)
    if i[0] == '1':
        
        S = list(reversed(S))
        print(S)
    elif i[0]=='2' and i[1]=='1':
        S.insert(0, i[2])
        print(S)
    elif i[0]=='2' and i[1]=='2':
        S.append(i[2])
        print(S)
print(''.join(S))

Submission status: Runtime Error

Solution code:
from sys import stdin
s = str(stdin.readline().rstrip())
q = int(stdin.readline().rstrip())
for i in range(q):
    query = list(map(int, stdin.readline().rstrip().split()))
    if int(query[0])==1:
        s = s[::-1]
    else:
        if int(query[1])==2:
            s += query[2]
        else:
            s = query[2] + s

print(s)

Submission status: Runtime Error

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

for k in range(Q):
    q =  [ i for i in input().split(' ')]
    if q[0] == '1':
        if not len(S) == 1:
            head = S[0]
            tail = S[-1]
            S = tail + S[1:-1] + head
            print(S)
    else:
        if q[1] == '1':
            S = q[2] + S
            print(S)
        else:
            S = S + q[2]
            print(S)
print(S)

Submission status: Runtime Error

Solution code:
def switch(S):
    tmp = S[0]
    S[0] = S[-1]
    S[-1] = tmp
    return S


def d2():
    S = str(input())
    Q = int(input())

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

        if q[0] == '1':
            S = switch(S)
        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))


##########
d2()

Submission status: Runtime Error

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

pre = ''
suf = ''
count = 0
for i in range(q):
    ts = sys.stdin.readline().rstrip().split()
    if ts[0] == '1':
        count += 1
    else:
        t = int(ts[0])
        f = int(ts[1])
        c = ts[2]
        if f == 1:
            strs = []
            if count % 2 == 1:                
                strs = [suf, c]
                suf = ''.join(strs)
            else:
                strs = [pre, c]
                pre = ''.join(strs)
        else:
            if count % 2 == 1:
                strs = [pre, c]
                pre = ''.join(strs)
            else:
                strs = [suf, c]
                suf = ''.join(strs)
        print('pre:'+pre)
        print('suf:'+suf)
        

print(count)
strs  = []
if count % 2 == 0:
    strs = [pre[::-1], s, suf[::-1]]
else:
    strs = [suf, s[::-1], pre]

print (''.join(strs))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
l = r = ""
for op in qs:
    if op[0] == '1':
        n = not n
    if op[0] == '2':
        t , f, c = op
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f = 1:
            l=c+l
        elif f == 2:
            r+=c
s = l+s+r
print(s[::-1] if n else s)

Submission status: Runtime Error

Solution code:
    import sys
    S = str(input())
    Q = int(input())
    Query = [list(map(str, input().split())) for _ in range(Q)]
     
    sArry = S.split()
    for i in range(Q):
    #    print(i,Query[i])
        if Query[i][0] == '1':
            tmp_first = sArry[0]
            sArry[0] = sArry[-1]
            sArry[-1] = tmp_first
        else:
            if Query[i][1] == '1':
                sArry.insert(0, Query[i][2])
            else:
                sArry.append(Query[i][2])
                
    print("".join(sArry))

Submission status: Runtime Error

Solution code:
S=list(input())
Q=int(input())
T=[]
for i in range(Q):
  T.append(input().split())
for i in range(Q-1):
  if T[i][0]=="1" and T[i+1][0]=="1":
    T.pop(i)
    T.pop(i)
for i in range(len(T)):
  if T[i][0]=="1":
    S.reverse()
  else:
    if T[i][1]=="1":
      S.insert(0,T[i][2])
    else:
      S.append(T[i][2])
print("".join(S))

Submission status: Runtime Error

Solution code:
N= input()
M= int(input())
for _ in range(M):
    a = list(map(str, input().split()))
    if (a[0]=='1'):
        count = count +1
    else:
        if (a[1]=='1'):
            N = a[2]+N
        else:
            N = N+a[2]
if (count%2==1):
    print(N[::-1])
else:
    print(N)