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:
List = []

def front(S,C):
    M = C + S
    return M
def back(S,C):
    M = S + C
    return M

S = input()
n = int(input())
for i in range(n):
    List.append(list(input().split()))

#print(S,n,List)

for i in range(n):
    if List[i][0] == "1":
        S = S[::-1]
        #print("1")
    elif  List[i][0] == "2" :
        if List[i][1] == "1":
            S = front(S,List[i][2])
            #print("2")
        else :
            S = back(S, List[i][2])
            #print("3")

print(S)

Submission status: Time Limit Exceeded

Solution code:
List = []
def inverse(S):
    M = ''
    for i in range(len(S)):
        M = M +(S[(len(S))-i-1])

    return M

def front(S,C):
    M = C + S
    return M
def back(S,C):
    M = S + C
    return M

S = input()
n = int(input())
for i in range(n):
    List.append(list(input().split()))

#print(S,n,List)

for i in range(n):
    if List[i][0] == "1":
        S = inverse(S)
        #print("1")
    elif  List[i][0] == "2" :
        if List[i][1] == "1":
            S = front(S,List[i][2])
            #print("2")
        else :
            S = back(S, List[i][2])
            #print("3")

print(S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

# import copy
# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
INF = sys.maxsize
# MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    s = input()
    n = ni()

    cnt = 0
    for i in range(n):
        d = list(sys.stdin.readline().split())
        if int(d[0]) == 1:
            cnt = (cnt + 1) % 2
        else:
            if not cnt:
                if int(d[1]) == 1:
                    s = d[2] + s
                else:
                    s = s + d[2]
            else:
                if int(d[1]) == 1:
                    s = s + d[2]
                else:
                    s = d[2] + s

    if cnt:
        s = s[::-1]

    print(s)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flag = 0   #"1" represents inversion
for i in range(Q):
    quary = list(map(str, input().split()))
    if quary[0] == "1":
        if flag == 0:
            flag = 1
        else:
            flag = 0
    else:
        if flag == 1:
            if quary[1] == "1":
                S = S+quary[2]
            else:
                S = quary[2]+S
        else:
            if quary[1] == "1":
                S = quary[2]+S
            else:
                S = S+quary[2]
if flag == 1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

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

dire = 0
front = []
back = []

for i in range(Q):
    quary = [_ for _ in input().split()]
    if len(quary) == 1:
        T = quary[0]
        dire += 1
    else:
        T, F, C = quary
        if F == "1":
            if dire % 2 == 0:
                front += [C]
            else:
                back += [C]
        else:
            if dire % 2 == 1:
                front += [C]
            else:
                back += [C]

for f in front:
    S = f + S
for b in back:
    S = S + b

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

Submission status: Time Limit Exceeded

Solution code:
S = input()  #文字
Q = int(input())  #数字
X = [0] * Q
R = 1
for i in range(Q):
    X[i] = list(input().split() ) #文字、複数行複数列
    if X[i][0] == "1":
        R *= - 1
    else:
        T = int(X[i][1])
        if T * R == 1 or T * R == -2:
            S = X[i][2] + S
        else:
            S = S + X[i][2]                

print(S[::R])

Submission status: Time Limit Exceeded

Solution code:
S = input()  #文字
Q = int(input())  #数字
X = [0] * Q
R = 1
for i in range(Q):
    X[i] = list(input().split() ) #文字、複数行複数列
    if X[i][0] == "1":
        R *= - 1
    else:
        if X[i][1] == "1":
            if R == 1:
                S = X[i][2] + S
            else:
                S = S + X[i][2]                
        else:
            if R == 1:
                S = S + X[i][2]                
            else:
                S = X[i][2] + S

print(S[::R])

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

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

s = input()
q = int(input())
# print(s,q)
head = True
for i in range(q):
    query = input().split(' ')
    # print(query)
    if query[0]=='1':
        head = not head
    else:
        if query[1]=='1':
            if head:
                s = query[2]+s
            else:
                s += query[2]
        else:
            if head:
                s += query[2]
            else:
                s = query[2]+s
    # print(s)
if head:
    print(''.join(s))
else:
    print(''.join(s[::-1]))

Submission status: Time Limit Exceeded

Solution code:
S = input()  #文字
Q = int(input())  #数字
X = [0] * Q
for i in range(Q):
    X[i] = list(input().split() ) #文字、複数行複数列
    if X[i][0] == "1":        
        S = S[::-1]
    else:
        if X[i][1] == "1":
            S = X[i][2] + S
        else:
            S = S + X[i][2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()  #文字
Q = int(input())  #数字
X = list(input().split() for i in range(Q)) #文字、複数行複数列

#print(X)

for i in range(Q):
    if X[i][0] == "1":
        S = S[::-1]
    else:
        if X[i][1] == "1":
            S = X[i][2] + S
        else:
            S = S + X[i][2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
f = False
# Falseだと左が先頭
for _ in range(q):
    t = input().split()
    if t[0]=="1":
        f = not f
    elif t[0]=="2":
        if f:
            t[2]=t[2][::-1]
            if t[1]=="1":
                s = s+t[2]
            elif t[1]=="2":
                s = t[2]+s
        else:
            if t[1]=="1":
                s = t[2]+s
            elif t[1]=="2":
                s = s+t[2]
if f:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for _ in range(q):
    t = input().split()
    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())
check=True
for i in range(q):
    a=input().split()
    if(a[0]=="1"):
        check= not check
    else:
        if(a[1]=="1"):
            if(check):
                s=a[2]+s
            else:
                s+=a[2]
        else:
            if(check):
                s+=a[2]
            else:
                s=a[2]+s
print(s if check else s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
check=True
for i in range(q):
    a=input().split()
    if(a[0]=="1"):
        check= not check
    else:
        if(a[1]=="1"):
            if(check):
                s=a[2]+s
            else:
                s+=a[2]
        else:
            if(check):
                s+=a[2]
            else:
                s=a[2]+s
ans=''
for i in range(1,len(s)+1):
    ans+=s[-i]
print(s if check else ans)

Submission status: Time Limit Exceeded

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

up = 0

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

    if query[0] == "1":
        if up:
            up = 0
        else:
            up = 1

    elif query[0] == "2":
        if query[1] == "1":
            if up:
                s = s + query[2]
            else:
                s = query[2] + s
        elif query[1] == "2":
            if up:
                s = query[2] + s
            else:
                s = s + query[2]

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

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 = query[2] + s
        elif query[1] == "2":
            s = s + query[2]

print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    temp=list(input().split())
    
    if temp[0]=="1":
        S=S[::-1]
        
    else:
        if temp[1]=="1":
            S=temp[2]+S
            
        else:
            S=S+temp[2]
            
print(S)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu May  7 13:56:40 2020

@author: shinba
"""
import sys
from collections import deque

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

dq = deque(s)

for i in range(q):
    t = sys.stdin.readline().rstrip().split()
    if t[0] == "1":
        dq.reverse()
    else:
        if t[1] == "1":
            dq.appendleft(t[2])
        else:
            dq.append(t[2])

print("".join(list(dq)))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu May  7 13:25:51 2020

@author: shinba
"""
import sys

S = input()
Q = int(input())
data = [sys.stdin.readline().rstrip().split() for i in range(Q)]

for i in data:
    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:
S = input()
Q = int(input())
cnt = 0
for i in range(Q):
  x = list(input().split())
  if(int(x[0]) == 1):
    cnt += 1
  else:
    if((cnt+int(x[1])) % 2 == 1):
      S = x[2] + S
    else:
      S = S + x[2]
if(cnt % 2 == 1):
  print(S[::-1])
else:
  print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
sousa=[list(map(str,input().split())) for i in range(Q)]
counter=0
up=''
down=''
for i in sousa:
    if i[0]=='1':
#         S=S[::-1]
        counter+=1
    if i[0]=='2':
        if counter%2==0:
            if i[1]=='1':
                up=i[2]+up
            if i[1]=='2':
                down=down+i[2]
        else:
            if i[1]=='1':
                down=down+i[2]
            if i[1]=='2':
                up=i[2]+up   
                
S=up+S+down
if counter%2==1:
    S=S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in sousa:
    if i[0]=='1':
        S=S[::-1]
    if i[0]=='2':
        if i[1]=='1':
            S=i[2]+S
        if i[1]=='2':
            S=S+i[2]
print(S)

Submission status: Time Limit Exceeded

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

for i in sousa:
    if int(i[0])==1:
        S=S[::-1]
    if int(i[0])==2:
        if int(i[1])==1:
            S=i[2]+S
        if int(i[1])==2:
            S=S+i[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
q = int(input())
Q = [list(map(str, input().split())) for i in range(q)]

ans = deque(s)

reverse_num = 0
for i in range(q):
  if int(Q[i][0]) == 1:
    reverse_num += 1
  else:
    if int(Q[i][1]) == 1:
      if reverse_num % 2 == 0:
        ans.appendleft(Q[i][2])
      else:
        ans.append(Q[i][2])
    else:
      if reverse_num % 2 != 0:
        ans.appendleft(Q[i][2])
      else:
        ans.append(Q[i][2])

res = ''
if reverse_num % 2 == 0: 
  for i in range(len(ans)):
    res += ans[i]
  print(res)
else:
  for i in range(len(ans)):
    res += ans[-i-1]
  print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
q = int(input())
Q = [list(map(str, input().split())) for i in range(q)]

ans = deque(s)

reverse_num = 0
for i in range(q):
  if int(Q[i][0]) == 1:
    reverse_num += 1
  else:
    if int(Q[i][1]) == 1:
      if reverse_num % 2 == 0:
        ans.appendleft(Q[i][2])
      else:
        ans.append(Q[i][2])
    else:
      if reverse_num % 2 != 0:
        ans.appendleft(Q[i][2])
      else:
        ans.append(Q[i][2])

res = ''
for i in range(len(ans)):
  res += ans[i]
if reverse_num % 2 == 0:   
  print(res)
else:
  print(res[::-1])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

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

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = input()
q = int(input())
ans = deque(s)
for _ in range(q):
    inp = readline().rstrip()
    if inp[0] == "2":
        if inp[2] == "1":
            ans.appendleft(inp[4])
        else:
            ans.append(inp[4])
    else:
        ans = deque(list(ans)[::-1])

print("".join(ans))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
dir=True
ans=s
query=[]
for i in range(q):
  temp=input()
  if temp[0] == "1":
    dir = dir ^ True
  if temp[0] == "2":
    t,f,c = temp.split()

    if (f == "1" and dir) or (f=="2" and not dir):
      ans=c+ans
    else:
      ans=ans+c
def reverse(s):
  t=""
  for i in range(len(s)-1,-1,-1):
    t+=s[i]
  return t

if dir:
  print(ans)
else:
  print(reverse(ans))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
dir=True
ans=s
query=[]
for i in range(q):
  temp=input()
  if temp[0] == "1":
    dir = dir ^ True
  if temp[0] == "2":
    t,f,c = temp.split()

    if (f == "1" and dir) or (f=="2" and not dir):
      ans=c+ans
    else:
      ans=ans+c
      
if dir:
  print(ans)
else:
  print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
dir=True
ans=s
for i in range(q):
  temp=input()
  if temp[0] == "1":
    dir = dir ^ True
  if temp[0] == "2":
    t,f,c = temp.split()
    ff = False
    if dir:
      if f == "1":
        ans=c+ans
      else:
        ans=ans+c
    else:
      if f == "1":
        ans=ans+c
      else:
        ans=c+ans

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


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

    ans = deque(list(S))

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

        if (T[0] == "1"):
            ans.reverse()
        else:
            if (T[1] == "1"):
                ans.appendleft(T[2])
            else:
                ans.append(T[2])
            pass

    print("".join(ans))


resolve()

Submission status: Time Limit Exceeded

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

    ans = list(S)

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

        if (T[0] == "1"):
            ans = ans[::-1]
        else:
            if (T[1] == "1"):
                ans.insert(0, T[2])
            else:
                ans.append(T[2])
            pass

    print("".join(ans))


resolve()

Submission status: Time Limit Exceeded

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

n = S
# r = S
# for i in range(Q):
#     op = input().split()
#     if len(op) == 1:
#         n, r = r, n
#     elif op[1] == "1":
#         n = op[2] + n
#         r = r + op[2]
#     else:
#         n = n + op[2]
#         r = op[2] + r
# print(n)

mode = 0
operations = []
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if op[0] == "1":
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        n = "".join([n, op[2]])
    else:
        n = "".join([op[2], n])
if mode == 0:
    print(n)
else:
    print(n[::-1])

Submission status: Time Limit Exceeded

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

n = S
# r = S
# for i in range(Q):
#     op = input().split()
#     if len(op) == 1:
#         n, r = r, n
#     elif op[1] == "1":
#         n = op[2] + n
#         r = r + op[2]
#     else:
#         n = n + op[2]
#         r = op[2] + r
# print(n)

mode = 0
operations = []
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if len(op) == 1:
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        n = n + op[2]
    else:
        n = op[2] + n
if mode == 0:
    print(n)
else:
    print(n[::-1])

Submission status: Time Limit Exceeded

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

n = S
# r = S
# for i in range(Q):
#     op = input().split()
#     if len(op) == 1:
#         n, r = r, n
#     elif op[1] == "1":
#         n = op[2] + n
#         r = r + op[2]
#     else:
#         n = n + op[2]
#         r = op[2] + r
# print(n)

mode = 0
for i in range(Q):
    op = input().split()
    if len(op) == 1:
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        n = n + op[2]
    else:
        n = op[2] + n
if mode == 0:
    print(n)
else:
    print(n[::-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = deque(input())
q = int(input())
l = list(list(input().split()) for _ in range(q))
for i in l:
  if len(i)==1:
    s.reverse()
  else:
    a,f,c = i
    if f=="1":
      s.appendleft(c)
    else:
      s.append(c)
      
print(*s,sep="")

Submission status: Time Limit Exceeded

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

for i in range(q):
    L = list(input().split())
    if L[0] == '1' :
        head = (head + 1)%2
    if L[0] == '2' :
        if head == 0:
            if L[1] == '1' :
                s = [L[2]] + s
            if L[1] == '2' :
                s = s + [L[2]]
        if head == 1:
            if L[1] == '1' :
                s = s + [L[2]]
            if L[1] == '2' :
                s = [L[2]] + s
if head == 0:
    print(''.join(s))
else: 
    print(''.join(reversed(s)))

Submission status: Time Limit Exceeded

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

for i in range(q):
    L = list(input().split())
    if L[0] == '1' :
        head = (head + 1)%2
    if L[0] == '2' :
        if head == 0:
            if L[1] == '1' :
                s = L[2] + s
            if L[1] == '2' :
                s = s + L[2]
        if head == 1:
            if L[1] == '1' :
                s = s + L[2]
            if L[1] == '2' :
                s = L[2] + s
if head == 0:
    print(s)
else: 
    s = ''.join(reversed(s))
    print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    L = list(input().split())
    if L[0] == '1' :
        s = ''.join(reversed(s))
    if L[0] == '2' :
        if L[1] == '1' :
            s = L[2] + s
        if L[1] == '2' :
            s = s + L[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:

s = input()
q = int(input())
L = []
for i in range(q):
    L.append(list(input().split()))
    if L[-1][0] == '1' :
        s = ''.join(reversed(s))
    if L[-1][0] == '2' :
        if L[-1][1] == '1' :
            s = L[-1][2] + s
        if L[-1][1] == '2' :
            s = s + L[-1][2]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

reverse_num = 0
for i in range(q):
  if int(Q[i][0]) == 1:
    reverse_num += 1
  else:
    if int(Q[i][1]) == 1:
      if reverse_num % 2 == 0:
      	s = Q[i][2] + s
      else:
        s = s + Q[i][2]
    else:
      if reverse_num % 2 == 0:
      	s = s + Q[i][2]
      else:
        s = Q[i][2] + s
        
if reverse_num % 2 == 0:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

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

for i in range(q):
  if int(Q[i][0]) == 1:
    s = s[::-1]
  else:
    if int(Q[i][1]) == 1:
      s = Q[i][2] + s
    else:
      s = s + Q[i][2]
      
print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
  if int(Q[i][0]) == 1:
    s = s[::-1]
  else:
    if int(Q[i][1]) == 1:
      s = Q[i][2] + s
    else:
      s = s + Q[i][2]
      
print(s)

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[0]=="1":
        rev=rev+1
    else:
        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:
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def main():
    s = input().rstrip()
    n = int(input())
    li = [input().split() for i in range(n)]
    a = 0 #先頭がどっちかという情報

    for x in li:
        if x[0] == "1":
            if a == 0:
                a = 1
            else:
                a = 0
        else:
            if (x[1] == "1" and a == 0) or (x[1] == "2" and a == 1):
                s = x[2] + s
            else:
                s = s + x[2]

    if a == 0: #先頭が前なら
        print(s)
    else: #先頭が後なら
        print(s[::-1])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    n = int(input())
    li = [input().split() for i in range(n)]
    a = 0 #先頭がどっちかという情報

    for x in li:
        if x[0] == "1":
            if a == 0:
                a = 1
            else:
                a = 0
        else:
            if (x[1] == "1" and a == 0) or (x[1] == "2" and a == 1):
                s = x[2] + s
            else:
                s = s + x[2]

    if a == 0: #先頭が前なら
        print(s)
    else: #先頭が後なら
        print(s[::-1])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
li = [input().split() for i in range(n)]
a = 0 #先頭がどっちかという情報

for x in li:
    if x[0] == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        if (x[1] == "1" and a == 0) or (x[1] == "2" and a == 1):
            s = x[2] + s
        else:
            s = s + x[2]

if a == 0: #先頭が前なら
    print(s)
else: #先頭が後なら
    print(s[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for x in li:
    if x[0] == "1":
        s = s[::-1]
    else:
        if x[1] == "1":
            s = x[2] + s
        else:
            s = s + x[2]

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
def I(): return int(input())  # '123' -> 123
def Is(): return map(int, input().split())  # '123 456' -> 123, 456
def LI(): return list(map(int, input().split()))  # '123 456' -> [123, 456]
def TI(): return tuple(map(int, input().split()))  # '123 456' -> (123, 456)
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  # 'aa bb' -> 'aa','bb' or ['aa','bb']
def LS(): return list(input())  # 'abc123' -> ['a','b','c','1','2','3']
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

s = S()
q = I()
flag = 0

for i in range(q):
    query = input()
    if query[0] == '1':
        flag = flag ^ 1  # flagを反転
    else:
        if (query[2] == '1' and not flag) or (query[2] == '2' and flag):
            s = query[4] + s
        else:
            s = s + query[4]

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
import sys
def I(): return int(input())  # '123' -> 123
def Is(): return map(int, input().split())  # '123 456' -> 123, 456
def LI(): return list(map(int, input().split()))  # '123 456' -> [123, 456]
def TI(): return tuple(map(int, input().split()))  # '123 456' -> (123, 456)
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  # 'aa bb' -> 'aa','bb' or ['aa','bb']
def LS(): return list(input())  # 'abc123' -> ['a','b','c','1','2','3']
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

s = S()
q = I()
Q = SsR(q)
flag = 0

# print(Q)

for x in Q:
    tmp = ""
    if x[0] == '1':
        flag = flag ^ 1  # flagを反転
    else:
        if (x[1] == '1' and not flag) or (x[1] == '2' and flag):
            tmp = x[2] + s
        else:
            tmp = s + x[2]
        s = tmp

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
s  = input()
n  = int(input())
#  = map(int, input().split())
#  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

is_seiichi = True

for _ in range(n):
    ipt = input()

    if ipt[0] == '1':
        is_seiichi = not is_seiichi
    else:
        _, id_, s_ = ipt.split()
        is_head = True if id_ == '1' else False
        if is_seiichi ^ is_head:
            s = s + s_
        else:
            s = s_ + s

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