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 = list(input())
n = int(input())
sl = []
fl = []
flag = True
for _ in range(n):
    q = list(map(str, input().split()))
    if len(q) == 1:
        if flag:
            flag = False
        else:
            flag = True
    if len(q) == 3:
        if q[1] == "1":
            if flag:
                s = list(q[2])+s
            else:
                s = s+list(q[2])
        else:
            if flag:
                s = s+list(q[2])
            else:
                s = list(q[2])+s
if flag == False:
    s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

ans = deque(s)

for t in ls:
    if t[0] == '1':
        ans.reverse()
    elif t[0] == '2':
        if t[1] == '1':
            ans.appendleft(t[2])
        elif t[1] == '2':
            ans.append(t[2])

l = list(ans)

print("".join(ans))

Submission status: Time Limit Exceeded

Solution code:


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

def addflont(s,c):
    text = "".join([c,s])
    return text
def addback(s,c):
    text = "".join([s,c])
    return text

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 = addflont(s,t[4])
            else:
                s = addback(s,t[4])
        elif t[2]=="2":
            if reverse==0:
                s = addback(s,t[4])
            else:
                s = addflont(s,t[4])

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

Submission status: Time Limit Exceeded

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

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

k= 0

for i in li:
    if i == "1":
        k += 1
    else:
        
        if (int(i[2])+k) % 2 == 0:
            s = s + i[4]
        else:
            s = i[4] + s

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

Submission status: Time Limit Exceeded

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


def formation():
    global S, Q

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

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

        #print(ls)

    return ''.join(ls)


print(formation())

Submission status: Time Limit Exceeded

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

A = []
B = []
count = 0

Que = []
ope = []

for i in range(Q):
    k = input()
    if k == "1":
        count += 1
        Que.append("1")
        ope.append("0")
    else:
        Que.append("2")
        ope.append([k[2],k[4]])

cou = 0

if count % 2 == 0:
    for i in range(Q):
        if Que[i] == "1":
            cou += 1
        else:
            if (int(ope[i][0]) + cou) % 2 == 1:
                A = [ope[i][1]] + A
            else:
                B = B + [ope[i][1]]
else:
    for i in range(Q):
        if Que[i] == "1":
            cou += 1
        else:
            if (int(ope[i][0]) + cou) % 2 == 1:
                A = A + [ope[i][1]]
            else:
                B = [ope[i][1]] + B

if count % 2 == 0:
    S = A + S + B
else:
    S.reverse()
    S = B + S + A

print(*S, sep="")

Submission status: Time Limit Exceeded

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

roll = False
ans = [s]
preNum = 0

for i in range(q) :
    inp = input()
    if inp[:1] == '1' :
        roll = not roll
        #print(roll)
    else :
        t, f, c = inp.split()
        #print(ans)
        #print(t, f, c)
        if (f == '1' and (not roll)) or (f == '2' and roll) :
            ans = ans[::-1]
            #print(ans, 'rev')
            ans.append(c)
            #print(ans, '+C')
            ans = ans[::-1]
            #print(ans, 'ver')
            preNum += 1
        else :
            ans.append(c)
#print(ans)
if roll :
    ans[preNum] = ans [preNum][::-1]
    ans = ans[::-1]

#print(ans)
#print("".join(ans))
for s in ans :
    print(s, end='')

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

if rev % 2 == 1:
    s = ''.join(list(reversed(s)))
"""
while len(d):
    tmp = d.popleft()
    if tmp == 'S':
        ans.append(s)
    else:
        ans.append(tmp)
"""
d = [s if i == 'S' else i for i in d]

print(*d,sep='')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
sousa = []
for i in range(Q):
  sousa.append(input().split())

rev = 0
for j in range(Q):
  if sousa[j][0] == '1':
    rev += 1
  else:
    if rev%2 == 1:
      S.reverse()
    rev = 0
    
    if sousa[j][1] == '1':
      S.appendleft(sousa[j][2])
    else:
      S.append(sousa[j][2])
if rev%2 == 1:
  S.reverse()
  
Ans = ''.join(list(S))
print(Ans)

Submission status: Time Limit Exceeded

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

