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())
f = 0
for i in range(n):
  l = input().split()
  if l[0] == "2":
    if (l[1] == "1" and f == 0) or (l[1] == "2" and f == 1):
      s.insert(0,l[2])
    else:
      s.append(l[2])
  else:
    if f == 0:
      f = 1
    else:
      f = 0

print("".join(s) if f == 0 else "".join(s[::-1]))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = input()
Q = int(input())
a = deque(S)

for i in range(Q):
  A = input().split()
  if A[0] == "1":
    a.reverse()
  else:
    if A[1] == "1":      
      a.appendleft(A[2])
    else:
      a.append(A[2])
print("".join(a))

Submission status: Time Limit Exceeded

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

a = input()

n = int(input())
mode = 1
for i in range(n):
    hoge = list(map(str,input().split()))
    if hoge[0] == '1':
        mode = mode * -1
    elif hoge[0] == '2':
        if mode == 1:
            if hoge[1] == '1':
                a = hoge[2] + a
            elif hoge[1] == '2':
                a = a + hoge[2]
        elif mode == -1:
            if hoge[1] == '2':
                a = hoge[2] + a
            elif hoge[1] == '1':
                a = a + hoge[2]

if mode == -1:
    a = ''.join(list(reversed(list(a))))
else:
    a = ''.join(list(a))

print(str(a))

Submission status: Time Limit Exceeded

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

for c in range(0,iteration):
    sousa = input().split()
    if sousa[0] == "1":
        s = s[:: -1]
    elif sousa[1] == "1":
        s = sousa[2] + s
    else:
        s = s + sousa[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

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

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    q = input()
    if len(q) == 1:
        S = S[::-1]
    else:
        T, F, C = q.split()
        if int(F) == 1:
            S = C + S
        else:
            S = S + C

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
rev_flag = False
for _ in range(q):
    i = input()
    if i == '1':
        rev_flag = not rev_flag
    else:
        a,b,c = i.split()
        if rev_flag:
            if b == '1':
                s = s + c
            else:
                s = c + s
        else:
            if b == '1':
                s = c + s
            else:
                s = s + c
if rev_flag:
    print(''.join(list(reversed(s))))
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
re = list(input())
Q = int(input())
sousa = [input().split() for i in range(Q)]
for i in sousa:
  if len(i) == 1:
    re = re[::-1]
  else:
    if i[1] == '1':
      k = list(i[2])
      re = k + re
    else:
      k = list(i[2])
      re = re + k
print(''.join(re))

Submission status: Time Limit Exceeded

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

def T(t, S):
    t = t.split()
    if t[0] == '1':
        return S[::-1]
    else:
        if t[1] == '1':
            S.insert(0, t[2])
        else:
            S.append(t[2])
        return S

for i in range(Q):
    S = T(input(), S)
print(''.join(S))

Submission status: Time Limit Exceeded

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


def main(s, q, query):
    addflag = 'front'
    for i in range(q):
        if query[i][0] == '1':
            if addflag == 'front':
                addflag = 'back'
            elif addflag =='back':
                addflag = 'front'
        elif query[i][0] == '2':
            if addflag == 'front' and  query[i][1] == '1':
                s = query[i][2] + s
            elif addflag == 'front' and query[i][1] == '2':
                s = s + query[i][2]
            elif addflag == 'back' and  query[i][1] == '1':
                s = s + query[i][2]
            elif addflag == 'back' and  query[i][1] == '2':
                s = query[i][2] + s

    if addflag == 'back':
        s = ''.join(reversed(list(s)))

    return s


print(main(S, Q, QUERY))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
q = []
for i in range (Q):
    q.append(input())
    if len(q[i]) ==1:
        S = S[::-1]
    else:
        n,F,C =q[i].split()
        f = int(F)
        if f == 1:
            S = C + S
        else:
            S = S + C

print(S)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
S = input()
Q = int(input())
reverse = 0
head = []
tail = []

for i in range(Q):
    readline = stdin.readline
    s = readline().split()
    if int(s[0]) == 1:
        reverse = (reverse + 1) % 2
    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)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
