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 = input()
q = int(input())

query = [""]*q

rev=1

for i in range(q):
    query[i] = input()
    if query[i]=="1":
        rev*=-1
    else:
        r = query[i].split()
        if rev=1:
            if r[1]=="1":
                s = r[2]+s
            else:
                s += r[2]
        else:
            if r[1]=="2":
                s = r[2]+s
            else:
                s += r[2]

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

Submission status: Runtime Error

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

query = [""]*q

for i in range(q):
    query[i] = input()
    if query[i]=="1":
        s = list(reversed(s))
    else:
        r = query[i].split()
        if r[1]=="1":
            s = r[2]+s
        else:
            s += r[2]

print(s)

Submission status: Runtime Error

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

query = []
t = []
f = []
c = []

for i in range(q):
    query[i] = input()
    if query[i]=="1":
        t.append(1)
    else:
        r = query[i].split()
        t.append(2)
        f.append(int(r[1]))
        c.append(r[2])

for i in range(q):
    if t[i]==1:
        s = s[::-1]
    elif t[i]==2:
        if f[i]==1:
            s = c[i]+s
        elif t[i]==2:
            s = s+c[i]
print(s)

Submission status: Runtime Error

Solution code:
def ABC158D():
    s = input()
    q = int(input())
    for i in range(2, q):
        txt3 = input().split()
        print(f"txt3 = {txt3}")
        if txt3[0] == '1':
            s = s[::-1]
        else:
            if txt3[1] == '1':
                s = txt3[2] + s
            else:
                s = s + txt3[2]
    print(s)

ABC158D()

Submission status: Runtime Error

Solution code:
def ABC158D():
    txt2 = txt.split("\n")
    s = txt2[0]
    q = int(txt2[1])
    for i in range(2, len(txt2)):
        txt3 = txt2[i].split()
        if txt3[0] == '1':
            s = s[::-1]
        else:
            if txt3[1] == '1':
                s = txt3[2] + s
            else:
                s = s + txt3[2]
    print(s)

f = open("foot/bar.dat")
txt = f.read()
f.close()
ABC158D(txt)

Submission status: Runtime Error

Solution code:
def ABC158D():
    txt2 = txt.split("\n")
    s = txt2[0]
    q = int(txt2[1])
    for i in range(2, len(txt2)):
        txt3 = txt2[i].split()
        if txt3[0] == '1':
            s = s[::-1]
        else:
            if txt3[1] == '1':
                s = txt3[2] + s
            else:
                s = s + txt3[2]
    print(s)
                
ABC158D()

Submission status: Runtime Error

Solution code:
def ABC158D(txt):
    txt2 = txt.split("\n")
    s = txt2[0]
    q = int(txt2[1])
    for i in range(2, len(txt2)):
        txt3 = txt2[i].split()
        if txt3[0] == '1':
            s = s[::-1]
        else:
            if txt3[1] == '1':
                s = txt3[2] + s
            else:
                s = s + txt3[2]
    print(s)
                
ABC158D(txt)

Submission status: Runtime Error

Solution code:
S=input()
Q=int(input())
ref=0
for i in range(Q):
    T=input().split()
    if T[0] == '2':
        if (T[1]=='1' and ref%2==0):
            S=T[2]+S
        elif (T[1]=='2' and ref%2==0):
            S=S+T[2]
        elif (T[1]=='1' and ref%2==1):
            S=S+T[2]
        elif (T[1]=='2' and ref%2==1):
            S=T[2]+S
    else:
        ref+=1
    print(S)
print(S if ref%2==0 else S[::-1])

Submission status: Runtime Error

Solution code:
from collections import deque

s=input()
s=deque(s)
q=int(input())
ans=''
flg=False
for _ in range(q):
	flg=False
	query=list(map(str,input().split()))
	if query[0]=='1':
		flg=True
	else:
		if flg:
			s.reverse()
		if query[1]=='1':
			s.appendleft(query[2])
		else:
			s.append(query[2])
if flg:
	s=s[::-1]

print(''.join(s))

Submission status: Runtime Error

