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 = input()
Q = int(input())
ans = S
for _ in range(Q):
    q = [i for i in input().split()]
    if int(q[0]) == 1:
        S = S[::-1]
    else:
        if int(q[1]) == 1:
            S = q[2] + S
        else:
            S += q[2]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for _ in range(q):
    a = input().split()
    if len(a) == 1:
        s = s[::-1]
    else:
        f, c = a[1], a[2]
        if f == '1':
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
a=1
for _ in range(q):
  t = input()
  if t=='1':
    a *= -1
  else:
    if t[2]=='1':
      s = t[4:] + s[::a]
    else:
      s = s[::a] + t[4:]
    a=1
print(s[::a])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=deque(list(input()))
q=int(input())
direction=0 #0:順 1:逆

for i in range(q):
    query=input()
    if query[0]=='1':
        direction=direction^1
    elif query[0]=='2':
        i,f,c=query.split()
        if f=='1' and direction==0:
            s.appendleft(c)
        elif f=='1' and direction==1:
            s.append(c)
        elif f=='2' and direction==0:
            s.append(c)
        elif f=='2' and direction==1:
            s.appendleft(c)


if direction==0:
    print(''.join(s))
else:
    x=[]
    for i in range(len(s)):
        x.append(s[-1-i])
    print(''.join(x))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        S = S[::-1]
    else:
        F = int(q[1])
        C = q[2]
        if F == 1:
            S = C + S
        else:
            S = S + C

print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
# import math
# import decimal
# import queue
# import bisect
# import heapq
# import time
# import itertools
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29

def main():
    s = list(input())
    q = int(input())
    for i in range(q):
        tfc = list(input().split())
        if tfc[0]=='1':
            s.reverse()
        else:
            t,f,c = tfc
            if f=='1':
                s.insert(0,c)
            else:
                s.append(c)
    print(''.join(s))
    return

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
count_1=0
l=[]
num =int(input())
for i in range(num):
    q=input().rstrip().split()
    l.append(q)

for i in l:
    if i[0]=="1":
        s=s[::-1]
        
    else:
        if i[1]=="1":
            s=i[2]+s
        else:
            s=s+i[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=list(str(input()))
s=deque(s)
q=int(input())
for i in range(q):
    que=list(input().split())
    if len(que)==1:
        s=list(s)
        s=s[::-1]
    else:
        if int(que[1])==1:
            s=deque(s)
            s.appendleft(que[2])
        elif int(que[1])==2:
            s=deque(s)
            s.append(que[2])
s=list(s)
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
S=[c for c in input()]
ans=S[:]
Q=int(input())
for i in range(Q):
    t=[a for a in input().split()]
    if t[0]=="1":
        ans=ans[::-1]
    else:
        if t[1]=="1":
            ans.insert(0,t[2])
        else:
            ans.append(t[2])
print("".join(ans))

Submission status: Time Limit Exceeded

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

mode = 1
for i in range(Q):
  query = input()
  if query=='1':
    mode *= -1
  else:
    _, F, C = query.split()
    if F=='1':
      if mode==1:
        #S = C+S # add to last
        S = "".join([C,S])
      else:
        #S = S+C # add to beginning
        S = "".join([S,C])
    else:
      if mode==1:
        #S = S+C # add to beginning
        S = "".join([S,C])
      else:
        #S = C+S # add to last
        S = "".join([C,S])

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

print(S)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
rev = False
s = input()
q = int(input())
while(q != 0):
	t = input().split()
	if(t[0] == "1"):
		rev = not(rev)
	else:
		# print("-->", t[0])
		f = int(t[1])
		c = t[2]
		if(f == 1):
			if(rev):
				s = s + c
			else:
				s = c + s
		else:
			if(rev):
				s = c + s
			else:
				s = s + c
	q -= 1

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
d = deque()
li = list(s)
for i in li:
    d.append(i)
n = int(input())
for _ in range(n):
    q = input().split()
    if (len(q)==1):
        d.reverse()
    else:
        a = int(q[1])
        b = q[2]
        if (a == 1):
            d.appendleft(b)
        else:
            d.append(b)
ans = list(d)
xxx = "".join(ans)
print(xxx)

Submission status: Time Limit Exceeded

Solution code:
def inv(s):
    t = ''
    for i in range(len(s)):
        t += s[-1 * (i + 1)]
    return t

s = input()
q = int(input())
ans = s
for i in range(q):
    qi = input()
    if qi[0] == '1':
        ans = inv(ans)
    else:
        if qi[2] == '1':
            ans = qi[4] + ans
        elif qi[2] == '2':
            ans = ans + qi[4]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#for _ in range(int(input())):
s = input()
q = int(input())
b=0
for _ in range(q):
    qi = input().split(' ')
    #print(len(qi))
    if(len(qi)==1):
        b=b^1
    else:
        fi = int(qi[1])
        if(fi==1):
            if(b==0):
                s=qi[2]+s
            else:
                s=s+qi[2]
        elif(fi==2):
            if(b==0):
                s=s+qi[2]
            else:
                s=qi[2]+s
if(b==1):
    s=s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
r = 1
for x in range(n):
    q = input().split()
    if q[0] == '1':
        r = -r
    elif q[1] == '1':
        z = q[2][0]
        if r == 1:
            s.insert(0, z)
        else:
            s.append(z)
    else:
        z = q[2][0]
        if r == -1:
            s.insert(0, z)
        else:
            s.append(z)
if r == -1:
    s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
def sol():
    s=input()
    q=int(input())
    x=0
    for i in range(q):
        l=[i for i in input().split()]
        if l[0]=="1":
            x=(x+1)%2
        elif l[1]=="1":
            if x==0: s=l[2]+s
            else: s=s+l[2]
        elif l[1]=="2":
            if x==0: s=s+l[2]
            else: s=l[2]+s
    print(s if x==0 else s[::-1])

if __name__=="__main__":
    sol()

Submission status: Time Limit Exceeded

Solution code:
s=input()
from sys import stdin
input = stdin.readline
q=int(input())
cnt=0
qq=[list(input().split()) for _ in range(q)]
for j in qq:
    if  len(j)!=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())