S =input()
Q=int(input())
orders=[]
for line in sys.stdin.readlines():
    #print(line)
    orders.append(line.rstrip().split(" "))
#print(orders)
for order in orders:
    if order[0]=="1":
        #print(1)
        S=S[::-1]
    elif order[0]=="2" and order[1]=="1":
        #print(2)
        S=order[2]+S
    elif order[0]=="2" and order[1]=="2":
        #print(3)
        S=S+order[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = []
t1 = 1
for i in range(q):
    t = input()
    query.append(t)

for i in range(len(query)):
    t = query[i]
    if t == '1':
        t1 *= -1
    else:
        if t1 == 1:
            if t[2] == '1':
                s = t[4] + s
            else:
                s = s + t[4]
        else:
            if t[2] == '2':
                s = t[4] + s
            else:
                s = s + t[4]
        
ans = ''
if t1 == -1:
    ans = ''.join(list(reversed(s)))
else:
    ans = s

print(ans)

Submission status: Time Limit Exceeded

Solution code:
item = [i for i in input()]
n = int(input())
flag = 0
append = item.append
for i in range(n):
    tmp = input()
    if tmp[0] == '1':
        flag += 1
        continue

    _, q, s = tmp.split()
    if q == '1' and flag % 2 == 0:
        item.reverse()
        flag += 1
    elif q == '2' and flag % 2 == 1:
        item.reverse()
        flag += 1
    append(s)

if flag % 2 == 1:
    item.reverse()
print(''.join(item))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse_cnt = 0
tmp = "*"
for i in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse_cnt += 1
        tmp = tmp[::-1]
    else:
        if query[1] == "1":
            tmp =  query[2] + tmp
        else:
            tmp = tmp + query[2] 

if reverse_cnt % 2 != 0:
    S = S[::-1]
    
print(tmp.replace("*", S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
cnt = 0
for i in range(q):
  query = input()
  if query == str(1):
    cnt += 1
  else:
    t, f, c = query.split(' ')
    f = int(f)
    if cnt % 2 == 0:
      if f == 2:
        s += c
      elif f == 1:
        s = c + s
    else:
      if f == 1:
        s += c
      elif f == 2:
        s = c + s

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flag = 0
for _ in range(Q):
    command = input()
    if command[0] == '1': flag += 1
    else:
        a, b, char = command.split()
        if (int(b) + flag) % 2 == 1: S = char + S
        else: S = S + char
if flag % 2 == 1: print(S[::-1])
else: print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
nq = int(input())
flg = 0
front = []
back = []
for i in range(nq):
    q = input()
    if len(q) == 1:
        flg += 1
    else:
        if flg % 2 == 0:
            if q[2] == '1':
                front = [q[-1]] + front
            else:
                back = back + [q[-1]]
        else:
            if q[2] == '1':
                back = back + [q[-1]]
            else:
                front = [q[-1]] + front
                
s = ''.join(front) + s + ''.join(back)
if flg % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

flag = 0
for x in X:
    if int(x[0]) == 1:
        # s = s[::-1]
        if flag ==0:
            flag = -1
        else:
            flag = 0
        continue
    if int(x[1]) +flag == 1:
        s.insert(0, x[2])
    else:
        s.append(x[2])
ans = ''.join(s)
if flag == -1:
    print(ans[::-1])
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
Q_list = []
for i in range(Q):
    Q_list.append(str(input()).split())
for i in Q_list:
    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:
import sys

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

is_reverse = False
for _ in range(q):
  tf = sys.stdin.readline().split()
  if tf[0] == '1':
    is_reverse = False if is_reverse else True
  else:
    if tf[1] == '1':
      if is_reverse:
        s += tf[2]
      else:
        s = tf[2] + s
    else:
      if is_reverse:
        s = tf[2] + s
      else:
        s += tf[2]
if is_reverse:
  print(s[::-1])
else:
  print(s)

Submission status: Time Limit Exceeded

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

flag = 0

for i in range(q):
    Q.append(input())

#print('start')
for p in Q:
    #print(p)
    if len(p)==1:
        flag = 1-flag
    if len(p)==5:
        if(p[2]=='1'):
            if flag ==1:
                ans = ans + p[4]
            else:
                ans = p[4] + ans
        else:
            if flag ==1:
                ans = p[4] + ans
            else:
                ans = ans + p[4]
    #print(ans)
    #print(f'flag{flag}')

if flag==0:
    print(ans)
else:
    num = len(ans)
    for i in range(num):
        print(ans[num-1-i],end="")
    print()

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 q[i][0] == "1":
    S = S[::-1]
    continue
  elif q[i][0] == "2":
    if q[i][1] == "1":#先頭に追加
      S = q[i][2] + S
      continue
    elif q[i][1] == "2":#末尾に追加
      S += q[i][2]
      continue

print(S)

Submission status: Time Limit Exceeded

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

ans = "".join([F, s, R])
if z % 2 == 0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

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

for i in range(q):
    t_f_c = list(input().split())
    if t_f_c[0] == "1":
        s = s[::-1]
    elif t_f_c[0] == "2":
        if t_f_c[1] == "1":
            s = t_f_c[2] + s
        elif t_f_c[1] == "2":
            s = s + t_f_c[2]
print(s)

Submission status: Time Limit Exceeded

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

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

rev = False
li_str = [s]
for q in li_q:
    if q[0] == '1':
        rev = not rev
    else:
        if q[1] == '1':
            if rev:
                li_str.append(q[2])
            else:
                li_str.insert(0, q[2])
        else:
            if rev:
                li_str.insert(0, q[2])
            else:
                li_str.append(q[2])
                
ans = ''.join(li_str)
if rev:
    ans = ans[::-1]
    
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    A = input()
    if A[0] == '1' and len(S)!=1:
        h = h + 1
    else:
        if h % 2 == 0:
            if A[2] == '1':
                S = A[4]+S
            else:
                S = S+A[4]
        else:
            if A[2] == '1':
                S = S+A[4]
            else:
                S = A[4]+S

if h % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

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

for _ in range(Q):
  A = sys.stdin.readline().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:
#!/usr/bin/env python3


def main():
    # Failed to predict input format
    S = input()
    Q = int(input())
    Queries = []

    cumsum_Queries = [0]
    current = 0
    for i in range(Q):
        q = input().split()
        if len(q) == 1:
            Queries.append(int(q[0]))
            current += 1
            cumsum_Queries.append(current)
        else:
            Queries.append([int(q[0]), int(q[1]), q[2]])
            cumsum_Queries.append(current)

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

    cumsum_Queries = [current - cumsum_Queries[i] for i in range(Q)]

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



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

    pass



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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

r_flag = False

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

if r_flag == False:
    print ''.join(S)
else:
    S.reverse()
    print ''.join(S)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

a = input()
q = int(input())

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

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    query= list(input().split()) 
    if query[0] == '1':
        S = S[::-1]
    elif query[0] == '2':
        if query[1] == '1':
            S = ''.join([query[2],S])
        elif query[1] == '2':
            S = ''.join([S,query[2]])

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
qn = int(input())
q = []
rev = False

for i in range(qn):
    qi = input().split()
    if qi[0] == "1":
        rev = not rev
    else:
        if rev:
            if qi[1] == "1":
                qi[1] = "2"
            else:
                qi[1] = "1"
        q.append((2, (int(qi[1]), qi[2])))
if rev:
    q.append((1, ()))
for i in range(len(q)):
    if q[i][0] == 1:
        s = s[::-1]
    if q[i][0] == 2:
        if q[i][1][0] == 1:
            s = q[i][1][1] + s
        else:
            s = s + q[i][1][1]
print(s)

Submission status: Time Limit Exceeded

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

ans = s
for i in range(q):
    qret = list(input().split())
    if qret[0] == "1":
        ans.reverse()
    else:
        f = qret[1]
        c = qret[2]
        if f == "1":
            ans.insert(0, c)
        else:
            ans.append(c)

print(''.join(ans))

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
flip = -1
strs = '/'
 
for i in range(q):
    query = list(map(str, input().split()))
    if query[0] == '1':
        flip = flip * -1
    elif query[0] == '2':
        if query[1] == '1':
            if flip == -1:
                strs = query[2] + strs
            else:
                strs = strs + query[2]
        else:
            if flip == -1:
                strs = strs + query[2]
            else:
                strs = query[2] + strs
 
if flip == -1:
    ans = strs.replace('/', s)
else:
    ans = strs[::-1].replace('/', s[::-1])
 
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

import sys
import math

debug = False

def log(text):
    if debug:
        print(text)


def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        # for line in sys.stdin:
        #     lines.append(line)
        lines.append(input())
        lines.append(input())
        q = int(lines[1])
        for _ in range(q):
            lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    
    s = lines[0]
    q = int(lines[1])
    query = []
    for i in range(2, 2+q):
        contents = lines[i].split(' ')
        query.append(contents)

    return (s, q, query)


def solve(s, q, query):

    tmp = s
    for contents in query:
        if contents[0] == '1':
            tmp = tmp[::-1]
        else:
            if contents[1] == '1':
                tmp = contents[2] + tmp
            else:
                tmp = tmp + contents[2]
            

    return tmp 


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Time Limit Exceeded

Solution code:
S=input()
q=int(input())
RV=0
A="A"
for i in range(q):
    IN=input()
    if IN=="1":
        RV+=1
    else:
        t,f,c=IN.split()
        if RV%2==0:
            if f=="1":
                A=c+A               
            else:
                A=A+c
        else:
            if f=="1":
                A=A+c
            else:
                A=c+A
if RV%2==0:
    for i in range(len(A)):
        if A[i]=="A":
            print(A[:i]+S+A[i+1:])
else:
    A=A[::-1]
    for i in range(len(A)):
        if A[i]=="A":
            print(A[:i]+S[::-1]+A[i+1:])

Submission status: Time Limit Exceeded

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

for b in Query:
    if b[0] == '1':
        S = S[::-1]
    else:
        if b[1] == '1':
            S = b[2] + S
        else:
            S = S + b[2]

print(S)

Submission status: Time Limit Exceeded

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

hanten = 0
t = [S]
for l in L:
    if len(l) == 1:
        hanten += 1
    elif (l[1] == '1' and hanten % 2 == 0) or (l[1] == '2' and hanten % 2 == 1):
        t.insert(0, l[2])
    else:
        t.append(l[2])
ans = ''.join(t)
if hanten % 2 == 1:
    ans = ''.join(list(reversed(ans)))
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(q):
    prototype = input()
    if prototype == '1':
        s_copy = ""
        for j in range(len(s)):
            s_copy = s_copy + s[(len(s) - 1) - j]
        
        s = s_copy
    else:
        num, f_num, c = prototype.split()
        
        if(f_num == '1'):
            s = c + s
        else:
            s = s + c

print(s)

Submission status: Time Limit Exceeded

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

first = []
end = []
f = 0
for _ in range(q):
    li = list(input().split())
    if len(li) == 1:
        f = ~f
    else:
        if li[1] == '1':
            if f == 0:
                first = [li[2]] + first
            else:
                end += [li[2]]
        else:
            if f == 0:
                end += [li[2]]
            else:
                first = [li[2]] + first
s = first + s + end
if f == -1:
    s = s[::-1]
    
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


def main():
    S = deque(list(input()))
    Q = int(input())
    for _ in range(Q):
        query = list(input().split())
        if query[0] == "1":
            S.reverse()
        if query[0] == "2":
            if query[1] == "1":
                S.appendleft(query[2])
            elif query[1] == "2":
                S.append(query[2])
    ans = "".join(S)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def sol():
    s=input().rstrip()
    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()
q = int(input())
for i in range(q):
    l = input()
    if l[0] == '1':
        s=''.join(list(reversed(s)))
    else:
        if l[2] == '1':
            s = l[4]+s
        else:
            s += l[4]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
d = deque()
d.extend(input())
q = int(input())
for i in range(q):
  qu = input().split()
  if qu[0] == "1":
    d.reverse()
  else:
    if qu[1] == "1":
      d.extendleft(qu[2])
    else:
      d.extend(qu[2])

print(''.join(list(d)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys
input2 = sys.stdin.readline()

s=list(input2)
Q=int(input())

d=deque(s)
d.pop()
cnt=0
for i in range(Q):
    tmp=list(input())
    if tmp[0] == "1":
        cnt+=1
    else:
        if tmp[2] == "1":
            if (cnt > 0) and (cnt%2 == 0):
                d.appendleft(tmp[4])
                cnt=0
            elif cnt > 0:
                d.reverse()
                d.appendleft(tmp[4])
                cnt=0
            else:
                d.appendleft(tmp[4])
        else:
            if (cnt > 0) and (cnt%2 == 0):
                d.append(tmp[4])
                cnt=0
            elif cnt > 0:
                d.reverse()
                d.append(tmp[4])
                cnt=0
            else:
                d.append(tmp[4])
if (cnt > 0) and (cnt%2 != 0):
    d.reverse()
ans=list(d)
answer="".join(ans)
print(answer)

Submission status: Time Limit Exceeded

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


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

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

    flg = True
    for query in queries:
        if "1" == query[0]:
            flg = not flg
        elif "2" == query[0]:
            if ("1" == query[1] and flg) or ("2" == query[1] and not flg):
                s = query[2] + s
            else:
                s += query[2]

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

current = a

for i in range(Q):
  tmp = input().split()
  if tmp[0] == '1':
    current = current[::-1]
  else:
    if tmp[1] == '1':
      current = tmp[2] + current
    else:
      current = current + tmp[2]
      
print(current)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
x = 1
for i in range(Q):
    a, *b = input().split()
    if a == "1":
        x *= -1
    elif a == "2":
        c, d = b
        if c == "1":
            if x == -1:
                S.append(d)
            else:
                S.insert(0, d)
        elif c == "2":
            if x == -1:
                S.insert(0, d)
            else:
                S.append(d)
if x == -1:
    S = S[::-1]
print(*S, sep="")

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
m = 0
for i in range(Q):
  q = input()
  if q == '':
    break
  if q == '1':
    m += 1
  else:
    _, f, c = q.split(' ')
    f = int(f)
    if (m%2 + f) % 2 == 0: #元順で末尾または逆順で先頭
      S = S + c
    else:
      S = c + S
if m % 2 == 1:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
letter=input()
n=int(input())
for i in range(n):
    ope=input().split(" ")
    o1=int(ope[0])
    if o1 ==1:
        letter="".join(list(reversed(letter)))
    else:
        o2,o3_s=int(ope[1]),ope[2]
        if o2==1:
            letter=o3_s+letter
        else:
            letter=letter+o3_s
print(letter)

Submission status: Time Limit Exceeded

Solution code:
ans = input()
odr = 0

N = int(input())

for _ in range(N):
    Q = input()
    
    if Q == '1':
        ans = ans[::-1]
        continue
    
    _, F, C = Q.split()
    if F == '1':
        ans = C + ans
    else:
        ans = ans + C
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
r = False
for _ in range(Q):
  qs = input().split(' ')
  if len(qs) == 1:
    r = not r
  else:
    if (qs[1] == '1' and (not r)) or (qs[1] == '2' and r):
      S = qs[2] + S
    else:
      S = S + qs[2]
if r:
  print(S[::-1])
else:
  print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()

flag = 0
cnt = 0
n = int(input())

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

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

Submission status: Time Limit Exceeded

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

query=[""]*Q
for i in range(Q):
    query[i]=input()

#print(query)

#Q is length
count=0
while(count<Q):
    if(count==0 or count==Q-1):
        a=query[count].split(" ")
        if(len(a)==1):
            S=S[::-1]
        else:
            if(a[1]=="1"):
                S=a[2]+S
            else:
                S+=a[2]
    else:
        a=query[count].split(" ")
        if(len(query[count+1].split(" "))==1 and len(a)==1):
            count+=2
            continue
        elif(len(a)==1):
            S=S[::-1]
        else:
            if(a[1]=="1"):
                S=a[2]+S
            else:
                S+=a[2]
    count+=1
    
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
n=int(input())
l = [list(input()) for _ in range(n)]

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