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())

f, l = S[0],S[-1]

mod = 0
for _ in range(Q):
    Query = input()
    if Query[0] == "1":
        f, l = l, f #swap
        mod = (1 + mod) % 2
    else:
        if Query[2] == "1":
            f = Query[4]
            if mod == 0:
                S = f + S
            else:
                s = s + f
        else:
            l = Query[4]
            if mod == 0:
                S = S + l
            else:
                S = l + S
    #print("Qans",S)

if mod==0:
    print(S)
else:
    print(S[::-1])

Submission status: Runtime Error

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

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

print(S)

Submission status: Runtime Error

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

S = str(input())
Q = int(input())
A = [[input().split("")] for _ in range(Q)]

for i in range(Q):
  if A[i] == ["1"]:
    S = ''.join(list(reversed(S)))
  else:
    if A[i][1] == "1":
      S = A[i][2] + str(S)
    else:
      S = str(S) + A[i][2] 
      
print(S)

Submission status: Runtime Error

Solution code:

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


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

print(S)

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
q_list=[input() for _ in range(q)]
for i in range(q):
  temp=q_list[i]
  if int(temp[0])==1:
    temps=s[::-1]
    s=temps
  elif int(temp[0])==2:
    if int(temp[1])==1:
      s=temp[2]+s
    elif int(temp[1])==2:
      s=s+temp[2]
print(s)

Submission status: Runtime Error

Solution code:
S = list(input())
Q = int(input())
l_list = []
r_list = []
for i in range(Q):
    A = list(map(str, input().split()))
    if len(A)==1:
        flag = not flag
    else:
        if flag:
            if A[1]=="1":
                l_list.insert(0, A[2])
            else:
                r_list.append(A[2])
        else:
            if A[1]=="1":
                r_list.insert(0, A[2])
            else:
                l_list.append(A[2])
if flag:
    print("".join(l_list)+"".join(S)+"".join(r_list))
else:
    print("".join(r_list[::-1])+"".join(S[::-1])+"".join(l_list[::-1]))

Submission status: Runtime Error

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


for i in range(Q):
  if q[i][0] == "1":
    S = S[::-1]
    continue
  else:
    if q[i][1] == "1":#先頭に追加
      S = q[i][2] + S
      continue
    else q[i][1] == "2":#末尾に追加
      S += q[i][2]
      continue

print(S)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
query_list = []
for i in range(Q):
    query_list.append(input())
# print(S, Q, query_list)

M = 10 ** 6
start_position = int(M / 2)
left_position = start_position
right_position = start_position + len(S)
result = ["-"] * M
for i in range(len(S)):
    # print(i + left_position)
    result[i + left_position] = S[i]

# result = "".join(result[left_position:right_position])
# print(result)
# exit(0)
reverse_flag = False

for i in range(Q):
    # print(i + 1)
    # print(result[left_position:right_position])
    q = query_list[i]
    # print(q)
    t = int(q[0])
    if t == 1:
        reverse_flag = not reverse_flag
    else:
        t, f, c = q.split()
        f = int(f)
        c = c
        if f == 1:
            if reverse_flag:
                result[right_position] = c
                right_position += 1
            else:
                result[left_position - 1] = c
                left_position -= 1
        else:
            if reverse_flag:
                result[left_position - 1] = c
                left_position -= 1
            else:
                result[right_position] = c
                right_position += 1
    # print(t, f, c, result)
    # print("--------")
    print(result[left_position:right_position])
# result = str(result)
# result = "".join(result)
# print(result)
# print(result[left_position:right_position])

result = "".join(result[left_position:right_position])
if reverse_flag:
    result = result[::-1]
print(result)

Submission status: Runtime Error

Solution code:
from collections import deque
S=list(input())
Q=int(input())
Qlst=[list(input().split()) for _ in range(Q)]