rev = False

for i in range(Q):
    # クエリを受け取る
    query = input().split()
    # 反転の操作
    if query[0] == '1':
        if rev == True:
            rev = False
        else:
            rev = True
    else:
        # 追加する操作
        F = int(query[1])
        C = query[2]
        # 先頭に追加する場合
        if F == 1:
            # 反転しているとき
            if rev == True:
                # 末尾に追加
                S = S + C
            # 反転していないとき
            else:
                # 先頭に追加
                S = C + S
        # 末尾に追加する場合
        else:
            # 反転しているとき
            if rev == True:
                # 先頭に追加
                S = C + S
            # 反転していないとき
            else:
                # 末尾に追加
                S = S + C
if rev == True:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

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

ans = S
mode = True  # True: Normal False: Reverse
for query in Query:
    T = query[0]
    if T == '1':
        # モードを反転
        mode = not mode
    else:
        F = query[1]
        if (mode and (F == '1')) or (not mode and (F == '2')):
            ans = query[2] + ans
        else:
            ans = ans + query[2]
    
if not mode:
    ans = ans[::-1]

print(ans)

Submission status: Time Limit Exceeded

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

for q in Query:
    if q[0]=='1':
        S = S[::-1]
    else:#q[0]=='2'
        if q[1]=='1':
            S = q[2] + S
        else:#q[1]=='2'
            S = S + q[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
S = input()
Q = int(input())
Query  = [list(input().split()) for _ in range(Q)]

#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 q in Query:
  if int(q[0]) == 1:
    ans = reverse(ans)
  if int(q[0]) == 2:
    ans = addition(ans, q[1], q[2])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
muki=True
a=[[_ for _ in input().split()] for i in range(q)]
for i in a:
    if i[0]=="1":
        muki=not muki
        continue
    if (i[1]=="1" and muki) or (i[1]=="2" and not muki):
        s=i[2]+s
    else:
        s+=i[2]
if muki:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for i in range(Q):
    q = list(input().split())
    if(int(q[0]) == 1):
        S = list(reversed(S))
    else:
        if(int(q[1]) == 1):
            S.insert(0,q[2])
        else:
            S.append(q[2])
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for i in range(Q):
    Query =  [str(p) for p in input().split()]
    if(int(Query[0]) == 1):
        S.reverse()
    else:
        if(int(Query[1]) == 1):
            S.appendleft(Query[2])
        else:
            S.append(Query[2])

print("".join(list(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
for query in query_li:
    if query[0] == '1':
        ans = ans[::-1]
    elif query[1] == '1':
        ans = query[2] + ans
    else:
        ans += query[2]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
ql=[]

for _ in range(q):
    ql.append(list(map(str,input().split())))
flg=True #正順
for qi in ql:
    # qi=list(map(str,input().split()))
    if qi[0]=="1":
        flg=not flg
    else:
        f,c=qi[1],qi[2]
        if f=="1":
            if flg:
                s=c+s
            else:
                s=s+c
        else:
            if not flg:
                s=c+s
            else:
                s=s+c
                

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

rev = False
for _ in range(q):
    query = input().split()

    if query[0] == '1' and not(rev):
        rev = True
    elif query[0] == '1' and rev:
        rev = False

    elif query[0] == '2' and query[1] == '1' and not(rev):
        #s = query[2] + s
        s.insert(0, query[2])
    elif query[0] == '2' and query[1] == '1' and rev:
        #s += query[2]
        s.append(query[2])

    elif query[0] == '2' and query[1] == '2' and not(rev):
        #s += query[2]
        s.append(query[2])
    elif query[0] == '2' and query[1] == '2' and rev:
        #s = query[2]+s
        s.insert(0, query[2])

ans = ''
if rev:
    s.reverse()
    ans = ''.join(s)
else:
    ans = ''.join(s)

print(ans)

Submission status: Time Limit Exceeded

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

for j in range(Q):
  if sousa[j][0] == '1':
    S = S[::-1]
  else:
    if sousa[j][1] == '1':
      S = sousa[j][2] + S
    else:
      S = S + sousa[j][2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
for i in range(int(input())):
    query = input()
    if len(query)==1:
        s = "".join(list(reversed(s)))
        continue

    s = query[-1]+s if query[2]=="1" else s+query[-1]

print(s)

Submission status: Time Limit Exceeded

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


# 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():
    a = input()
    Q = int(input())
    qs = [input().split() for _ in range(Q)]
    ans = a
    for q in qs:
        if len(q) == 1:
            ans = ''.join(list(reversed(ans)))
        else:
            if int(q[1]) == 1:
                ans = q[2] + ans
            else:
                ans = ans + q[2]
    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(n):
    if l[i][0] == '1':
        s = s[::-1]
    else:
        if l[i][1] == '1':
            s = l[i][2] + s
        else:
            s += l[i][2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
ans_q = [s]
ans = []
one_count = 0
for i in range(q):
    ans.append(list(input().split()))
for i in ans:
    if int(i[0])==1:
        one_count += 1
    else:
        if one_count%2==0:
            if int(i[1]) == 1:
                ans_q.insert(0,i[2])
            else:
                ans_q.insert(len(ans_q),i[2])
        else:
            if int(i[1]) == 1:
                ans_q.insert(len(ans_q),i[2])
            else:
                ans_q.insert(0,i[2])
if one_count%2==0:
    print(''.join(ans_q))
else:
    print(''.join(ans_q)[::-1])

Submission status: Time Limit Exceeded

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

rev = 0

for i in range(q):
    t = list(input().split())
    if t == ['1']:
        rev += 1
        continue
    if t[1] == '1':
        if rev % 2 == 0:
            s.insert(0, t[2])
        else:
            s.append(t[2])
    else:
        if rev % 2 == 0:
            s.append(t[2])
        else:
            s.insert(0, t[2])
if rev % 2 != 0:
    s.reverse()

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
s=input().replace('\n','')
q=int(input())
num1=0
for i in range(q):
    query=input()
    if query[0]=='1':
        num1+=1
    else:
        query=query.replace('\n','')
        _,f,c=map(str,query.split())
        if f=='1':
            if num1%2==0:
                s=c+s
            else:
                s=s+c[::-1]
        else:
            if num1%2==0:
                s=s+c
            else:
                s=c[::-1]+s
if num1%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
for _ in range(n):
    q = input().split()
    if q[0] == "1":
        s.reverse()
    elif q[0] == "2":
        if q[1] == "1":
            s.reverse()
            s.append(q[2])
            s.reverse()
        elif q[1] == "2":
            s.append(q[2])
s = "".join(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys; sys.setrecursionlimit(2147483647); input = sys.stdin.readline
from math import floor, ceil, sqrt, factorial, log
from collections import Counter, defaultdict
from operator import itemgetter
INF = float('inf'); MOD = 10**9+7
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(MI())
def LIR(n): return [LI() for i in range(n)]
def S(): return input().rstrip()
def LIR(n): return [S().split() for i in range(n)]

def main():
    s = S()
    Q = I()
    Qu = LIR(Q)
    for que in Qu:
        if que[0] == '1':

            s = s[::-1]
        else:
            if que[1] == '1':
                s = que[2]+s
            else:
                s = s+que[2]
    print(s)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def solve():
    S = input()
    Q = int(input())

    is_reverse = False # 反転中か？

    for _ in range(Q):
        query = input()
        if query == "1":
            is_reverse = not is_reverse
        else:
            t, f, c = query.split()
            if f == "1":
                if is_reverse:
                    # 末尾にcを追加
                    S = S + c
                else:
                    # 先頭にcを追加
                    S = c + S
            else:
                if is_reverse:
                    # 先頭に追加
                    S = c + S
                else:
                    # 末尾に追加
                    S = S + c

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


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

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

result = S
reverse_flag = False

for i in range(Q):
    # print(i + 1)
    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)
        if f == 1:
            if reverse_flag:
                result = result + c
            else:
                result = c + result
        else:
            if reverse_flag:
                result = c + result
            else:
                result = result + c
    # print(t, f, c, result)
    # print("--------")
if reverse_flag:
    result = result[::-1]
print(result)

Submission status: Time Limit Exceeded

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

a = ""
b = ""

for _ in range(Q):
    query = input()
    if query[0]=="1":
        t = 1
        rev_cnt +=1
        a,b = b[::-1],a[::-1]
    else:
        t,f,c = query.split()
        if f=="1":
            a = c+a
        elif f=="2":
            b = b+c
if rev_cnt%2==0:
    ans = a+S+b
else:
    ans = a+S[::-1]+b
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
s = sys.stdin.readline().rstrip()
q = int(sys.stdin.readline())
for i in range(q):
    ts = sys.stdin.readline().rstrip().split()
    if ts[0] == '1':
        s = s[::-1]
    else:
        t = int(ts[0])
        f = int(ts[1])
        c = ts[2]
        if f == 1:
            s = c + s
        else:
            s = s + c
print (s)

Submission status: Time Limit Exceeded

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

ans = list(S)
rev = 0
head = []
foot = []
for i in range(Q) :
    query = input()
    if query == "1" :
        rev += 1
    else :
        a, F, C = query.split()
        if F == "1" and rev%2 == 0:
            head.insert(0, C)
        elif F == "1" and rev%2 == 1:
            foot.append(C)
        elif F == "2" and rev%2 == 0:
            foot.append(C)
        else :
            head.insert(0, C)

if rev%2 == 0 :
    print("".join(head + ans + foot))
else :
    print("".join((reversed(head + ans + foot))))

Submission status: Time Limit Exceeded

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

for t in ls:
    if t[0] == '1':
        s = s[::-1]
    elif t[0] == '2':
        if t[1] == '1':
            s = t[2] + s
        elif t[1] == '2':
            s = s + t[2]
    
print(s)

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:
    a, F, C = q.split()
    if F == '1':
      if rev:
        S = S + C
      else:
        S = C + S
    else:
      if rev:
        S = C + S
      else:
        S = S + C
      
if rev:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
jun = 1
for _ in range(q):
  
  tmp = input()
  if len(tmp)==1:
    jun = 1 if jun==0 else 0 
  else:
    _,f,c = tmp.split()
    if jun+int(f) ==2:
      s = c+s
    else:
      s += c
if jun==1:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

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

hanten = 0

for q in range(Q):
    query = input()
    if query == "1":
        if hanten == 0:
            hanten = 1
        else:
            hanten = 0
    else:
        T,F,C = query.split()
        if F == "1":
            if hanten == 0:
                S = C+S
            else:
                S = S+C
        elif F =="2":
            if hanten == 0:
                S = S + C
            else:
                S = C + S
            
if hanten == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

rev = 0
for q in query:
    if q[0] == "1":
        rev = (rev+1)%2
    else:
        if q[2] == "1":
            if rev == 0:
                S = q[4] + S
            else:
                S = S + q[4]
        else:
            if rev ==0:
                S = S + q[4]
            else:
                S = q[4] + S
if rev == 1:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for i in range(q):
	a=input()
	if a=="1":
		s=s[::-1]
	else:
		a,f,c = a.split()
		if f=="1":
			s=c+s
		else:
			s+=c
print(s)

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
for i in range(int(input())):
    q=input()
    if q=='1':
        S.reverse()
    else:
        a,f,c=q.split()
        S=[c]+S if f=='1' else S+[c] 
print(''.join(S))

Submission status: Time Limit Exceeded

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

reverse_flag = False

top = []
last = []
for i in range(q):
  q_list = list(input().split())
  
  if len(q_list) == 1:
    reverse_flag = not reverse_flag
    
  elif q_list[1] == '1' and (not reverse_flag):
    top.insert(0, q_list[2])
    
  elif q_list[1] == '2' and (not reverse_flag):
    last.append(q_list[2])
    
  elif q_list[1] == '1' and reverse_flag:
    last.append(q_list[2])
    
  elif q_list[1] == '2' and reverse_flag:
    top.insert(0, q_list[2])
    
if reverse_flag:
  print(''.join(reversed(last)) + ''.join(reversed(s)) + ''.join(reversed(top)))
else:
  print(''.join(top) + ''.join(s) + ''.join(last))

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    q = str(input())
    if len(q)==1:
        # print('re')
        s=s[::-1]
    else:
        _, f,c = q.split()
        if f =='1':
            s=c+s
        elif f =='2':
            s=s+c
        # s.append(c)
    # print(s)
        
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:
  print("".join(ans))
else:
  t = ans[::-1]
  print("".join(t))

Submission status: Time Limit Exceeded

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

isreverse = False

for i in range(Q):
    query = input()
    if str(query)[0]=="1":
        isreverse = isreverse^True
    else:
        F = int(query.split()[1])
        C = str(query.split()[2])
        if (F==1 and isreverse==False) or (F==2 and isreverse==True):
            S = C+S
        else:
            S = S+C
if isreverse==False:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
#n = int(input())
#l = list(map(int, input().split()))

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''


def main():
    import sys
    input = sys.stdin.readline
    s = input().rstrip("\n")
    q= int(input())
    han=0
    
    for i in range(q):
        que=input().split()
        if len(s)==1 and que[0]=="1":
            continue

        if que[0]=="1":
            han+=1
            #print(han, s, que)
        else:
            if han%2==0:#反転なし
                if que[1]=="1":
                    s=que[2]+s
                else:
                    s+=que[2]
            else:#あり
                if que[1] == "1":
                    s+=que[2]
                else:
                    s=que[2]+s
            #print(han, s, que)

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(loop):
    queries = input().split()
    if len(queries) == 1:
        s = s[::-1]
    else:
        type = int(queries[1])
        c = str(queries[2])
        if type == 1:
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
  A = input().split()
  if int(A[0]) == 1:
    S = S[::-1]
  else:
    if A[1] == '1':
      S = A[2] + S
    else:
      S += A[2]

print(S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def main():
  d=deque(list(input()))
  Q=int(input())
  for i in range(Q):
    q=list(map(str, input().split()))
    if q[0]=='1':
      d.reverse()
    else:
      if q[1]=='1':
        d.appendleft(q[2])
      else:
        d.append(q[2])
  print(''.join(list(d)))
main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
S = (stdin.readline().rstrip())
Q = int(stdin.readline().rstrip())
cnt = 0
s = "@"
for i in range(Q):
    data = [(x) for x in stdin.readline().rstrip().split()]
    if data[0] == "2":
        if data[1] == "1":
            s = "".join([data[2],s])
            #S = data[2] + S
        else:
            #S = S + data[2]
            s = "".join([s,data[2]])
    else:
        s = s[::-1]
        cnt += 1
if cnt % 2 != 0:
    S = S[::-1]
ans = s.replace("@",S)
print(ans)