for i in range(0, q):
    t=raw_input()
    if t[0] == "1":
        s = s[::-1]
    else:
        if t[2] == "1":
            s = t[4] + s
        else:
            s = s + t[4]

print s

Submission status: Time Limit Exceeded

Solution code:
ans = input()
odr = 0
 
N = int(input())
 
for _ in range(N):
    Q = input()
 
    if Q == '1':
        ans_len = len(ans)
        
        kaibun = True
        for i in range(ans_len // 2) :
            if (ans[i] != ans[-(i+1)]):
                kaibun = False
                break
                
        if kaibun:
            continue
            
        if odr == 0:
            odr = 1
        else:
            odr = 0
            
        continue
    
    A, F, C = Q.split()
    if (odr == 0 and F == '1') or (odr == 1 and F == '2'):
        ans = C + ans
    else:
        ans = ans + C

if odr == 1:
    print(ans[::-1])
else:
    print(ans)

Submission status: Time Limit Exceeded

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

    is_true = True
    for _ in range(q):
        input_list = list(input().split())
        if input_list[0] == '1':
            if is_true:
                is_true = False
            else:
                is_true = True
            continue
        if input_list[1] == '1':
            if is_true:
                t = [input_list[2], s]
                s = ''.join(t)
            else:
                t = [s, input_list[2]]
                s = ''.join(t)
        else:
            if is_true:
                t = [s, input_list[2]]
                s = ''.join(t)
            else:
                t = [input_list[2], s]
                s = ''.join(t)
            
    if not is_true:
        s = s[::-1]

    print(s)

if __name__ == '__main__':
    main()

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]
  if query[0] == "2":
    if query[2] == "1":
      s = query[4] + s
    if query[2] == "2":
      s = s + query[4]
      
print(s)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

for i in range(Q):
    query = input()

    if query == '1':
        S.reverse()
    else:
        T, F, C = query.split()
        
        if F == '1':
            S.insert(0, C)
        else:
            S.append(C)

print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
#A,B = map(int,input().split())

a = input()
n = int(input())
mode = 1

i = 0
while i < n:
    hoge = input().split()
    if hoge[0] == '1':
        mode = mode * -1
    else:
        if mode == 1:
            if hoge[1] == '1':
                a = hoge[2] + a
            else:
                a = a + hoge[2]
        else:
            if hoge[1] == '2':
                a = hoge[2] + a
            else:
                a = a + hoge[2]
    i += 1

if mode == -1:
    a = a[::-1]

print(a)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math

s = stdin.readline().rstrip()
n = int(stdin.readline().rstrip())

flag = True
for i in range(n):
    q = stdin.readline().rstrip().split()
    if int(q[0]) == 1:
        if flag:
            flag = False
        else:
            flag = True
    if int(q[0]) == 2:
        if int(q[1]) == 1:
            if flag:
                s = q[2] + s
            else:
                s = s + q[2]
        if int(q[1]) == 2:
            if flag:
                s = s + q[2]
            else:
                s = q[2] + s

if flag:   
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

#一番最後に1度だけ反転すればよい
reverse_count = 0
for i in range(q):
  y = input().split()
  if y[0] == '1':
    #反転は最後に行う
    reverse_count += 1
    continue
  elif y[0] == '2':
    if y[1] == '1':
      #文字列の先頭に追加。反転回数が奇数なら、末尾に追加と同義
      if reverse_count % 2 == 1:
        s = s + [y[2]]
      else:
        s = [y[2]] + s
    elif y[1] == '2':
      #文字列の末尾に追加。反転回数が奇数なら、先頭に追加と同義
      if reverse_count % 2 == 1:
        s = [y[2]] + s
      else:
        s = s + [y[2]]

#最後に一度だけ反転させる
if reverse_count % 2 == 0:
  #元の順序に戻るので、そのまま
  print("".join(s))
else:
  s.reverse()
  print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

mae = ""
ushiro = ""
for i in range(q):
    query = list(map(str,input().split()))
    if int(query[0]) == 1 :
        mae, ushiro  = ushiro, mae
        mae = mae[::-1]
        ushiro = ushiro[::-1]
        s = s[::-1]
        continue
    else:
        if int(query[1]) == 1:
            mae = query[2] +mae 
        else:
            ushiro = ushiro + query[2]