len_S=len(S)
s0=deque(sorted(S[:len_S//2]))
s1=deque(S[len_S//2:])

if len_S%2!=0:
    mid=s1.popleft()
else:
    mid=''

fflag=0
for i in range(Q):
    q=Qlst[i]
    if q[0]=='1':
        fflag=abs(fflag-1)
        continue
    if q[1]=='1':
        if fflag==0:
            s0.append(q[2])
            if mid=='':
                mid=s0.popleft()
            else:
                s1.insert(0,mid)
                mid=''
        else:
            s1.append(q[2])
            if mid=='':
                mid=s1.popleft()
            else:
                s0.insert(0,mid)
                mid=''
    elif q[1]=='2':
        if fflag==0:
            s1.append(q[2])
            if mid=='':
                mid=s1.popleft()
            else:
                s0.append(mid)
                mid=''
        else:
            s0.append(q[2])
            if mid=='':
                mid=s0.popleft()
            else:
                s1.append(mid)
                mid=''

if fflag==0:
    s0.reverse()
    print(''.join(s0)+mid+''.join(s1))
else:
    s1.reverse()
    print(''.join(s1)+mid+''.join(s0))

Submission status: Runtime Error

Solution code:
def add_w(n, letter, a):
    if n == 1:
        a = letter + a
    else:
        a = a + letter
    return a

def seq(query, a):
    data = query.split()
    if int(data[0]) == 1:
        a = a[::-1]
    else:
        n = int(data[1])
        letter = data[2]
        a = add_w(n, letter, a)
    return a

a = input()
num = int(input())

data = get_inputs(num)

for d in data:
    a = seq(d, a)
print(a)

Submission status: Runtime Error

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

f, l = S[0],S[-1]

mod = 0
for _ in range(Q):
    Query = input()
    if Query[0] == "1":
        f, l = l, f #swap
        mod = (1 + mod) % 2
    else:
        if Query[2] == "1":
            f = Query[4]
            if mod == 0:
                S = f + S
            else:
                s = s + f
        else:
            l = Query[4]
            if mod == 0:
                S = S + l
            else:
                S = l + S
    #print("Qans",S)

print(S)

Submission status: Runtime Error

Solution code:
import sys

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

for query in sys.stdin:
    if query == '1':
        s = s[::-1]
    else:
        t,f,c = query.split()
        if f == '1':
            s = c + s
        elif f == '2':
            s = s + c
print(s)

Submission status: Runtime Error

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

# 文字列格納リスト
ans = [S]

# クエリ格納リスト
q_s = []

for i in range(Q):
  _q = list(map(str, input().split()))
  if _q[0] == "2":
    q_s.append(_q)
  elif _q[0] == "1":
    if len(q_s) > 0:
      if q_s[-1][0] == "1":
        del q_s[-1]
      else:
        q_s.append(_q)
    elif len(q_s) == 0:
      q_s.append(_q)
for i in q_s:
  if i[0] == "1":
    ans.reverse()
  elif i[0] == "2":
    if i[1] == "1":
      # 前に追加
      ans.insert(0,i[2])
    elif i[1] == "2":
      # 後ろに追加
      ans.append(i[2])
print("".join(ans))

Submission status: Runtime Error

Solution code:
import pandas as pd

s = str(input())
n = int(input())
q_list = [list(map(str, input().strip().split())) for i in range(n)]
df = pd.DataFrame( columns=['S', 'N'] )
df = df.append(pd.Series([s, 0], index=df.columns), ignore_index=True)
out = ''

for q in q_list:
    if q[0] == '1':
        df['N'] = len(df) - 1 - df['N']
    elif q[1] == '1':
        df['N'] = df['N'] + 1
        df = df.append(pd.Series([q[2], 0], index=df.columns), ignore_index=True)
    else:
        df = df.append(pd.Series([q[2], len(df) ], index=df.columns), ignore_index=True)

df_s = df.sort_values('N')
for x in df_s['S']:
    out += x
print(out)

Submission status: Runtime Error

Solution code:
s1=input()
q=int(input())
s0=[]
f=1
for i in range(q):
  a=list(input().split())
  #print(i,a,s)
  if int(a[0])==1:
    f=(f+1)%2
  else:
    if (f+int(a[1]))%2==0:
      s0.append(a[2])
    else:
      s1.append(a[2])
if f==1:
  print("".join(s0[::-1])+"".join(s1))
else:
  print("".join(s1[::-1])+"".join(s0))

Submission status: Runtime Error

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

S = input()
Q = int(input())
A = [(input().split("")) for _ in range(Q)]

for i in range(Q):
  if A[i] == ["1"]:
    S = ''.join(list(reversed(S)))
  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())
count = 0
lr = ['', '']
for i in range(Q):
    TFC = input()
    if TFC[0] == '1':
        count += 1
    elif TFC[2] == '1':
        lr[count % 2] += TFC[4]
    else:
        lr[(count+1) % 2] += TFC[4]
if count % 2 == 0:
    # print(lr[0][::-1] + S + lr[1])
    print(''.join([*list(reversed(lr[0])), S, lr[1]]))
else:
    print(''.join([*list(reversed(lr[1])) , *list(reversed(S)), lr[0]]))

Submission status: Runtime Error

Solution code:


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

def addflont(s,c):
    return c+s
def addback(s,c):
    return s+c

reverse = 0

for i in range(q):
    t = input()
    if t[0]=="1":
        reverse = reverse^1
    if t[0]=="2":
        if t[2]=="1":
            if reverse==0:
                s = c+s
            else:
                s = s+c
        elif t[2]=="2":
            if reverse==0:
                s = s+c
            else:
                s = c+s

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

Submission status: Runtime Error

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

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

    if(len(l) == 0):
        s.reverse()
    else:
        if(int(l[1]) == 1):
            s = l[2] + s
        else:
            s = s + l[2]
print(s)

Submission status: Runtime Error

Solution code:
s = input()
sc = s[::-1]
n = int(input())
l = [list(input().split()) for i in range(n)]

#last = 1

for i in range(n):
    if l[i][0] == '1':
        save = s
        s = sc
        sc = save
    
if last > 0:
    print(s)
else:
    print(sc)

Submission status: Runtime Error

Solution code:
S=input()
Q=int(input())
list=[]
mojiretu=[z for z in S]
for a in range(Q):
    b=input().split()
    list.append(b)
for z in range(len(list)-1):
    if list[z]==list[z+1]:
        list.replace(list[z],' ')
        list.replace(list[z+1],' ')
nlist=[x for x in list if x]
for c in nlist:
    if len(c)==1:
        mojiretu.reverse()
    else:
        if c[1]=='1':
            mojiretu.insert(0,c[2])
        else:
            mojiretu.append(c[2])
print(''.join(mojiretu))

Submission status: Runtime Error

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

S = input()
Q = int(input())
A = [(input().split("")) for _ in range(Q)]

for i in range(Q):
  if A[i] == ["1"]:
    S = ''.join(list(reversed(S)))
  else:
    if A[i][1] == "1":
      S = S + A[i][2]
    else:
      S = A[i][2] + S
      
print(S)

Submission status: Runtime Error

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

li = [input() for i in range(q)]

k= 0

for i in range(q):
    if li[0] == 1:
        k += 1
    else:
        li3 = li[i].split()
        if (int(li3[1])+k) % 2 == 0:
            s = s + li3[2]
        else:
            s = li3[2] + s

if k % 2 == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Runtime Error

Solution code:
def toku()
  S = input()
  Q = int(input())
  s = [S[0]]
  A = []
  count = 0
  for i in range(Q):
    A += [input().split()]
  for i in range(Q):
    if int(A[i][0]) == 2:
      if count % 2 == 1:
        s = s[::-1]
      count = 0
      if int(A[i][1]) == 1:
        s = [A[i][2]] + s
      else:
        s = s + [A[i][2]]
    else:
      count += 1
      if i == Q-1:
        if count % 2 == 1:
          s = s[::-1]


  result = ''.join(s)
  print(result)

Submission status: Runtime Error

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

def main():
    
    s = input()
    q = int(input())
    rev = False
    
    arr = np.array([s])
    for _ in range(q):
        a = input()
        if a == '1':
            #arr = arr[::-1]
            rev = rev != True
        else:
            tmp, f, c = a.split(' ')
            if f == '1':
                if rev == True:
                    arr = np.append(arr, c)
                else:
                    arr = np.insert(arr, 0, c)
            elif f == '2':
                if rev == True:
                    arr = np.insert(arr, 0, c)
                else:
                    arr = np.append(arr, c)
            else:
                print(f)
    if rev == True:
        print(''.join(arr[::-1]))
    else:
        print(''.join(arr))
        

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
rom sys import stdin

S = input()
Q = int(input())
h = 0

for i in range(Q):
    A = stdin.readline()[:-1]
    print(A)
    if A[0] == '1' and len(S)!=1:
        h = h + 1
    else:
        if h % 2 == 0:
            if A[2] == '1':
                S = A[4]+S
            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:
def toku()
  S = input()
  Q = int(input())
  s = [S[0]]
  A = []
  count = 0
  for i in range(Q):
    A += [input().split()]
  for i in range(Q):
    if int(A[i][0]) == 2:
      if count % 2 == 1:
        s = s[::-1]
      count = 0
      if int(A[i][1]) == 1:
        s = [A[i][2]] + s
      else:
        s = s + [A[i][2]]
    else:
      count += 1
      if i == Q-1:
        if count % 2 == 1:
          s = s[::-1]


  result = ''.join(s)
  print(result)
toku()

Submission status: Runtime Error

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

qs = [input().split() for i in range(Q)]

i = 0
while i < len(qs):
    q = qs[i]
    if q[0] == '1':
        if qs[i+1][0] == '1':
            i += 2
        else:
            S = S[::-1]
            i += 1
    else:
        if q[1] == '1':
            S = q[2] + S
        else:
            S = S + q[2]
        i += 1



print(S)

Submission status: Runtime Error

Solution code:
a=input()
q=int(input())
for i in range(q):
    b=input().split()
    if b[0]=="1":
        a=[::-1]
    else:
        if b[1]=="1":
            a=b[2]+a
        else:
            a+=b[2]
print(a)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
Query = [input() for _ in range(Q)]

result_list = []
result_list.append(S)
for que in Query:
    print(result_list)
    if len(que) > 1:
        que_list = que.split()
        print(que_list[1])
        if que_list[1] == "1":
            result_list.insert(0, que_list[2])
        else:
            result_list.append(que_list[2])
    else:
        result_list.reverse()

print(''.join(result_list))

Submission status: Runtime Error

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

buf = [0] * (3*(10**5)+10)
for i, s in enumerate(S):
    buf[i] = s

is_reverse = False
first = 0
last = len(S) - 1

def add_top(c):
    global buf
    global first
    if is_reverse:
        first += 1
    else:
        first -= 1
    buf[first] = c

def add_tail(c):
    global buf
    global last
    if is_reverse:
        last -= 1
    else:
        last += 1
    buf[last] = c

for i in range(Q):
    Query = input().split()
    t = int(Query[0])
    if t == 1:
        is_reverse = not is_reverse
        first, last = last, first
    else:
        f = int(Query[1])
        c = Query[2]
        if f == 1:
            add_top(c)
        else:
            add_tail(c)

ans = ''
#print(is_reverse, first, last)
#print(buf[first])
#print(buf[last])
if is_reverse:
    l = list(reversed(buf[:first+1]))
    #print(l)
    l[len(l):] = list(reversed(buf[last:]))
    #print(l)
    #ans = ''.join(buf[last:first+1:-1])
    ans = ''.join(l)
else:
    l = buf[first:]
    #print(l)
    l[len(l):] = buf[:last+1]
    #print(l)
    #ans = ''.join(buf[first:last+1])
    ans = ''.join(l)
print(ans)

Submission status: Runtime Error

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

for i in range(a):
    c=input()
    if c[0]=='1':
        s=''.join(list(reversed(s)))
    else:
        if c[2]=='1':
            s=c[4]+s
        else:
            s=s+c[4]
    
    print(s)
            
print(s)

Submission status: Runtime Error

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

buf = [0] * (2*(10**5)+10)
for i, s in enumerate(S):
    buf[i] = s

is_reverse = False
first = 0
last = len(S) - 1

def add_top(c):
    global buf
    global first
    if is_reverse:
        first += 1
    else:
        first -= 1
    buf[first] = c

def add_tail(c):
    global buf
    global last
    if is_reverse:
        last -= 1
    else:
        last += 1
    buf[last] = c

for i in range(Q):
    Query = input().split()
    t = int(Query[0])
    if t == 1:
        is_reverse = not is_reverse
        first, last = last, first
    else:
        f = int(Query[1])
        c = Query[2]
        if f == 1:
            add_top(c)
        else:
            add_tail(c)

ans = ''
#print(is_reverse, first, last)
#print(buf[first])
#print(buf[last])
if is_reverse:
    l = list(reversed(buf[:first+1]))
    #print(l)
    l[len(l):] = list(reversed(buf[last:]))
    #print(l)
    #ans = ''.join(buf[last:first+1:-1])
    ans = ''.join(l)
else:
    l = buf[first:]
    #print(l)
    l[len(l):] = buf[:last+1]
    #print(l)
    #ans = ''.join(buf[first:last+1])
    ans = ''.join(l)
print(ans)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
P = []
for n in range(Q):
    P.append(input().split())
if Q > 1:
    for i in range(0, Q, 2):
        if len(P[i]) == 1 and len(P[i+1]) != 1:
            S = S[::-1]
            if P[i+1][1] == "1":
                S = P[i+1][2] + S
            if P[i+1][1] == "2":
                S = S + P[i+1][2]
        if len(P[i]) != 1 and len(P[i+1]) == 1:
            if P[i][1] == "1":
                S = P[i][2] + S
            if P[i][1] == "2":
                S = S + P[i][2]
            S = S[::-1]
        if len(P[i]) != 1 and len(P[i+1]) != 1:
            if P[i][1] == "1":
                S = P[i][2] + S
            if P[i][1] == "2":
                S = S + P[i][2]
            if P[i+1][1] == "1":
                S = P[i+1][2] + S
            if P[i+1][1] == "2":
                S = S + P[i+1][2]
if Q%2 == 1:
    if len(P[-1]) == 1:
        S = S[::-1]
    else:
        if P[-1][1] == "1":
            S = P[-1][2] + S
        if P[-1][1] == "2":
            S = S + P[-1][2]
print(S)

Submission status: Runtime Error

Solution code:
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=[]
s=input()
S.append(s)
Q=int(input())
for i in range(Q):
    r=input()
    if len(r.split()) == 3:
        T,F,C=r.split()
    else:
        T='1'
    if T == '1':
        tmp=S[0]
        S[0]=S[len(S)-1]
        S[len(S)-1]=tmp
        continue
    if T == '2':
        if F == '1':
            q=[]
            q.append(C)
            q.append(S)
            S=q
        if F == '2':
            S.append(C)

print("".join(S))

Submission status: Runtime Error

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

if count%2==0:
    print(''.join(ad[0],s,ad[1]))
else:
    print(''.join(ad[0]),end='')
    for i in range(len(s)):
        print(s[-(i+1)],end='')
    print(''.join(ad[1]))

Submission status: Runtime Error

Solution code:
s=list(input())
q=int(input())
start=[]
owari=[]
r=1
for i in range(q):
    data=input().split()
    if(len(data)==1):
        r*=-1
    elif(data[1]=="1"):
        if(r==1):
            start.append(data[2])
        else:
            owari.append(data[2])
    else:
        if(r==1):
            owari.append(data[2])
        else:
            start.append(data[2])
    print(start,owari,r)

if(r==1):
    start.reverse()
    print(''.join(start),end='')
    print(''.join(s),end='')
    print(''.join(owari),end='')
else:
    owari.reverse()
    s.reverse()
    print(''.join(owari),end='')
    print(''.join(s),end='')
    print(''.join(start),end='')

Submission status: Runtime Error

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

for _ in range(Q):
    q = input()
    if len(q) == 1:
        S.reverse()
    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=input()
n=int(input())
for i in range (n):
    q=input().split()
    #文字列のリストとして受け取る
    if q[0]=='1':
        s=s[::-1]
    else:
        if q[1]=='1':
            #Fi=1のとき
            s=q[2]+s
        elif q[1]=='2':
            s=s+q[2]
    print(s,i+1)
print(s)

Submission status: Runtime Error

Solution code:
S = input()
Q = int(input())
reverse = False
front = ""
end = ""

for i in range(Q):
    print(end[::-1]+S[::-1]+front)
    inp = input()
    if inp[0] == "1":
        reverse = ~reverse
    else:
        _, F, C = inp.split()
        if (F == "1" and reverse == False) or (F == "2" and reverse == True):
            front = front+C
        else:
            end = end+C

if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)

Submission status: Runtime Error

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

forb = 1

for i in range(q):
    order = str(input())
    if len(order) == 1:
        forb = forb * (-1)

    elif order[2] == '1':
        if forb == 1:
            s = order[4] + s
        else:
            s = s + order[4]
    else:
        if forb == 1:
            s = s + order[4]
        else:
            s = order[4] + s

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

Submission status: Runtime Error

Solution code:
import sys
import math
import collections
from collections import deque
#n = int(input())
#a, b = map(int, input().split())
s = input()
#x = list(map(int, input().split()))
q = int(input())
d = deque([])
for i in range(len(s)):
    d.append(s[i])

rev = 1
for i in range(q):
    que = list(map(int, input().split()))
    if que[0] == "1":
        rev *= -1

    else:
        if rev == 1:
            if que[1] == "1":
                d.appendleft(que[2])
            else:
                d.append(que[2])

        else:
            if que[1] == "1":
                d.append(que[2])
            else:
                d.appendleft(que[2])


if rev == 1:
    for i in range(len(d)):
        print(d.popleft(), end="")
else:
    for i in range(len(d)):
        print(d.pop(), end="")

Submission status: Runtime Error

Solution code:
S=[]
s=input()
S.append(s)
Q=int(input())
for i in range(Q):
    r=input()
    if len(r.split()) == 3:
        T,F,C=r.split()
    else:
        T='1'
    if T == '1':
        tmp=S[0]
        S[0]=S[len(S)-1]
        S[len(S)-1]=tmp
        continue
    if T == '2':
        if F == '1':
            #S.insert(0,C)
        if F == '2':
            S.append(C)

print("".join(S))

Submission status: Runtime Error

Solution code:
s=input()
q=int(input())
for i in range(q):
	a=input()
	if a[0]=="1":
		s=s[::-1]
	elif a[2]=="1":
		s=c+s
	else:
		s=s+c
print(s)

Submission status: Runtime Error

Solution code:
S = input().split()
N = int(input())
reverse = 0
r_b = []
r_f = []
for i in range(N):
    line = input().split()
    if line[i][0] == '1' :
        reverse = (reverse + 1) % 2
    else :
        if reverse == 0 :
            if line[i][1] == '1' :
                r_f = [line[i][2]] + r_f
            else :
                r_b.append(line[i][2])
        else:
            if line[i][1] == '1':
                r_b.append(line[i][2])
            else:
                r_f = [line[i][2]] + r_f

if reverse == 0 :
    S = r_f + S + r_b
    print("".join(S))
else:
    S.reverse()
    r_b.reverse()
    r_f.reverse()
    S = r_b + S + r_f
    print("".join(S))

Submission status: Runtime Error

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

Ques = []
nReverse = 0
nAddFront= 0
nAddBack = 0
for i in range(nQ):
  line = input().split()
  if int(line[0]) == 1:
    nReverse += 1
    Ques.append([int(line[0])])
  else:
    if int(line[1]) == 1:
      if nReverse % 2 == 0:
        nAddFront += 1
      else:
        nAddBack  += 1
    else:
      if nReverse % 2 == 0:
        nAddBack += 1
      else:
        nAddFront  += 1      
    Ques.append([int(line[0]),int(line[1]),line[2]])

#print(nAddFront,nAddBack)
SS = [-1] * (nAddFront + len(S) + nAddBack)
for i in range(len(S)):
   SS[nAddFront + i] = S[i]

left = nAddFront -1
right= nAddFront + len(S)

nReverseLoc = 0
for iQ in Ques:
   if iQ[0] == 1:
      nReverseLoc += 1
   if iQ[0] == 2:
    if int(iQ[1]) == 1:
      if nReverseLoc % 2 == 0:
        #nAddFront += 1
        SS[left] = iQ[2]
        left -= 1
      else:
        SS[right] = iQ[2]
        right += 1
    else:
      if nReverseLoc % 2 == 0:
        SS[right] = iQ[2]
        right += 1
      else:
        SS[left] = iQ[2]
        left  += 1 

S = "".join(SS)
if nReverse % 2 == 1:
  S = S[::-1]
  
print(S)

Submission status: Runtime Error

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

Ques = []
nReverse = 0
nAddFront= 0
nAddBack = 0
for i in range(nQ):
  line = input().split()
  if int(line[0]) == 1:
    nReverse += 1
    Ques.append([int(line[0])])
  else:
    if int(line[1]) == 1:
      if nReverse % 2 == 0:
        nAddFront += 1
      else:
        nAddBack  += 1
    else:
      if nReverse % 2 == 0:
        nAddBack += 1
      else:
        nAddFront  += 1      
    Ques.append([int(line[0]),int(line[1]),line[2]])

#print(nAddFront,nAddBack)
SS = [-1] * (nAddFront + len(S) + nAddBack)
for i in range(len(S)):
   SS[nAddFront + i] = S[i]

left = nAddFront -1
right= nAddFront + len(S)

nReverseLoc = 0
for iQ in Ques:
   print(SS)
   if iQ[0] == 1:
      nReverseLoc += 1
   if iQ[0] == 2:
    if int(iQ[1]) == 1:
      if nReverseLoc % 2 == 0:
        #nAddFront += 1
        SS[left] = iQ[2]
        left -= 1
      else:
        SS[right] = iQ[2]
        right += 1
    else:
      if nReverseLoc % 2 == 0:
        SS[right] = iQ[2]
        right += 1
      else:
        SS[left] = iQ[2]
        left  += 1 

S = "".join(SS)
if nReverse % 2 == 1:
  S = S[::-1]
  
print(S)

Submission status: Runtime Error

Solution code:
s = input()
n = int(input())
q = [input().split() for l in range(n)]
for i in range(n):
  if q[i][0] == '1':
    s = s.reverse()
  elif q[i][0] == '2':
    if q[i][1] == '1':
      s = q[i][2] + s
    elif q[i][1] == '2':
      s = s + q[i][2]
print(s)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def main():
    s = list(input())
    q = int(input())
    s.pop(-1)
    a = [''] * (10**5*2)
    a.append(*s)
    a.extend([''] * (10**5*2))
    start = 10**5*2
    end = start + len(s) - 1
    for i in range(q):
        query = input().split()
        if query[0] == '1':
            tmp = a[start]
            a[start] = a[end]
            a[end] = tmp
        elif query[0] == '2':
            if query[1] == '1':
                a[start-1] = query[2]
                start -= 1
            elif query[1] == '2':
                a[end+1] = query[2]
                end += 1
    print(''.join(a[start:end+1]))


main()

Submission status: Runtime Error

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

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

print(S)

Submission status: Runtime Error

Solution code:
from collections import deque
def main():
    S = input()
    S = deque([c for c in S])
    Q = int(input())
    rvCt = 0

    head, tail = deque([]), deque([])
    for _ in range(Q):
        query = input().split()
        if query[0] == '1':
            rvCt += 1
        else:
            if rvCt % 2 == 1:
                S = head + S + tail
                S.reverse()
                head, tail = deque([]), deque([])
            rvCt = 0
            if query[1] == '1':
                head.appendleft(query[2])
            else:
                tail.append(query[2])

    if rvCt % 2 == 1:
        S = head + S + tail
        S.reverse()
    else:
        S = head + S + tail
    
    print(''.join(S))

main()

Submission status: Runtime Error

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

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

print(S)

Submission status: Runtime Error

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

S = input()
Q = int(input())
A = [[input().split()] for _ in range(Q)]

for i in range(Q):
  if A[i] == ["1"]:
    S = ''.join(list(reversed(S)))
  else:
    if A[i][1] == "1":
      S = S + A[i][2]
    else:
      S = A[i][2] + S
      
print(S)