Solution code:
def main():

    st=input()
    N = int(input())
    sw=True
    for i in range(N):
        t= input().split()
        if t[0]=='1':
            sw=not sw
        if (sw and t[1]=='2') or (not sw and t[1] == '1'):
                st=''.join([st,t[2]])
            else:
                st=''.join([t[2],st])

    if not sw:
        print(st[::-1])
    else:
        print(st)

main()

Submission status: Runtime Error

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

flag = 0
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        flag = flag ^ 1
    elif query[0] == '2':
        if flag == 0:
            if query[1] == '1':
                S.appendleft(query[2])
            else:
                S.append(query[2])
        else:
            if query[1] == '1':
                S.append(query[2])
            else:
                S.appendleft(query[2])
if flag == 0:
    print(''.join(S))
else:
    print(''.join(S[::-1]))

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
reverse=False
top=''

for i in range(n):
    ss=input().split()
    if len(ss)==1:
        reverse=not reverse
    else:
        if ss[1]=="1":
            if reverse: s+=ss[2]
            else: top+=ss[2]
            print(s)
            print(top)
        else:
            if reverse: top+=ss[2]
            else: s+=ss[2]
            print(s)
            print(top)
if reverse:
    s=s[::-1]+top
else:
    s=top[::-1]+s


print(s)

Submission status: Runtime Error

Solution code:
S = str(input())		;#1st input	
num = int(input())		;#2nd input

inv_flag = 1 
left_txt = ""
right_txt =""

##type of valiable##
for i in range(num):
  in_val =input().split()
  if in_val[0] == str(1):
    inv_flag = -1 * inv_flag
  elif in_val[0] == str(2):
    ## add right ##
    if in_val[1] == str(1) and inv_flag == 1:
      left_txt = in_val[2] + left_txt
    if in_val[1] == str(1) and inv_flag == -1:
      rigth_txt = right_txt + in_val[2]
    if in_val[1] == str(2) and inv_flag == 1:
      rigth_txt = right_txt + in_val[2]
    if in_val[1] == str(2) and inv_flag == -1:
      left_txt = in_val[2] + left_txt

out = str(left_txt + S + rigth_txt)
if inv_flag == 1:
  print(out)
else:
  print(out[::-1])

Submission status: Runtime Error

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

s = str(input())
Q = int(input())

for _ in range(Q):
	qu = input()
	if qu == '1':
		s = s[::-1]
	else:
		_, fi, ci = qu.split()
		if fi == '1':
			s = ci + s
		if fi == '2':
			s += ci
print(s)

Submission status: Runtime Error

Solution code:
import sys
s = sys.stdin.readlines()
S = [s[0]]
Q = s[1]
Qs = s[2:]
turned = False


for q in Qs:
  if len(q.strip()) == 1:
    turned = !turned
  else:
    F, C = q.split()[1:3]
    if F == "1":
      S = C + S
    else:
      S += C
print(S)

Submission status: Runtime Error

Solution code:
import sys
s = sys.stdin.readlines()
S = s[0]
Q = s[1]
Qs = s[2:]

for q in Qs:
  if len(q) == 1:
    S = S[::-1]
  else:
    F, C = q.split()[1:3]
    if F == "1":
      S = C + S
    else:
      S += C
print(S)

Submission status: Runtime Error

Solution code:
import sys
s = sys.stdin.readlines()
S = s[0]
Q = s[1]
Qs = s[2:]

for q in Qs:
  T = q.split()[0]
  if T == 1:
    S = S[::-1]
  else:
    F, C = q.split()[1:3]
    if F == 1:
      S = C + S
    else:
      S = S + C
print(S)

Submission status: Runtime Error

Solution code:
# TODO: Fix RE (https://www.dropbox.com/sh/arnpe0ef5wds8cv/AAAk_SECQ2Nc6SVGii3rHX6Fa?dl=0)
import sys
from collections import deque

# sys.stdin = open('input.txt', 'r')

s = deque([s for s in input()])
q = int(input())
rev = False

for query in sys.stdin:
    if query.strip() == '1': # sys.stdin does not remove line breaks. (vs. input() removes line breaks)
        if rev:
            rev = False
        else:
            rev = True
    else:
        t,f,c = query.split()
        if f == '1':
            if rev == True:
                s.append(s)
            else:
                s.appendleft(c)
        elif f == '2':
            if rev == True:
                s.appendleft(c)
            else:
                s.append(c)