print(mae + s  + ushiro)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

Submission status: Time Limit Exceeded

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

q_list = []

rev_c = 0
count = 0

for _ in range(q):
  v = input()
  q_list.append(v)
  if v == '1':
    rev_c += 1

if rev_c % 2 == 1:
  s = s[::-1]
  
for v in q_list:
  if v == '1':
    count += 1
  else:
    num = v[2]
    char = v[4]
    if ((rev_c-count)%2==0 and num=='1') or  ((rev_c-count)%2==1 and num == '2'):
      s = char + s
    else:
      s = s + char
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

d = deque(input())
Q = int(input())


for _ in range(Q):
    i = input()
    if len(i) >= 2:
        i, F, C = i.split()
    if int(i) == 1:
        d.reverse()
    else:
        if int(F) == 1:
            d.appendleft(C)
        else:
            d.append(C)

for _ in range(len(d)):
    print(d.popleft(), end='')
print()

Submission status: Time Limit Exceeded

Solution code:
str = input()
num = int(input())
for i in range(num):
  l = list(input().split())
  if l[0] == "1":
    str = str[::-1]
  elif l[0] == "2":
    if l[1] == "1":
      str = l[2] + str
    elif l[1] == "2":
      str = str + l[2]
print(str)

Submission status: Time Limit Exceeded

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


for i in range(Q):
  if len(q[i]) == 1:
    S = S[::-1]
    continue
  else:
    if int(q[i][1]) == 1:
      S = q[i][2] + S
      continue
    else: 
      S += q[i][2]
      continue
K = S
print(K)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = []
 
def func(string, t, f, c):
    if int(t) == 1:
        new_str = ''.join(list(reversed(string)))
        return new_str
    else:
        if int(f) == 1:
            return c + string
        else:
            return string + c
 
for i in range(0,q):
    query.append(input().split())
for i in query:
    if int(i[0]) == 1:
        s = func(s,i[0],'','')
    else:
        s = func(s,i[0],i[1],i[2])
print(s)

Submission status: Time Limit Exceeded

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

result = S
for query in Q_queries:
    if query == '1':
        result = result[::-1]
    else:
        if query.split(' ')[1] == '1': # 先頭
            result = query.split(' ')[2] + result
        else: # 末端
            result = result + query.split(' ')[2]
print(result)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
R = []
T = []
for i in range(Q):
    T.append(input())
for i in range(Q - 1):
    if T[i] == T[i + 1] == 1:
        del T[i:i+1]
for i in T:
    if i == '1':
        S = S[::-1]
    else:
        X = i.split()
        if int(X[1]) == 1:
            S.insert(0,X[2])
        if int(X[1]) == 2:
            S.append(X[2])
print(''.join(S))

Submission status: Time Limit Exceeded

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

    rev = 0
    for i in range(Q):
        if q[i][0] == "1":
            rev += 1
        else:
            if (q[i][1] == "1" and rev%2 == 0) or (q[i][1] == "2" and rev%2 == 1):
                S = q[i][2] + S
            else:
                S = S + q[i][2]

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
newQuery = [[]]
querys = [input().split() for i in range(Q)]
rev_cnt=0
for i in range(Q):
    for j in reversed(range(i+1, Q)):
        if(querys[j-1][0] < querys[j][0]):
            if(querys[j][1] == '2'):
                querys[j][1] = '1'
            else:
                querys[j][1] = '2'
            querys[j-1], querys[j] = querys[j], querys[j-1]
    if(querys[i][0]=='1'):
        rev_cnt += 1

for i in range(Q-rev_cnt):
    query = querys[i]
    if(query[1] == '1'):
        S = query[2] + S
    else:
        S = S + query[2]
if(rev_cnt%2 == 1):
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s1 = input()
s2 = s1[::-1]
q = int(input())
x = 0
for i in range(q):
    query = input().split()
    if query[0] == '1':
        x += 1
    else :
        y = x + int(query[1])
        if y % 2 == 0:
            s1 = s1 + query[2]
            s2 = query[2] + s2
        else :
            s1 = query[2] + s1
            s2 = s2 + query[2]
