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:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

    ans = deque(S)

    rev = False
    for _ in range(Q):
        T = input()

        if len(T) == 1:
            rev = not rev
        else:
            t, F, C = T.split()
            # 先頭
            if int(F) == 1:
                if not rev:
                    ans.appendleft(C)
                else:
                    ans.append(C)
            # 末尾
            else:
                if not rev:
                    ans.append(C)
                else:
                    ans.appendleft(C)

    x = ""
    while len(ans) > 0:
        x += ans.popleft()

    if rev:
        x = x[::-1]

    print(x)


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
Q = int(input())
front = 1
for i in range(Q):
    q = list(input().split())
    if q[0] == "1":
        front *= -1
    else:
        if q[1] == "1":
            if front == 1:
                s = q[2] + s
            else:
                s = s + q[2]             
        else:
            if front == 1:
                s = s + q[2]
            else:
                s = q[2] + s

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
bol = 0
for i in range(Q):
    a = input().split()
    if a[0] == "1":
        bol = 0 if bol else 1
    elif a[1] == "1":
        if not bol:
            S = a[2] + S
        else:
            S = S + a[2]
    else:
        if not bol:
            S = S + a[2]
        else:
            S = a[2] + S
print(S if not bol else "".join(reversed(S)))

Submission status: Time Limit Exceeded

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

rev = 0
for i in range(n):
    q = input().split()
    if q[0] == '1':
        rev = (rev + 1) % 2
    else:
        head = int(q[1]) % 2
        if head ^ rev:
            s = q[2] + s
        else:
            s += q[2]
            
print(s[::-1]) if rev else print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    query = list(input().split())
    if int(query[0]) == 1:
        flg *= -1
    else:
        f, c = int(query[1]), query[2]
        if f == 1:
            if flg == 1:
                s = c + s
            else:
                s += c
        else:
            if flg == 1:
                s += c
            else:
                s = c + s

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    query = list(input().split())
    if int(query[0]) == 1:
        s = s[::-1]
    else:
        f, c = int(query[1]), query[2]
        if f == 1:
            s = c + s
        else:
            s += c

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys, bisect, math, itertools, string, queue, copy
#import numpy as np
#import scipy
# from collections import Counter,defaultdict,deque
# from itertools import permutations, combinations
# from heapq import heappop, heappush
# from fractions import gcd
# input = sys.stdin.readline
# sys.setrecursionlimit(10**8)
# mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

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

isrev=False
for i in range(q):
  if ql[i][0] == '1':
    isrev = not isrev
    continue
  if (ql[i][1] == '2') == isrev:
    s=''.join([ql[i][2],s])
  else:
    s=''.join([s,ql[i][2]])
if isrev:
  s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = int(input())
str = [] * (N * 2)
rev = False
for i in range(N):
    A = input().split()
    if len(A) == 1:
        rev = not rev
    elif A[1] == '1':
        if rev == False:
            S = A[2] + S
        else:
            S = S + A[2]
    else:
        if rev == False:
            S = S + A[2]
        else:
            S = A[2] + S
