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: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
reverse = False
for i in range(Q):
    query = list(map(str,input().split()))
    if query[0] == '1':
        if reverse == True:
            reverse = False
        else:
            reverse = True
    if query[0] == '2':
        if query[1] == '1':
            if reverse == False:
                S = query[2] + S
            else:
                S = S + query[2]
        else:
            if reverse == True:
                S = query[2] + S
            else:
                S = S + query[2]
if reverse == True:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

flag = True
ans = s
for i in range(q):
  query = list(input().split())
  if int(query[0]) == 1:
    flag = not flag
  else:
    if int(query[1]) == 1:
      if flag:
        ans = query[2] + ans
      else:
        ans+=query[2]
    else:
      if flag:
        ans+=query[2]
      else:
        ans = query[2] + ans

if flag:
  for i in ans:
    print(i, end="")
else:
  t = ans[::-1]
  for i in t:
    print(i, end="")

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
rev = 0
for i in range(Q):
  query = input()
  if query == "1": rev += 1
  elif query[2] == "1" and rev % 2 == 0 or query[2] == "2" and rev % 2 == 1: S = query[4] + S
  else: S = S + query[4]
    
if rev % 2 == 1: S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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 = sys.stdin.readline().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[i], end="")
else:
    for i in range(len(d)):
        print(d[len(d) - i - 1], end="")

Submission status: Time Limit Exceeded

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

qs = list()
for i in range(Q):
    qs.append(input().split())

ns = s
# for i in range(Q):
    # q = input().split()
for q in qs:
    if len(q) == 1:
        ns = ns[::-1]
    else:
        f, c = int(q[1]), q[2]
        if f == 1:
            ns = c + ns
        else:
            ns = ns + c