if x % 2 == 0:
    print(s1)
else :
    print(s2)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	SS = input()
	SS = SS[:-1]
	Q = inputInt()

	mae = ""
	usiro = ""

	flg = True
	for _ in range(Q):
		inp = input()
		inp = inp[:-1]
		inpt = inp.split()

		if len(inpt) == 1:
			if flg == True:
				flg = False
			else:
				flg = True
		else:
			oder = inpt[1]
			if oder == "1":
				if flg == True:
					mae = inpt[2] + mae
				else:
					usiro = usiro + inpt[2]
			else:
				if flg == True:
					usiro = usiro + inpt[2]
				else:
					mae = inpt[2] + mae

	#print(S)
	#S = "".join(S)
	S = mae + SS + usiro
	if flg == True:
		print(S)
	else:
		print(S[::-1])


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
ans = S
reverse = False

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse = not reverse
    if query[0] == "2":
        F, C = query[1:]
        if (F == "1" and not reverse) or (F == "2" and reverse):
            ans = C + ans
        else:
            ans = ans + C

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    from sys import stdin
    S = input()
    Q = int(input())
    reverse = 0
    head = []
    tail = []
    
    for _ in [0] * Q:
        readline = stdin.readline
        s = readline().split()
        if int(s[0]) == 1:
            if reverse == 1:
                reverse = 0
            else:
                reverse = 1
        else:
            if int(s[1]) == 1:
                if reverse == 1:
                    tail.append(s[2])
                else:
                    head.insert(0, s[2])
            else:
                if reverse == 1:
                    head.insert(0, s[2])
                else:
                    tail.append(s[2])
    head = ''.join(head)
    tail = ''.join(tail)
    if reverse == 1:
        print(tail[::-1]+S[::-1]+head[::-1])
    else:
        print(head+S+tail)
        
main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
for i in range(int(input())):
    query = input()
    s = s[::-1] if query[0] == "1" else  query[-1] + s if query[2] == "1" else s + query[-1]

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
r_count = 0
for query in range(Q):
  query = list(input().split())
  t = query[0]
  if (t == '1'):
    r_count += 1
  elif (t == '2'):
    f = query[1]
    new_S = ""
    if(f == '1' and (r_count % 2 == 0)):
      new_S = query[2] + S
    elif(f == '1' and (r_count % 2 == 1)):
      new_S = S + query[2]
    elif(f == '2' and (r_count % 2 == 0)):
      new_S = S + query[2]
    elif(f == '2' and (r_count % 2 == 1)):
      new_S = query[2] + S
    S = new_S

if (r_count % 2 == 1):
  for i, s in enumerate(reversed(S)):
    if (i == len(S) - 1):
      print(s)
    else:
      print(s, end="")
else:
  print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
    tmp = input()
    if tmp == '1':
        if flg == 0:
            flg = 1
        else:
            flg = 0
    else:
        t1,t2,t3 = tmp.split()
        if int(t2) == 1:
            if flg == 0:
                s = t3 + s
            else:
                s = s + t3
        if int(t2) == 2:
            if flg == 0:
                s = s + t3
            else:
                s = t3 + s

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

Submission status: Time Limit Exceeded

Solution code:

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

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

Submission status: Time Limit Exceeded

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

topFlg = True

for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        topFlg = not (topFlg)
    else:
        if query[1] == "1":
            S = query[2] + S if topFlg else S + query[2]
        else:
            S = S + query[2] if topFlg else query[2] + S