if rev == True:
    s.reverse()
print(''.join(s))

Submission status: Runtime Error

Solution code:
from collections import deque
ans=deque()
ans.append(input())
c=0
qu=[input().split() for i in range(int(input()))]
c=0
for i in qu:
    if i[0]=='1':
        c+=1
    else:
        if c%2==0:
            if i[1]=='1':
                ans.appendleft(i[2])
            else:
                ans.append(i[2])
        else:
            ans=ans[::-1]
            if i[1]=='1':
                ans.appendleft(i[2])
            else:
                ans.append(i[2])
            
print(''.join(ans))

Submission status: Runtime Error

Solution code:
import collections
n = int(input())
a = list(map(str,input().split()))
c = collections.Counter(a)

comon = c.most_common()
ans = 0
for i in comon:
  if i[1] != 1:
  	ans += (i[1]*(i[1]-1)//2)
  
for i in a:
  if (ans-int(a.count(i))) < 0:
    print(0)
  else:
    co = a.count(i)
    print(ans-(co*(co-1)//2)+((co-1)*(co-2)//2))

Submission status: Runtime Error

Solution code:
tr = input()
times = int(input())
revers = 0
head = ""
tail = ""
for i in range(times):
    com = input().split()
    if com[0] == "1":
        if revers == 0:
            revers = 1
        else :
            revers = 0
        
    else :
        if com[1] == "1":
            if revers == 0:
                head = com[2]+head
            else :
                tail = tail + com[2]
        else : 
            if revers == 1:
                head = com[2]+head
            else :
                tail = tail + com[2]          
str = head + str + tail
if revers == 1:
    str = str[::-1]
                
print(str)

Submission status: Runtime Error

Solution code:
from collections import deque as dq
s = input()
nq = int(input())

s = dq(s)

jun = True
for i in range(nq):
  query = input().split()
  if query[0] == '1':
    jun = not jun
    continue
  if jun:
    if query[1] == '1':
      s.appendleft(query[2])
    else:
      s.append(query[2])
  else:
    if query[1] == '1':
      s.append(query[2])
    else:
      s.appendleft(query[2])
if jun:
  print(''.join(s))
else:
  print(''.join(s[::-1]))

Submission status: Runtime Error

Solution code:
from collections import deque as dq
s = input()
nq = int(input())

s = dq(s)

jun = True
for i in range(nq):
  query = input().split()
  if query[0] == '1':
    jun = not jun
    continue
  if jun:
    if query[1] == '1':
      s.appendleft(query[2])
    else:
      s.qppend(query[2])
  else:
    if query[1] == '1':
      s.append(query[2])
    else:
      s.appendleft(query[2])
if jun:
  print(''.join(s))
else:
  print(''.join(s[::-1]))

Submission status: Runtime Error

Solution code:
str = input()
times = int(input())
revers = 0
head = ""
tail = ""
for i in range(times):
    com = input().split()
    if com[0] == "1":
        if revers == 0:
            revers = 1:
        else :
            revers = 0
        
    else :
        if com[1] == "1":
            if revers == 0:
                head = com[2]+head
            else :
                tail = tail + com[2]
        else : 
            if revers == 1:
                head = com[2]+head
            else :
                tail = tail + com[2]          
str = head + str + tail
if revers == 1:
    str = str[::-1]
                
print(str)

Submission status: Runtime Error

Solution code:
#n=int(input())
#n,m=map(int,input().split())
#a=[int(x) for _ in input().split()]

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

Submission status: Runtime Error

Solution code:
from collections import deque
S = list(input())
Q = int(input())
perm = 0
d = deque(S)

for i in range(0, Q):
    l = input().split()
    if len(l) == 1:
        perm += 1
    else:
        if (int(l[1])+perm) % 2 == 0:
            d.append(l[2])
        else:
            d.insert(0, l[2])
 
S0 = list(d)
if perm % 2 != 0:
    S0.reverse()
ans = ""
for i in range(0, len(S0)):
    ans = ans + S0[i]
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 22 10:29:00 2020

@author: azikausy
"""

from collections import deque
S = list(input())
Q = int(input())
#Query = {}
#TFC = []
perm = 0
d = deque(S)
for i in range(0, Q):
    l = input().split()
    if len(l) == 1:
        #Query[i] = [l[0], 0, 0]
        #S.reverse()
        perm += 1
    else:
        #Query[i] = l
        if (int(l[1])+perm) % 2 == 0:
            d.append(l[2])
        else:
            d.insert(0, l[2])

#print(d)
S0 = list(d)
#print(S0)
if perm % 2 != 0:
    S0.reverse()
ans = ""
for i in range(0, len(S0)):
    ans = ans + S0[i]
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 22 10:29:00 2020

@author: azikausy
"""

from collections import deque
S = list(input())
Q = int(input())
#Query = {}
#TFC = []
perm = 0
d =deque(S)
for i in range(0, Q):
    l = input().split()
    if len(l) == 1:
        #Query[i] = [l[0], 0, 0]
        #S.reverse()
        perm += 1
    else:
        #Query[i] = l
        if (int(l[1])+perm) % 2 == 0:
            d.append(l[2])
        else:
            d.insert(0, l[2])

S0 = list(d)
if perm % 2 != 0:
    S0.reverse()
ans = ""
for i in range(0, len(S0)):
    ans = ans + S0[i]
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 22 09:27:00 2020

@author: azikausy
"""
from collections import deque

S = list(input())
Q = int(input())
#Query = {}
#TFC = []
perm = 0
d = deque(S)
#l = []
for i in range(0, Q):
    l = input().split()
    if len(l) == 1:
        #Query[i] = [l[0], 0, 0]
        #S.reverse()
        perm += 1
    else:
        #l[2] = l[2] + perm
        #Query[i] = l
        if (int(l[1])+perm) % 2 == 0:
            d.append(l[2])
        else:
            d.insert(0, l[2])

S0 = list(d)
if perm % 2 != 0:
    S0.reverse()
ans = ""
for i in range(0, len(S0)):
    ans = ans + S0[i]
print(ans)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
flag = True
for _ in range(Q):
    T = input().split()
    if len(T) == 1:
        flag = !flag
                        
    if flag: #1
        if int(T[0]) == 2:
            if int(T[1]) == 1:
                S = T[2]+ S
            elif int(T[1]) == 2: 
                S = S + T[2]
    else: #2
        if int(T[0]) == 2:
            if int(T[1]) == 1:
                S = S + T[2]
            elif int(T[1]) == 2: 
                S = T[2] + S
if not flag:
    S = S[::-1]
print(S)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
Q_query = []
flag = True
for _ in range(Q):
    T = input().split()
    if len(T) == 1:
        flag = !flag
                        
    if flag: #1
        if int(T[0]) == 2:
            if int(T[1]) == 1:
                S = T[2]+ S
            elif int(T[1]) == 2: 
                S = S + T[2]
    else: #2
        if int(T[0]) == 2:
            if int(T[1]) == 1:
                S = S + T[2]
            elif int(T[1]) == 2: 
                S = T[2] + S
if !flag:
    S = S[::-1]
print(S)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
Q_query = []
flag = True
for _ in range(Q):
    T = input().split()
    if len(T) == 1:
        flag = !flag
                        
    if flag: #1
        if int(T[0]) == 2:
            if int(T[1]) == 1:
                S = T[2]+ S
            elif int(T[1]) == 2: 
                S = S + T[2]
    elif !flag: #2
        if int(T[0]) == 2:
            if int(T[1]) == 1:
                S = S + T[2]
            elif int(T[1]) == 2: 
                S = T[2] + S
if !flag:
    S = S[::-1]
print(S)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
Q_query = []
flag = True
for _ in range(Q):
    T = input().split()
    if len(T) == 1:
        flag = !flag
                        
    if flag: #1
        if int(T[0]) == 2:
            if int(T[1]) == 1:
                S = T[2]+ S
            elif int(T[1]) == 2: 
                S = S + T[2]
    elif flag: #1
        if int(T[0]) == 2:
            if int(T[1]) == 1:
                S = S + T[2]
            elif int(T[1]) == 2: 
                S = T[2] + S
if !flag:
    S = S[::-1]
print(S)

Submission status: Runtime Error

Solution code:
s = input()
q = int(input())
for i in range(q):
	t, f, c = map(str, input().split())
	if t == '1':
		s = s[::-1]
	elif f == '1':
			s = c + s
	else:
			s += c
print(s)

Submission status: Runtime Error

Solution code:
s = input()
n = int(input())
for i in range(n):
    q = input().split()
    if q[0] == '1':
        if len(s) != 1:
            s_new = ''
            for i in range(len(s)):
                s_new += s[len(s)-1-i]
            s = s_new
        else:
            pass
    else:
        if q[1] == '1':
            s = q[2] + s
        else:
            s += q[2]
    print(s)
print(s)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from collections import deque
s = deque()
n = int(input())
initial = "left"
for i in range(n):
    order = list(input().split())
    if len(order) == 3:
        if initial == "left":
            if int(order[1]) == 1:
                s.appendleft(order[2])
            else:
                s.append(order[2])
        else:
            if int(order[1]) == 1:
                s.append(order[2])
            else:
                s.appendleft(order[2])
    else:
        initial = "right"
    
if initial == "left":
    print(s)
else:
    print(s[::-1])

Submission status: Runtime Error

Solution code:
s = deque()
n = int(input())
initial = "left"
for i in range(n):
    order = list(input().split())
    if len(order) == 3:
        if initial == "left":
            if int(order[1]) == 1:
                s.appendleft(order[2])
            else:
                s.append(order[2])
        else:
            if int(order[1]) == 1:
                s.append(order[2])
            else:
                s.appendleft(order[2])
    else:
        initial = "right"
    
if initial == "left":
    print(s)
else:
    print(s[::-1])

Submission status: Runtime Error

Solution code:


S=input()
Q=int(input())
lis=[]
for _ in range(Q):
    lis.append(input().split())
ans=S
cnt=0
for i in lis:
    if(int(i[0]) == 1):
        cnt= (cnt+1) % 2
    elif((cnt== 0 and i[1]=='1') or (cnt == 1 and i[1]=='2')):
        ans= i[2] + ans
    else:
        ans+=i[2]

print(ans[::-1]) if(cnt % 2 == 1) else print(ans)
print(time.time() - t)

Submission status: Runtime Error

Solution code:
import sys

def main():

    try:
        Ti = n = 0
        S = []
        S = list(map(str, input()))
        Q = map(int, input())
        while n < 6:
            Query = input().split()
            if len(Query) == 1:
                S[0], S[-1] = S[-1], S[0]

            if len(Query) == 3:
                Ti, Fi, Ci = Query
                if Ti == '2':
                    if Fi == '1':
                        S.insert(0, Ci)
                    if Fi == '2':
                        S.append(Ci)
            n+=1
        print(''.join(S))

    except KeyboardInterrupt:
        sys.exit(0)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

int main(){
    string s; cin >> s;
    deque<char> str;
    for (char x : s){
        str.push_back(x);
    }
    bool reversed = false;
    int q; cin >> q;
    while(q--){
        int t; cin >> t;
        if (t == 1){
            reversed = !reversed;
        }
        else{
            int f; cin >> f;
            char c; cin >> c;
            if (reversed){
                f = 3 - f;
            }
            if (f == 1){
                str.push_front(c);
            }
            else{
                str.push_back(c);
            }
        }
    }
    if (reversed){
        reverse(str.begin(), str.end());
    }
    for (char & x : str){
        cout << x;
    }
    cout << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
from collections import deque

s = deque(list(input()))
q = int(input())

rev = True

for i in range(q):
    q = list(input().split())
    if(int(q[0]) == 1):
        rev = rev ^ True
    else:
        n = int(q[1])
        if((n == 1 and rev) or (n == 2 and not rev)):
            s.appendleft(q[2])
        else:
            s.append(q[2])

if not rev:
    s = s[::-1]

print(''.join(s))

Submission status: Runtime Error

Solution code:
from collections import deque

s = deque(list(input()))
q = int(input())

rev = True

for i in range(q):
    q = list(input().split())
    if(int(q[0]) == 1):
        rev = rev ^ True
    else:
        n = int(q[1])
        if((n == 1 and rev) or (n == 2 and not rev)):
            s.appendleft(0, q[2])
        else:
            s.append(q[2])

if not rev:
    s = s[::-1]

print(''.join(s))

Submission status: Runtime Error

Solution code:
    reply = input()
    num = int(input())
    hanten = False
    text = ""
    for i in range(num):
        text = input().split()
        if text[0] == "1":
            hanten = not hanten
        else:
            if text[1] == "1":
                if hanten:
                    reply = reply + text[2]
                else:
                    reply = text[2] + reply
            else:
                if hanten:
                    reply = text[2] + reply
                else:
                    reply = reply + text[2]
     
    if hanten:
        reply = reply[::-1]
     
     
    print(reply)

Submission status: Runtime Error

Solution code:
    reply = []
    reply.append(input())
    num = int(input())
    hanten = False
    text = ""
    for i in range(num):
        text = input().split()
        if text[0] == "1":
            hanten = not hanten
        else:
            if text[1] == "1":
                if hanten:
                    reply.append(text[2])
                else:
                    reply.insert(0,text[2])
            else:
                if hanten:
                    reply.insert(0,text[2])
                else:
                    reply.append(text[2])
     
    reply_text=''.join(reply)
    if hanten:
        reply_text = reply_text[::-1]
     
    print(reply_text)

Submission status: Runtime Error

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

for i in range(Q):
    Y.append(input().split())
    if Y[i][0] == '1':
        S = S[::-1]
        print('反転')
        print(S)
    elif Y[i][1] == '1':
        S = Y[i][2] + S
        print('先頭')
        print(S)
    else:
        S += Y[i][2]
        print('末尾')
        print(S)
print(S)

Submission status: Runtime Error

Solution code:
S=list(input())
q=int(input())
T=['1' for j in range(10**5)]
for i in range(q):
  query=list(input().split())
  if query[0]=='1':
    p=len(S)
    for k in range(p):
      T[k]=S[p-1-k]
    S=T[:p]  
  else:
    if query[1]=='1':
      S.insert(0,query[2])
    else:
      S.append(query[2])
moji=''.join(S)
print(moji)

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
r=-1
f=[]
b=[]
for i in range(n):
    q=input().split()
    if q[0]=='1':
        r*=-1
    else:
        t=int(q[1])
        if t*r==-1 or t*r==2 :
            f.append(q[2])
        else:
            b.append(q[2])
s=f[::-1]+s+b
if r==1:
    s=s[::-1]
print(s)

Submission status: Runtime Error

Solution code:

#n,m=map(int,input().split())
#a=[int(x) for _ in input().split()]

s=input()
q=int(input())
ans=''
flg=False
for i in range(q):
    t=map(str,input().split())
    if t[0]=='1':
    	flg=True
    else:
		if flg:
			s.reverse()
        if t[1]=='1':
			s=t[2]+s
        else:
            s=s+t[2]
    ans=s
print(s)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
flag = False
for i in range(Q):
    T = input()
    if T[0]==1:
        flag = not flag
        continue
    if flag:
        S = S[::-1]

    if T[2]=="1":
        S = T[4]+S
        continue
    else:
        S = S+T[4]
        continue
    if i == Q-1 and flag:
        S = S[::-1]
    
print(S)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    flag = False
    T = input()
  
    if len(T)==1:
        flag = not flag
    else:
      	S = S[::-1]
        if T[2]=="1":
            S = T[4]+S
        else:
              S = S+T[4]
if flag:
    S = S[::-1]
print(S)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
flag = False
for _ in range(Q):
    T = input()
  
    if len(T)==1:
        flag = not flag
    else:
      	S = S[::-1]
        if T[2]=="1":
            S = T[4]+S
        else:
              S = S+T[4]
print(S)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    T = input()
  
    if len(T)==1:
      S = S[::-1]
    else:
	  if T[3]=="1":
          S = T[5]+S
      else:
          S = S+T[5]
print(S)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
  T = input()
  
  if len(T)==1:
    S = S[::-1]
  else:
	if T[3]=="1":
      S = T[5]+S
    else:
      S = S+T[5]
print(S)

Submission status: Runtime Error

Solution code:
S = input()
s_i = 0
Q = int(input())
for _ in range(Q):
  T = input().split()
  if T[0]=="1":
    S = S[::-1]
  else:
	if T[1]=="1":
      S = T[2]+S
    else:
      S = S+T[2]
print(S)

Submission status: Runtime Error

Solution code:
S = input()
s_i = 0
Q = int(input())
for _ in range(Q):
  T = input().split()
  if T[0]=="1":
    S = S[::-1]
  else:
	if T[1]=="1":
      S = T[2]+S
    else:
      S = S+T[2]
print(S)

Submission status: Runtime Error

Solution code:
from collections import deque
S = deque(list(input().strip()))
Q = int(input().strip())
flag = 1
for i in range(Q):
  line = input().strip()
  if len(line) == 1:
    flag = 1 - flag
  else:
    _, F, C = line.strip().split()
    if flag:
      if int(F) == 1:
        S.appendleft(C)
      else:
        S.append(C)
    else:
      if int(F) == 1:
        S.append(C)
      else:
        S.appendleft(C)

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

Submission status: Runtime Error

Solution code:
import math
A, B = list(map(int, input().split()))

amax = math.ceil((A + 1) / 8 * 100) - 1
amin = math.ceil(A / 8 * 100)
bmax = math.ceil((B + 1) * 10) - 1
bmin = math.ceil(B / 10 * 10)

if amin > bmax or bmin > amax:
    print(-1)
else:
    print(int(max(amin, bmin)))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;

int main(){
  string s;
  ll q;
  cin >> s >> q;
  rep(i, q){
    ll t;
    cin >> t;
    if(t == 1) reverse(s.begin(), s.end());
    else{
      ll f;
      string c;
      cin >> f >> c;
      if(f == 1) s = c + s;
      else s = s + c;
    }
  }
  cout << s;
}

Submission status: Runtime Error

Solution code:
from collections import deque
S=input()
q=int(input())
que=deque(S)
rev=False
for i in range(q):
      s=input()
      if s=='1':
            rev=not rev
      else:
            t,f,c=s.split()
            if (f=='1' and rev==False) or (f=='2' and rev==True):
                  que.appendleft(c)
            else:
                  que.append(c)
if rev:
      que=que[::-1]
print(''.join(que))

Submission status: Runtime Error

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

print(s)

Submission status: Runtime Error

Solution code:
s = input()
n = int(input())
query = []
for i in range(n):
    query.append(input().split())
cond = 1
s = deque(s)
for i in range(n):
    if query[i] == ['1']:
        cond *= -1
    else:
        if cond == 1:
            if query[i][1] == '1':
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])
        else:
            if query[i][1] == '2':
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])

result = ''.join(s)
if cond == -1:
    result = result[::-1]
print(result)

Submission status: Runtime Error

Solution code:
s = input()
n = int(input())
query = []
for i in range(n):
    query.append(input().split())
cond = 1
deque(s)
for i in range(n):
    if query[i] == ['1']:
        cond *= -1
    else:
        if cond == 1:
            if query[i][1] == '1':
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])
        else:
            if query[i][1] == '2':
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])

result = ''.join(s)
if cond == -1:
    result = result[::-1]
print(result)

Submission status: Runtime Error

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

a = [0] * q

for i in range(q):
    a[i] = list(map(str, input().split()))
    
for i in range(q):
    if a[i] == [1]:
        for j in range(len(s) // 2):
            s[j],s[len(s)-j] = s[len(s)-j],s[j]
    else:
        if a[i][1] == 1:
            s = a[i][2] + s
        else:
            s = s + a[i][2]
            
print(s)

Submission status: Runtime Error

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

a = [0] * q

for i in range(q):
    a[i] = list(map(str, input().split()))
    
for i in range(q):
    if a[i] == [1]:
        for j in range(len(s) // 2):
            s[j],s[len(s)-j] = s[len(s)-j],s[j]
    else:
        if a[i] == 1:
            s = a[i][2] + s
        else:
            s = s + a[i][2]
            
print(s)