print(ns)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
for i in range(Q):
    Query = list(map(str,input().split()))
    if Query[0] == '1':
        S = S[::-1] # 反転
    else: # T = '2'
        if Query[1] == '1':
            S = Query[2] + S
        else: # F = '2'
            S += Query[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = deque([x for x in input()])
Q = int(input())
for _ in range(Q):
    inp = input()
    if len(inp) == 1:
        T = int(inp)
        S = deque(list(S)[::-1])
    else:
        T, F = map(int, inp.split()[:2])
        C = inp.split()[-1]
        if F == 1:
            S.appendleft(C)
        else:
            S.append(C)

print("".join(S))

Submission status: Time Limit Exceeded

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

for i in range(q):
    query = input()
    if query == '1':
        str = str[::-1]
    else:
        two, f, c = query.split()
        if f == '1':
            str = c + str
        else:
            str = str + c

print(str)

Submission status: Time Limit Exceeded

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

for _ in range(q):
  query = list(input().split())
  if len(query) == 1:
    #反転
    ans = ''.join(list(reversed(ans)))
  else:
    if int(query[1]) == 1:
      # 先頭
      ans = query[2] + ans
    else:
      # 後方
      ans = ans + query[2]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = input()
q = int(input())

queries = [input().split() for i in range(q)]

for query in queries:
    if "1" == query[0]:
        s = s[::-1]
    elif "2" == query[0]:
        if "1" == query[1]:
            s = query[2] + s
        elif "2" == query[1]:
            s += query[2]

print(s)

Submission status: Time Limit Exceeded

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 c in list:
    if len(c)==1:
        mojiretu.reverse()
    else:
        if c[1]=='1':
            mojiretu.insert(0,c[2])
        else:
            mojiretu.append(c[2])
moji=''
for z in mojiretu:
    moji+=z
print(moji)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    S = list(input())
    Q = int(input())
    Query = [input().split() for _ in range(Q)]

    for q in Query:
        if q[0] == '1':
            S = S[::-1]
        else:
            if q[1] == '1':
                S.insert(0, q[2])
            else:
                S.append(q[2])

    print(''.join(S))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

rev = 0

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        rev += 1
    else:
        if (t[1] == "1" and rev % 2 == 0) or (t[1] == "2" and rev % 2 != 0):
            tmp = [t[2],s]
            s = "".join(tmp)
        else:
            tmp = [s,t[2]]
            s = "".join(tmp)

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

Submission status: Time Limit Exceeded

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

r = False
for i in range(Q):
    x = input().split()
    
    if x[0] == "1":
        r = not r
        
    else:
        if x[1] == "1":
            if r == False :
                S.insert(0, x[2])
            else:
                S.append(x[2])
        else:
            if r == True :
                S.insert(0, x[2])
            else:
                S.append(x[2])
        
if r:
    print("".join(S[::-1]))
else:
    print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

# -*- coding: utf-8 -*-
S = input()
Q = int(input())

rev = False

for q in range(Q):
    i = input().split()
    if i[0] == "1":
        rev = not rev
    elif not rev and i[1] == "1" or rev and i[1] == "2":
        S = i[2] + S
    else:
        S = S + i[2]

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

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

s = input()
s = s[0:len(s)-1]
Q = int(input())
lq = [input().split() for _ in range(Q)]

flg = 0 # 0:sonomama 1:gyakuten
for q in lq:
    if q[0] == "1":
        if flg == 0:
            flg = 1
        else:
            flg = 0
    elif q[0]=="2":
        tmp = q[2]
        if (q[1] == "1" and flg ==0) or (q[1] == "2" and flg ==1):
            tmp += s
            s = tmp
        elif (q[1] == "2" and flg == 0) or (q[1] == "1" and flg == 1):
            s += tmp

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

Submission status: Time Limit Exceeded

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

h = 0
for i in range(n):
    qq = list(input().split())
    if qq[0] == '1':
        h += 1
    else:
        if h % 2 ==1:
            s = s[::-1]
            h = 0
        if qq[1] == '1':
            s = qq[2] + s
        else:
            s = s + qq[2]

if h % 2 ==1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

lis = []
for i in range(q):
	lis.append(input().split())



for i in range(q):
	tmp = lis[i]
	if tmp[0] == '1':
		s = s[::-1] 
	else:
		if tmp[1] == '1':
			s = tmp[2] + s
		else:
			s = s + tmp[2]


print(s)

Submission status: Time Limit Exceeded

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

flag = 0
for i in range(n):
    q = list(map(str,input().split()))
    if len(q) == 1:
        flag = 1 - flag 
    elif q[1] == "1":
        if flag == 0:
            s = q[2] + s
        else:
            s = s + q[2]
    else:
        if flag == 0:
            s = s + q[2]
        else:
            s = q[2] + s


if flag == 0:
    print(s)
elif flag == 1:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
# Problem D - String Formation

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

# initialization
sento = 1

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

# output process
if sento==1:
    for s in S:
        print(s, end="")
else:
    for s in S[::-1]:
        print(s, end="")

Submission status: Time Limit Exceeded

Solution code:
s = input()

flag = 0
#print(len(s))
if(len(s) > 1):
    flag = 1
    s_save = s[1:-1]
    s = s[0]+"123"+s[-1]
 
n = int(input())

for i in range(n):
    que = input()
    if(len(que) == 1):
        s = s[::-1]
    else:
        que = que.split()
        if(que[1]=="1"):
            s = que[2] + s
        else:
            s = s + que[2]


if(flag == 0):
    print(s)
else:
    if("123" in s):
        print(s.replace("123",s_save))
    else:
        print(s.replace("321",s_save[::-1]))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
isReverse = -1
for i in range(q):
    query = input().split()
    if(query[0] == "1"):
        isReverse *= -1
    else:
        if(query[1] == "1"):
            if(isReverse == 1):
                s = "".join([s, query[2]])
            else:
                s = "".join([query[2], s])
        else:
            if(isReverse == 1):
                s = "".join([query[2], s])
            else:
                s = "".join([s, query[2]])
if(isReverse == -1):
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

li_q = []
for _ in range(query):
    li_q.append(input().split())

ans = s
for q in li_q:
    if q[0] == '1':
        ans = ans[::-1]
    else:
        if q[1] == '1':
            ans = q[2] + ans
        else:
            ans = ans + q[2]

print(ans)

Submission status: Time Limit Exceeded

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

rev = False
for i in range(Q):
  q = input()
  if q[0] == '1':
    rev = not rev
  else:
    if q[2] == '1' and rev or q[2] == '2' and not rev:
      S = S + q[4]
    else:
      S = q[4] + S
      
print(S[::-1] if rev else S)

Submission status: Time Limit Exceeded

Solution code:
import math

S = input().strip()
Q = int(input().strip())
order = 1

for i in range(Q):
  q = input().strip().split()
  #print(q)
  if q[0] == '1':
    # 反転
    # S = S[::-1]
    order *= -1
  else:
    F = q[1]
    if F == '1':
      if order == 1:
        # 先頭にCを追加
        S = q[2] + S
      else:
        # 末尾にCを追加
        S = S + q[2]
    else:
      if order == 1:
        # 末尾にCを追加
        S = S + q[2]        
      else:
        # 先頭にCを追加
        S = q[2] + S
        
print(S[::order])

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
len_queries = int(input())
queries = [input().split() for _ in range(len_queries)]

is_forward = True
for query in queries:
    if "1" == query[0]:
        is_forward = not is_forward
    else:
        if is_forward:
            insert_index = 0 if "1" == query[1] else len(s)
            s.insert(insert_index, query[2])
        else:
            insert_index = len(s) if "1" == query[1] else 0
            s.insert(insert_index, query[2])
if not is_forward:
    s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
n = int(input())
s = []
for i in range(n):
    array = list(map(str,input().split()))
    s.append(array)
for i in range(n):
    if s[i][0] == "1":
        S.reverse()
    else:
        if s[i][1] == "1":
            S.insert(0,s[i][2])
        else:
            S.append(s[i][2])

print("".join(S))

Submission status: Time Limit Exceeded

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

res = S

is_rev = False

def add_char(s, f, c, is_rev):
  if(f == "1"):
    return c + s if not is_rev else s + c
  else:
    return s + c if not is_rev else c + s

for _ in range(Q):
  q = input().split()
  
  if(q[0] == '1'): # Ti = 1
    is_rev = not is_rev
  
  else: #Ti = 2
    res = add_char(res, q[1], q[2], is_rev)


print(res[::-1] if is_rev else res)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
s = deque(S)
Q = int(input())
for _ in range(Q):
    q = input()
    if q == "1":
        s.reverse()
    else:
        _, F, C = q.split()
        if F == "1":
            s.appendleft(C)
        else:
            s.append(C)
print("".join(s))

Submission status: Time Limit Exceeded

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:
        s = s[::-1]
    else:
        if int(query[i][1]) == 1:
            s = query[i][2] + s
        else:
            s += query[i][2]
            
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flg = 0
for _ in range(Q):
    s = input().split()
    if len(s) == 1:
        flg += 1
    else:
        if flg % 2 == 1:
            S = S[::-1]
            flg = 0
        if s[1] == "1":
            S = s[2] + S
        else:
            S = S + s[2]
if flg % 2 == 1:
    S = S[::-1]
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = list(input())
q = int(input())
ans = deque(s)
for i in range(q):
    arr = list(map(str, input().split()))
    if arr[0] == '1':
        ans.reverse()
    else:
        if arr[1] == '1':
            ans.appendleft(arr[2])
        else:
            ans.append(arr[2])
print(*ans, sep='')

Submission status: Time Limit Exceeded

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

for _ in range(q):
  query = input()
  if query[0] == '1':
    s = s[::-1]
  else:
    a,b = query[2],query[4]
    if a == '1':
      s = ''.join([b,s])
    elif a == '2':
      s = ''.join([s,b])

print(s)

Submission status: Time Limit Exceeded

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


for h in range(Q):
  q.append(input().split())
  
  
for i in range(Q):
  if q[i][0] == "1":
    S = S[::-1]
  
  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: Time Limit Exceeded

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


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

print(s)

Submission status: Time Limit Exceeded

Solution code:
first_step_string = input()
query_num = int(input())
query_li = list()
for _ in range(query_num):
    query_li.append(input().split())

ans = first_step_string
rev_flag = False
for query in query_li:
    if query[0] == '1':
        rev_flag = not rev_flag
    elif (query[1] == '1' and rev_flag) or (query[1] == '2' and not rev_flag):
        ans += query[2]
    else:
        ans = query[2] + ans

if rev_flag:
    print(ans[::-1])
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flag = 1
for _ in range(Q):
    query = input()
    if len(query) == 1:
        #S = S[::-1]
        flag = flag ^ 1
    else:
        F, C = int(query[2]), query[4]
        if F == 1:
            S = [S+C, C+S][flag]
            #S = C + S
        else:
            S = [C+S, S+C][flag]
            #S = S + C
print([S[::-1], S][flag])

Submission status: Time Limit Exceeded

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

for query in queries:
    if query == "1":
        S = S[::-1]
    else:
        _, F, C = list(query.split())
        if F == "1":
            S = C + S
        else:
            S = S + C

print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
    t = input().split()
    if t[0] == '1':
        s = s[::-1]
    else:
        if t[1] == '1':
            s = t[2] + s
        else:
            s += t[2]

print(s)

Submission status: Time Limit Exceeded

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

s = list(s)
# s = s[::-1]
for _ in range(q):
    q = input()
    if q[0] == '1':
        s = s[::-1]
    else:
        q = q.split(' ')
        if q[1] == '1':
            s.insert(0,q[-1])
        else:
            s.extend(q[-1])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s=input()
from sys import stdin
input = stdin.readline
q=int(input())
cnt=0
for i in range(q):
    j=list(input().split())
    if  int(j[0])!=1 :
        if (cnt+int(j[1]))%2==1:
            s=j[2]+s
        else:
            s=s+j[2]
    else:
        cnt+=1
if cnt%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

for _ in range(Q):
    q=input().split()
    if int(q[0])==1:
        f = (f+1)%2
    if int(q[0])==2:
        if int(q[1])==1:
            if f==0:
                S="".join([q[2],S])
            else:
                S="".join([S,q[2]])
        else:
            if f==0:
                S="".join([S,q[2]])
            else:
                S="".join([q[2],S])
if f==0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
for i in range(n):
    qs = input()
    if len(qs) == 1:
        s = s[::-1]
    else:
        if int(qs[1:-2]) == 1:
            s = qs[-1] + s
        else:
            s = s + qs[-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for _ in range(Q):
    query = list(input().split())
    if query[0] == "1":
        S.reverse()
    if query[0] == "2":
        if query[1] == "1":
            S.insert(0,query[2])
        elif query[1] == "2":
            S.append(query[2])
ans = "".join(S)
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
    q = input()
    if q[0] == "1":
        a = a[::-1]
    else:
        if q[2] == "1":
            a = q[4] + a
        else:
            a = a + q[4]
            
print(a)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

d = deque(['S'])

#d = ['S']

rev = 0

for i in range(q):
    if query[i][0] == '2':
        if query[i][1] == '1':
            d.appendleft(query[i][2])
        else:
            d.append(query[i][2])
    else:
        d.reverse()
        rev += 1

ans = []

if rev % 2 == 1:
    s = ''.join(list(reversed(s)))


for i in range(len(d)):
    if d[i] == 'S':
        ans.append(s)
    else:
        ans.append(d[i])

print(*ans,sep='')

Submission status: Time Limit Exceeded

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

reverse = 1 #reverse == 1 -> 正常, reverse == -1 -> 逆から
for i in range(Q):
    Query_tmp = list(map(str, input().split()))
    
    if Query_tmp[0] == "1":
        reverse *= -1
        # S = S[::-1]
    else:
        Ci = Query_tmp[2]

        if Query_tmp[1] == "1":
            if reverse == 1:
                S = Ci + S
            else:
                rev_Ci = Ci[::-1]
                S = S + rev_Ci
        else:
            if reverse == 1:
                S = S + Ci
            else:
                rev_Ci = Ci[::-1]
                S = rev_Ci + S
    
    # if reverse == 1:
    #     print(S)
    # else:
    #     print(S[::-1])

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
for i in range(n):
    a = input().split()
    if len(a) == 1:
        s = s[::-1]
    else:
        t, c = int(a[1]),a[2]
        if t == 1:
            s = c+s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
z = 0
F = ""
R = ""
for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        z += 1
        z %= 2
    else:
        if (int(a[1]) + z) % 2 == 0:
            R += a[2]
        else:
            F = a[2] + F
ans = F + s + R
if z % 2 == 0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

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

    for query_list in query:
        if query_list[0] == '1':
            s = s[::-1]
        else:
            if query_list[1] == '1':
                s = ''.join([query_list[2], s])
            else:
                s = ''.join([s, query_list[2]])

    print(s)

main()

Submission status: Time Limit Exceeded

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

s = list(input())
s.pop(-1)
q = int(input())

r = 0
for i in range(q):
  tfc = input().split()
  if tfc[0] == '1':
    r += 1
    #s.reverse()
    #s = s[::-1]
  else:
    if tfc[1] == '1':
      if r % 2 == 0:
        s.insert(0, tfc[2])
      else:
        s.append(tfc[2])
    if tfc[1] == '2':
      if r % 2 == 1:
        s.insert(0, tfc[2])
      else:
        s.append(tfc[2])          
if r % 2 == 0:
  for i in s:
      print(i, end='')
else:
  for i in (reversed(s)):
    print(i, end='')

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().strip()
s=str(input())
q=int(input())
flag=0
for i in range(q):
    j=input()
    if j[0]=="1":
        flag+=1
    else:
        if flag%2==1:
            if j[2]=="1":
                s+=j[4]
            else:
                s=j[4]+s
        else:
            if j[2]=="1":
                s=j[4]+s
            else:
                s+=j[4]
if flag%2==1:
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3


def main():
    # Failed to predict input format
    S = input()
    Q = int(input())
    Queries = []
    for i in range(Q):
        q = input().split()
        if len(q) == 1:
            Queries.append(int(q[0]))
        else:
            Queries.append([int(q[0]), int(q[1]), q[2]])

    cumsum_Queries = [0]
    for i in range(1, Q):
        if Queries[i] == 1:
            cumsum_Queries.append(cumsum_Queries[i-1] + 1)
        else:
            cumsum_Queries.append(cumsum_Queries[i-1])
    cumsum_Queries = [cumsum_Queries[-1] - cumsum_Queries[i] for i in range(Q)]

    if cumsum_Queries[0] % 2 == 0:
        S = S
    else:
        S = S[::-1]

    for i in range(Q):
        if Queries[i] == 1:
            continue
        else:
            if Queries[i][1] == 1:
                if cumsum_Queries[i] % 2 == 0:
                    S = Queries[i][2] + S
                else:
                    S = S + Queries[i][2][::-1]
            else:
                if cumsum_Queries[i] % 2 == 0:
                    S = S + Queries[i][2]
                else:
                    S = Queries[i][2][::-1] + S
    print(S)

    pass



# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main_slow():
    # Failed to predict input format
    S = input()
    Q = int(input())
    Queries = []
    for i in range(Q):
        q = input().split()
        if len(q) == 1:
            Queries.append(int(q[0]))
        else:
            Queries.append([int(q[0]), int(q[1]), q[2]])
    for i in range(Q):
        if Queries[i] == 1:
            S = S[::-1]
        else:
            if Queries[i][1] == 1:
                S = Queries[i][2] + S
            elif Queries[i][1] == 2:
                S = S + Queries[i][2]
    print(S)
    pass

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
#query = [input().split() for _ in range(Q)]

rev = False
forw = ""
back = ""
for i in range(Q):
  query = input().split()
  if query[0] == "1":
    rev = not rev
  else:
    if (not rev and query[1] == "1") or (rev and query[1] == "2"):
      forw = query[2] + forw
    else:
      back += query[2]
#S = forw + S + back
if rev:
  S = back[::-1] + S[::-1] + forw[::-1]
else:
  S = forw + S + back
print(S)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    tmp = list(map(str,input().split()))
    if len(tmp) == 1:
        q.append(int(tmp[0]))
        f.append(0)
        c.append('')
    else:
        q.append(int(tmp[0]))
        f.append(int(tmp[1]))
        c.append(tmp[2])
s2 = s[::-1]
for i in range(Q):
    if q[i] == 1:
        tmp = s
        s = s2
        s2 = tmp
    else:
        if f[i] == 1:
            s = c[i] + s
            s2 = s2 + c[i]
        else:
            s = s + c[i]
            s2 = c[i] + s2

print(s)

Submission status: Time Limit Exceeded

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



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for operation in qs:
    if operation[0] == '1':
        s = s[::-1]
    elif operation[0] == '2':
        t , f, c = operation
        if f == '1':
            s = c+s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
S = input()
Q = int(input())

#T1 反転
def reverse(str):
  return str[::-1]

#T2 追加
def addition(str, f, c):
  if int(f) == 1:
    return c + str
  if int(f) == 2:
    return str + c

#solve
ans = S
for i in range(Q):
  Query = input().split()
  if int(Query[0]) == 1:
    ans = reverse(ans)
  if int(Query[0]) == 2:
    ans = addition(ans, Query[1], Query[2])
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    k = input()
    if k == "1":
        S = list(reversed(S))
    else:
        if k[2] == "1":
            S = [k[4]] + S
        else:
            S = S + [k[4]]

print(*S, sep="")

Submission status: Time Limit Exceeded

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

for i in range(Q):
    cmd = raw_input().split()
    if cmd[0] == '1':
        S.reverse()
    else: # cmd[0] == 2
        if cmd[1] == '1':
            S.insert(0, cmd[2])
        else:
            S.append(cmd[2])

print ''.join(S)