print(S) if topFlg else print(S[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    s = input()
    s = s[0:len(s)-1]
    Q = int(input())

    flg = 0 # 0:sonomama 1:gyakuten
    for _ in range(Q):
        q = input().split()
        if q[0] == "1":
            if flg == 0:
                flg = 1
            else:
                flg = 0
        elif q[0]=="2":
            index,tmp = q[1],q[2]
            if (index == "1" and flg ==0) or (index == "2" and flg ==1):
                s = "".join([tmp,s])
            elif (index == "2" and flg == 0) or (index == "1" and flg == 1):
                s = "".join([s,tmp])

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

ans = S
for i in range(Q):
  if len(query[i])==1:
    ans = ans[::-1]
  else:
    t = [ans,query[i][2]]
    if(query[i][1]=="1"):
      t = [query[i][2],ans]
      #ans = ''.join(query[i][2])
      ans = "".join(t)
    else:
      t = [ans,query[i][2]]
      #ans = ''.join(query[i][2])
      ans = "".join(t)
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    query = input().split()
    if int(query[0]) == 1:
        parity += 1
    else:
        if int(query[1]) == 1 + (parity % 2):
            S = query[2] + S
        else:
            S = S + query[2]

if parity % 2 == 1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
bool = True
import sys
def input1():
	return sys.stdin.readline()[:-1]
for i in range(0, Q-1):
	s = input1()
	if len(s) == 1:
		bool = not bool
	else:
		if s[2] == "1":
			if bool:
				S = s[-1] + S
			else:
				S += s[-1]
		else:
			if bool:
				S += s[-1]
			else:
				S = s[-1] + S

s = input()
if len(s) == 1:
	bool = not bool
else:
	if s[2] == "1":
		if bool:
			S = s[-1] + S
		else:
			S += s[-1]
	else:
		if bool:
			S += s[-1]
		else:
			S = s[-1] + S
if bool:
	print(S)
else:
	print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
def main(s,x,q):
    res = s
    rev = 1 #-1なら反転中
    for op in q:
        if int(op[0]) == 1:
            rev = rev * -1
        elif int(op[1])*rev == 1 or int(op[1])*rev == -2:
                res = op[2] + res
        elif int(op[1])*rev == -1 or int(op[1])*rev == 2:
            res = res + op[2]
    return res if rev == 1 else res[::-1]

s = input()
x = int(input())
q = [input().split() for _ in range(x)]
print(main(s,x,q))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = deque(input())
n = int(input())
for _ in range(n):
    a = input()
    if len(a) != 1:
        b = list(a.split())
        if b[1] == '2':
            s.append(b[2])
        else:
            s.appendleft(b[2])
    else:
        s.reverse()
else:
    print(''.join(s))

Submission status: Time Limit Exceeded

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

for _ in range(Q):
  T = list(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: Time Limit Exceeded

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

for uery in uerys:
    if uery[0] == '1':
        S = S[::-1]
    
    if uery[0] == '2':
        if uery[1] == '1':
            S = uery[2] + S
        else:
            S = S + uery[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flag = 0
front = ""
back = ""
for i in range(Q):
  q = list(map(str, input().split()))
  if(q[0]=="1"):
    if(flag==0):
      flag = 1
    else:
      flag = 0
  else:
    if(q[1]=="1"):
      if(flag==0):
        front = ''.join([q[2], front])
      else:
        back = ''.join([back, q[2]])
    else:
      if(flag==0):
        back = ''.join([back, q[2]])
      else:
        front = ''.join([q[2], front])
ans = ''.join([front, S, back])
if( flag%2 == 0 ):
  print(ans)
else:
  print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
S=str(input())
Q=int(input())
count=0
for i in range(Q):
	a=list(map(str,input().split()))
	if a[0]=="1":
		count+=1
	elif a[0]=="2":
		if count%2==0:
			if a[1]=="1":
				S=a[2]+S
			elif a[1]=="2":
				S=S+a[2]
		elif count%2==1:
			if a[1]=="1":
				S=S+a[2][::-1]
			elif a[1]=="2":
				S=a[2][::-1]+S
if count%2==1:
	print(S[::-1])
else:
	print(S)

Submission status: Time Limit Exceeded

Solution code:
import math

a = input()
n = int(input())


rev = 0

for i in range (n):
    li = input().split()
    
    if li[0] == '1':
        rev = rev + 1
    elif li[0] == '2' and li[1] == '1':
        if rev % 2 == 0:
            a = li[2] + a
        else:
            a = a + ''.join(list(reversed(li[2])))
    else:
        if rev % 2 == 1:
            a = ''.join(list(reversed(li[2]))) + a
        else:
            a = a + li[2]

if rev % 2 == 1:
    a = ''.join(list(reversed(a)))
    print(a)
else:
    print(a)