if rev == False:
    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]
    elif A[1] == '1':
        S = A[2] + S
    else:
        S += A[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys, bisect, math, itertools, string, queue, copy
#import numpy as np
#import scipy
# from collections import Counter,defaultdict,deque
# from itertools import permutations, combinations
# from heapq import heappop, heappush
# from fractions import gcd
# input = sys.stdin.readline
# sys.setrecursionlimit(10**8)
# mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

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

isrev=False
for i in range(q):
  if ql[i][0] == '1':
    isrev = not isrev
    continue
  if (ql[i][1] == '2') == isrev:
    s=ql[i][2]+s
  else:
    s=s+ql[i][2]
if isrev:
  s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys, bisect, math, itertools, string, queue, copy
#import numpy as np
#import scipy
# from collections import Counter,defaultdict,deque
# from itertools import permutations, combinations
# from heapq import heappop, heappush
# from fractions import gcd
# input = sys.stdin.readline
# sys.setrecursionlimit(10**8)
# mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

s=input().replace("\n","")
q=inp()

ql = []
for i in range(q):
    tmp = input()
    if tmp[0] == "1":
        ql.append("1")
    else:
        ql.append(tmp.split())

isrev=False
for i in range(q):
  if ql[i][0] == '1':
    isrev = not isrev
    continue
  if (ql[i][1] == '2') == isrev:
    s=ql[i][2]+s
  else:
    s=s+ql[i][2]
if isrev:
  s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys, bisect, math, itertools, string, queue, copy
#import numpy as np
#import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

s=input().replace("\n","")
q=inp()

ql = []
for i in range(q):
    tmp = input()
    if tmp[0] == "1":
        ql.append("1")
    else:
        ql.append(tmp.split())

isrev=False
for i in range(q):
  if ql[i][0] == '1':
    isrev = not isrev
    continue
  if (ql[i][1] == '2') == isrev:
    s=ql[i][2]+s
  else:
    s=s+ql[i][2]
if isrev:
  s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools

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

flag = 0
prefix = []
suffix = []

for i in range(n):
    q = [x for x in stdin.readline().rstrip().split()]

    if len(q) == 1:
        flag = (flag + 1) % 2
    else:
        if (q[1] == "1" and flag == 0) or (q[1] == "2" and flag == 1):
            prefix.insert(0,q[2])
        else:
            suffix.append(q[2])

s = ''.join(prefix) + s + ''.join(suffix)

if flag == 1: s = ''.join(list(reversed(s)))
print (s)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools

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

flag = 0
for i in range(n):
    q = [x for x in stdin.readline().rstrip().split()]

    if len(q) == 1:
        flag = (flag + 1) % 2
    else:
        if (q[1] == "1" and flag == 0) or (q[1] == "2" and flag == 1):
            s = q[2] + s
        else:
            s = s + q[2]

if flag == 1: s = ''.join(list(reversed(s)))
print (s)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools

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

for i in range(n):
    q = [x for x in stdin.readline().rstrip().split()]

    if len(q) == 1:
        s = ''.join(list(reversed(s)))
    else:
        if q[1] == "1": s = q[2] + s
        if q[1] == "2": s = s + q[2]

print (s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
r = False
for _ in range(q):
    xs = input().split()
    if xs[0] == "1":
        r = not r
    else:
        if (not r and xs[1] == "1") or (r and xs[1] == "2"):
            s = "".join([xs[2], s])
        else:
            s = "".join([s, xs[2]])
print(s[::-1] if r else s)

Submission status: Time Limit Exceeded

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


def main():
  S = input()[:-1]
  Q = int(input())

  reverse = False
  for i in range(Q):
    line = input().split()
    if line[0] == '1':
      reverse = not(reverse)
    else:
      if reverse ^ (int(line[1])-1):
        S = S + line[2]
      else:
        S = line[2] + S

  print(''.join(reversed(S)) if reverse else S)


if(__name__ == '__main__'):
  main()

Submission status: Time Limit Exceeded

Solution code:
# ABC158
# D - String Formation

def ChangeState(Is_normal):
    if Is_normal==True:
        return False
    else:
        return True



S=input()
Q=int(input())
Is_normal=True

for _ in range(Q):
    q = input()
    if len(q)== 1 :
        Is_normal = ChangeState(Is_normal)
    else:
        if q[2]=='1':
            if Is_normal==True:
                S=q[4]+S
            else :
                S=S+q[4]
        else :
            if Is_normal==True:
                S=S+q[4]
            else:
                S=q[4]+S
    # print(Is_normal, S)
if Is_normal==False:
    S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
# ABC158
# D - String Formation

def ChangeState(Is_normal):
    if Is_normal==True:
        return False
    else:
        return True



S=input()
Q=int(input())
Is_normal=True

for _ in range(Q):
    q = list(map(str,input().split()))
    if len(q)== 1 :
        Is_normal = ChangeState(Is_normal)
    else:
        if q[1]=='1':
            if Is_normal==True:
                S=q[2]+S
            else :
                S=S+q[2]
        else :
            if Is_normal==True:
                S=S+q[2]
            else:
                S=q[2]+S
    # print(Is_normal, S)
if Is_normal==False:
    S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
p = int(input())
dir = 0
for _ in range(p):
  Q = input().split()
  if Q[0] == "1":
    dir = not dir
  else:
    if (int(Q[1]) - 1) == dir:
      s.insert(0, Q[2])
    else:
      s.append(Q[2])
      
if not dir:
  print("".join(s))
else:
  print("".join(s[::-1]))

Submission status: Time Limit Exceeded

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

cnt = 0
for q in query:
    if q[0] == "1":
        cnt += 1
    
    elif q[0] == "2":
        if cnt % 2 == 0:
            if q[1] == "1":
                S = q[2] + S
            elif q[1] == "2":
                S = S + q[2]
        else:
            if q[1] == "1":
                S = S + q[2]
            elif q[1] == "2":
                S =q[2] + S

if cnt % 2 == 1:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

for q in query:
    if q[0] == "1":
        S = S[::-1]
    elif q[0] == "2":
        if q[1] == "1":
            S = q[2] + S
        elif q[1] == "2":
            S = S + q[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
now: bool = True

for i in range(n):
    q = list(input().split())
    if q[0] == '1':
        now = not now
    else:
        if (q[1] == '1' and now == True) or (q[1] == '2' and now == False):
            s = q[2]+s
        elif (q[1] == '1' and now == False) or (q[1] == '2' and now == True):
            s = s+q[2]
if now == True:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

s = X()
s = deque(s)
q = k()
for i in range(q):
    i = L()
    if i[0]=="1":
        s.reverse()
        continue
    d = deque(i)
    a = d.popleft()
    if a == 1:
        pass
    else:
        b = d.popleft()
        c = d.popleft()
        if b == "1":
            s.insert(0,c)
        else:
            s.append(c)

print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
cnt=0
for _ in range(n):
    tmp=input()
    if tmp[0]=="1":
        cnt+=1
    else:
        num,side,st=tmp.split()
        if side=="1":
            if cnt%2==0:
                s=st+s
            else:
                s=s+st
        else:
            if cnt%2==0:
                s=s+st
            else:
                s=st+s
if cnt%2==1:
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    s = list(input())
    q = int(input())
    reverse = False
    
    for _ in range(q):
        query = list(input().split())
        if query[0] == '1':
            if reverse:
                reverse = False
            else:
                reverse = True
        else:
            if reverse:
                if query[1] == '1':
                    s = s + [query[2]]
                else:
                    s = [query[2]] + s
            else:
                if query[1] == '1':
                    s = [query[2]] + s
                else:
                    s = s + [query[2]]
                    
    s = ''.join(s)
    if reverse:
        print(s[::-1])
    else:
        print(s)
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    s = input()
    q = int(input())
    reverse = False
    
    for _ in range(q):
        query = list(input().split())
        if query[0] == '1':
            if reverse:
                reverse = False
            else:
                reverse = True
        else:
            if reverse:
                if query[1] == '1':
                    s = s + query[2]
                else:
                    s = query[2] + s
            else:
                if query[1] == '1':
                    s = query[2] + s
                else:
                    s = s + query[2]
    
    if reverse:
        print(s[::-1])
    else:
        print(s)
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

q = int(input())

reverse = 0

for i in range(q):
  query = list(input().split())
  if query[0] == '1':
    reverse ^= 1
    continue
  else:
    if query[1] == '1':
      if reverse:
        s.append(query[2])
      else:
        s.insert(0, query[2])
    else:
      if reverse:
        s.insert(0, query[2])
      else:
        s.append(query[2])
if reverse:
  s = s[::-1]
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

s = X()
q = k()
for i in range(q):
    a = X()
    if a[0] == "1":
        s.reverse()
    else:
        if a[2] == "1":
            s.insert(0,a[4])
        else:
            s.append(a[4])

print("".join(s))

Submission status: Time Limit Exceeded

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

forward = True

for _ in range(Q):
    query = input().split()

    if query[0] == '1':
        forward = not forward

    else:
        head = (query[1] == '1')

        if head ^ forward:
            S = S + query[2]

        else:
            S = query[2] + S

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

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    query = input().split()

    if query[0] == '1':
        # reverse
        S = S[::-1]

    else:
        # add character

        # at head
        if query[1] == '1':
            S = query[2] + S

        # at tail
        else:
            S = S + query[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s,q=deque([*input()]),int(input())
for i in range(q):
  t=input().split()
  if len(t)==1:
    s.reverse()
  else:
    t,f,c=t
    if f=='1': s.appendleft(c)
    else: s.append(c)
print(*s,sep='')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s,q=deque([*input()]),int(input())
for i in range(q):
  t=input().split()
  if len(t)==1:
    s.reverse()
  else:
    if t[1]=='1': s.appendleft(t[2])
    else: s.append(t[2])
print(*s,sep='')

Submission status: Time Limit Exceeded

Solution code:
s,q=[*input()],int(input())
for i in range(q):
  t=input().split()
  if len(t)==1:
    s=s[::-1]
  else:
    if t[1]=='1':
      s.insert(0,t[2])
    else: s.append(t[2])
print(*s,sep='')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = deque(list(input()))
n = int(input())
q = [list(input().split()) for _ in range(n)]
for tfc in q:
    if tfc[0] == "1":
        s.reverse()
    else:
        if tfc[1] == "1":
            s.appendleft(tfc[2])
        else:
            s.append(tfc[2])

print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import collections as cl


def II(): return int(sys.stdin.readline())


def MI(): return map(int, sys.stdin.readline().split())


def LI(): return list(map(int, sys.stdin.readline().split()))


def main():
    S = input()
    Q = II()
    
    direction = 1
    for i in range(Q):
        query = input()
        if query == '1':
            direction *= -1
        else:
            _, f, c = query.split()
            if direction == 1:
                if f == '1':
                    S = "".join([c,S])
                else:
                    S = "".join([S,c])
            else:
                if f == '1':
                    S = "".join([S,c])
                else:
                    S = "".join([c,S])
    if direction == 1:
        print(S)
    else:
        print(S[::-1])

main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import collections as cl


def II(): return int(sys.stdin.readline())


def MI(): return map(int, sys.stdin.readline().split())


def LI(): return list(map(int, sys.stdin.readline().split()))


def main():
    S = input()
    Q = II()
    
    direction = 1
    for i in range(Q):
        query = input()
        if query == '1':
            direction *= -1
        else:
            _, f, c = query.split()
            if direction == 1:
                if f == '1':
                    S = c + S
                else:
                    S = S + c
            else:
                if f == '1':
                    S = S + c
                else:
                    S = c + S
    if direction == 1:
        print(S)
    else:
        print(S[::-1])

main()

Submission status: Time Limit Exceeded

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

# In[14]:


from collections import deque


# In[15]:


S = deque(input())
Q = int(input())
for _ in range(Q):
    q = list(input().split())
    if len(q) == 1:
        S = deque(reversed(S))
    else:
        if q[1] == "1":
            S.appendleft(q[2])
        else:
            S.append(q[2])
print("".join(S))


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
top=bool()
n=int(input())
for i in range(n):
  q=list(map(str,input().split()))
  if q[0]=='1':
    top=not top
  else:
    if (q[1]=="1") ^ top:
      s.insert(0,q[2])
    else:
      s.append(q[2])
if top:
  print(*list(reversed(s)),sep='')
else:
  print(*s,sep='')

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
top=bool()
t=[]
u=[]
n=int(input())
for i in range(n):
  q=list(map(str,input().split()))
  if q[0]=='1':
    top=not top
  else:
    if (q[1]=="1") ^ top:
      s.insert(0,q[2])
    else:
      s.append(q[2])
if top:
  print(*list(reversed(s)),sep='')
else:
  print(*s,sep='')

Submission status: Time Limit Exceeded

Solution code:
s=input()
top=bool()
n=int(input())
for i in range(n):
  q=list(map(str,input().split()))
  if q[0]=='1':
    top=not top
  else:
    if (q[1]=="1") ^ top:
      s=q[2]+s
    else:
      s=s+q[2]
if top:
  print(*list(reversed(list(s))),sep='')
else:
  print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
sgn = 1
Q = int(input())
ans = []
for _ in range(Q):
  i = input()
  if i[0] == '1':
    sgn*=-1
  else:
    if i[2] == '1':
      ans.append([i[4], -sgn])
    else:
      ans.append([i[4], sgn])

if sgn == -1:
  s = s[::-1]
  for k in range(len(ans)):
    if ans[k][1] == -1:
      s = s + ans[k][0]
    else:
      s = ans[k][0] + s
else:
  for k in range(len(ans)):
    if ans[k][1] == 1:
      s = s + ans[k][0]
    else:
      s = ans[k][0] + s

print(s)

Submission status: Time Limit Exceeded

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

s = input().strip()
q=int(input())
count = 0
for i in range(q):
    L = list(input().rstrip().split())    
    if L[0]=='1':
        count += 1
    else:
        a,b,c = L
        b = int(b)
        a=int(a)
        if count&1==1:
            b=3-b
        if int(b)==2:
            s+=c
        else:
            v = list(s)
            v.insert(0,c)
            s = "".join(v)
if count &1==1:
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

s = input().strip()
q=int(input())
for i in range(q):
    L = list(input().rstrip().split())    
    if L[0]=='1':
        s = s[::-1]
    else:
        a,b,c = L
        if int(b)==2:
            s+=c
        else:
            v = list(s)
            v.insert(0,c)
            s = "".join(v)
print(s)

Submission status: Time Limit Exceeded

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

def main():
    S = input()
    Q = int(input())
    reflag = False
    for i in range(Q):
        query = list(input())
        if query[0] == '1':
            reflag = not reflag
        elif query[2] == '1':
            if reflag:
                S = S + query[4]
            else:
                S = query[4] + S
        else:
            if reflag:
                S = query[4] + S
            else:
                S = S + query[4]
    if reflag:
        print(S[::-1])
    else:
        print(S)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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

    for i in range(Q):
        query = list(input())
        if query[0] == '1':
            S = S[::-1]
        elif query[2] == '1':
            S = query[4] + S
        else:
            S = S + query[4]
    print(S)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
is_order = True
for i in range(Q):
    tmp = input()
    if len(tmp) == 1:
        t = 1
        is_order = not is_order
    else:
        t,f,c = tmp.split()
        f = int(f)
        if f == 1:
            if is_order:
                S = c + S
            else:
                S = S + c[::-1]
        else:
            if is_order:
                S = S + c
            else:
                S = c[::-1] + S
if is_order:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    tmp = input()
    if len(tmp) == 1:
        t = 1
        S = "".join(list(reversed(S)))
    else:
        t,f,c = tmp.split()
        f = int(f)
        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):
    tmp = input()
    if len(tmp) == 1:
        t = 1
        S = S[::-1]
    else:
        t,f,c = tmp.split()
        f = int(f)
        if f == 1:
            S = c + S
        else:
            S = S + c
print(S)