Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Time Limit Exceeded

Solution code:
s = input()
Q = int(input())
rev_count = 0

for i in range(Q):
    a = input()
    if a[0] == "1":
        rev_count += 1
    else:
        b = a.split()
        if rev_count % 2 == 0:
            if b[1] == "1":
                s = b[2] + s
            else:
                s = s + b[2]
        else:
            if b[1] == "1":
                s = s + b[2]
            else:
                s = b[2] + s

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    query = input()
    if query == "1":
        s = s[::-1]
    else:
        q2 = query.split()
        if q2[1] == "1":
            s = q2[2]+s
        else:
            s = s + q2[2]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

#整数値龍力　複数の入力
def input_multiple_number():
    return map(int, input().split())

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

preffix = ""
suffix = ""
revcnt = 0
for i in range(Q):
    query = input()
    if query[0] == "1":
        wk = preffix
        preffix = suffix
        suffix = wk
        suffix = suffix[::-1]
        preffix = preffix[::-1]
        revcnt += 1
    elif query[2] == "1":
        preffix = (query[4]+preffix)
    else:
        suffix += query[4]

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

print(preffix+S+suffix)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
flag = 0
cnt = 0

for i in range(q):
    t = input()
    if len(t) == 1:
        cnt += 1
        if flag == 0:
            flag = 1
        elif flag == 1:
            flag = 0
    else:
        _, a, b = (str(i) for i in t.split())
        if a == "1":
            if flag == 0:
                s = b + s
            elif flag == 1:
                s = s + b
        elif a == "2":
            if flag == 0:
                s = s + b
            elif flag == 1:
                s = b + s

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

Submission status: Time Limit Exceeded

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

flag = False
for _ in range(Q):
    IN = list(map(str,input().split()))

    if IN[0] == "1":
        flag = not flag
        continue

    _,F,C = IN
    if flag:
        if F == "1":
            S += C
        else:
            S = C + S
    else:
        if F == "1":
            S = C + S
        else:
            S += C

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
for i in range(Q):
  A=list(input().split())
  if len(A)==1:
    S=S[::-1]
  else:
    if int(A[1])==1:
      S=A[-1]+S
    else:
      S=S+A[-1]
print(S)

Submission status: Time Limit Exceeded

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

if __name__=="__main__":
    sol()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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


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

    if line[0] == "1":
        rev = not rev

    else:
        if line[2] == "1":
            if rev:
                S = S + line[4]
            else:
                S = line[4] + S
        else:
            if rev:
                S = line[4] + S
            else:
                S = S + line[4]

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

Submission status: Time Limit Exceeded

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

ans = S
for query in Query:
    T = query[0]
    if T == '1':
        # Sの前後を反転
        ans = ans[::-1]
    else:
        F = query[1]
        if F == '1':
            # 文字列Sの先頭にCを追加
            ans = query[2] + ans
        else:
            # 文字列Sの末尾にCを追加
            ans = ans + query[2]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
q=int(input())
Q=[]
state=True
ans=deque(S)
for _ in range(q):
    t = input().split()
    if t[0] == "1":
        Q.append(int(t[0]))
    elif t[0] == "2":
        Q.append((int(t[0]),int(t[1]),t[2]))
for que in Q:
    if que == 1:
        state = not state
    else:
        if state:
            if que[1] == 1:
                ans.appendleft(que[2])
            else:
                ans.append(que[2])
        else:
            if que[1] == 2:
                ans.appendleft(que[2])
            else:
                ans.append(que[2])
anss=""
if not state:
    while len(ans) >0:
        anss += ans.pop()
else:
    while len(ans) >0:
        anss += ans.popleft()
print(anss)

Submission status: Time Limit Exceeded

Solution code:
#input = raw_input

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

rev = False
for i in range(q):
    elms = input().split(" ")
    t = int(elms[0])
    if t == 1:
        rev = not rev
    else:
        f = int(elms[1])
        c = elms[2]

        if f == 1:
            if rev:
                s = s + c
            else:
                s = c + s
        else:
            if rev:
                s = c +s
            else:
                s = s + c
if rev:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
p = 1
start = ""
end = ""
query =[]
for i in range(q):
    a = list(map(str,input().split()))
    query.append(a)

for i in range(q):
    if int(query[i][0]) == 1:
        p = p * (-1)
    else:
        if int(query[i][1]) == 1:
            if p ==1:
                start = query[i][2] + start
            else:
                end = query[i][2] + end
        else:
            if p == 1:
                end = query[i][2] + end
            else:
                start = query[i][2] + start
if p == 1:
    ans = start + s + end[::-1]
else:
    ans = end + s[::-1] + start[::-1]
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q) :
    query = input()
    if query == "1" :
        S = "".join(list(reversed(S)))
    else :
        a, F, C = query.split()
        if F == "1" :
            S = C + S
        else :
            S = S + C

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
c = 0
for _ in range(q):
    t = input()
    if t != "1":
        t = t.split()
        if t[1] == "1":
            if c % 2 == 0:
                s.insert(0, t[2])
            else:
                s.append(t[2])
        else:
            if c % 2 == 0:
                s.append(t[2])
            else:
                s.insert(0, t[2])
    else:
        c += 1

if c % 2 == 1:
    s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

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

for _ in range(q):
    query = input()
    if query == "1":
        s = s[::-1]
    else:
        _, f, c = query.split(" ")
        s = c + s if f == "1" else s + c

print(s)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
import fractions
import heapq
import collections
import bisect
import sys
import copy

sys.setrecursionlimit(10**9)
mod = 10**7+9
inf = 10**20

s = input()
q = int(input())
for i in range(q):
    l = input().split()
    if l[0]=="1":
        s = s[::-1]
    else:
        if l[1]=="1":
            s = l[2]+s
        else:
            s += l[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
flip = False
for i in range(q):
    tfc = input()
    if tfc[0] == '1':
        flip = not flip
    else:
        if flip:
            if tfc[2] == '1':
                s.append(tfc[4])
            else:
                ｓ.insert(0, tfc[4])
        else:
            if tfc[2] == '1':
                ｓ.insert(0, tfc[4])
            else:
                s.append(tfc[4])
if flip:
    s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

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

S = input().rstrip()
Q = int(input())
Query = []

for _ in range(Q):
    Query.append(input().rstrip().split())

for i in Query:
    if i[0] == "1":
        S = S[::-1]
    elif i[1] == "1":
        S = i[2] + S
    else:
        S += i[2]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
L = []
for i in range(Q):
    l = list(input().split())
    if len(l) == 1:
        S.reverse()
    else:
        l[1] = int(l[1])
        if l[1] == 1:
            S.insert(0,l[2])
        else:
            S.append(l[2])
        
print(''.join(S))

Submission status: Time Limit Exceeded

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

for i in range(q):
    query = list(input().split())
    if len(query) == 1:
        s = ''.join(list(reversed(s)))
    elif 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 = list(input())
q = int(input())
for i in range(q):
  t = input().split()
  if t[0] == "1":
    s = s[::-1]
  else:
    if t[1] == "1":
      s = [t[2]]+s
    else:
      s = s+[t[2]]
print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    reverse = False
    front = ""
    back = ""
    for _ in range(Q):
        TFC = list(input().split())
        if len(TFC) == 1:
            reverse = not(reverse)
        else:
            t,f,c = TFC
            if reverse:
                if f == "2":
                    front += c
                else:
                    back += c

            else:
                if f == "1":
                    front += c
                else:
                    back += c
    if reverse:
        print(back[::-1] + S[::-1] + front)
    else:
        print(front[::-1] + S + back)
    return

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    query = []
    for q in range(Q):
        query.append(input())

    for q in query:
        if len(q.split(' ')) > 1:
            T, F, C = q.split(' ')
            if F == '1':
                S = C + S
            elif F == '2':
                S = S + C
        elif q == '1':
            S = S[::-1]

    print(S)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

hanten = 0
sen = ""
mat = ""
for i in range(q):
    x = input()
    if x == "1":
        if hanten == 0:
            hanten = 1
        else:
            hanten = 0
    else:
        if x[2] == "1":
            if hanten == 0:
                sen += x[4]
            else:
                mat += x[4]
        else:
            if hanten == 0:
                mat += x[4]
            else:
                sen += x[4]

if hanten == 0:
    print(sen[::-1] + s + mat)
else:
    print(mat[::-1] + s[::-1] + sen)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
r = False
for i in range(q):
    tmp = list(input().split(' '))
    if tmp[0]=='1':
        r = not r
    elif (not r and tmp[1]=='1') or (r and tmp[1] =='2'):
        s = tmp[2]+s
    else:
        s = s + tmp[2]
if not r:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

h = ''
t = ''
reverse = False

for q in QL:
    if q[0] == '1':
        reverse = not reverse
    else:
        if q[1] == '1':
            if reverse:
                t = t+q[2]
            else:
                h = q[2]+h
        else:
            if reverse:
                h = q[2]+h
            else:
                t = t+q[2]

s = h + S + t
if reverse:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

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

def do(S, method):
    if method[0] == "1":
        return S[::-1]
    elif method[0] == "2":
        if method[1] == "1":
            return method[2] + S
        else:
            return S + method[2]

for _ in range(N):
    S = do(S, input().split(" "))

print(S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

rev = False

for i in range(q):
    tfc = input()
    if len(tfc) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        t, f, c = tfc.split()
        if f == '1':
            if rev:
                s = s+c
            else:
                s = c+s
        else:
            if rev:
                s = c+s
            else:
                s = s+c

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    op = input().split()
    if op[0] == "1":
        s.reverse()
    else:
        if op[1] == "1":
            s.insert(0, op[2])
        else:
            s.append(op[2])
    
for i in s:
    print(i, end='')
print()

Submission status: Time Limit Exceeded

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

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        s = s[::-1]
    else:
        if t[1] == "1":
            s = t[2] + s
        else:
            s = s + t[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
s = str(stdin.readline().rstrip())
q = int(stdin.readline().rstrip())
for i in range(q):
    query = list(map(str, stdin.readline().rstrip().split()))
    if len(query)==1:
        #反転
        pass
        s = s[::-1]
    else:
        if int(query[1])==2:
            s += query[2]
        else:
            s = query[2] + s

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()

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

Submission status: Time Limit Exceeded

Solution code:
# from fractions import gcd
# mod = 10 ** 9 + 7
# N = int(input())
# a = list(map(int,input().split()))
# a,b,c = map(int,input().split())
# ans = [0] * N
# math.ceilで切り上げ
# dp = [[0] * 4 for i in range(3)] #2次元配列初期化
# dp = [[[0] * 2 for i in range(3)] for j in range(5)]
# (ord('A'))でASCII出力 Aは65
import math
import statistics
import string


def intinput():
    return int(input())
def listintinput():
    return list(map(int, input().split()))
def splitintinput():
    return map(int, input().split())
def factorialsurplus(x, y, z):  # xからyまでの階乗をzで割ったあまりを求める
    ret = 1
    for i in range(x, y + 1):
        ret = (ret * i) % z
    return (ret)
def isprime(x):
    if x == 1:
        return False
    if x == 2:
        return True
    sq = int(math.sqrt(x))
    for i in range(2,sq+1):
        if x % i == 0:
            return False
    return True
def hcfnaive(a,b):#最大公約数 math.gcdが使えない時の手段として入れとく。pythonが更新されたらいらなくなるはず
    if(b==0):
        return a
    else:
        return hcfnaive(b,a%b)

s = input()
Q = intinput()
rev_count = 0

for i in range(Q):
    a = input()
    if a[0] == "1":
        rev_count += 1
    else:
        b = a.split()
        if rev_count % 2 == 0:
            if b[1] == "1":
                s = b[2] + s
            else:
                s = s + b[2]
        else:
            if b[1] == "1":
                s = s + b[2]
            else:
                s = b[2] + s

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

Submission status: Time Limit Exceeded

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

a = list(input())
a.pop(-1)

Q = int(input())
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        a.reverse()
    else:
        if q[1] == "1":
            a.insert(0, q[2])
        else:
            a.append(q[2])
print("".join(a))

Submission status: Time Limit Exceeded

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

def change(s):
  t = ""
  for i in reversed(range(0, len(s))):
    t += s[i]
  return t

ans = S
for i in range(Q):
  q = list(map(str, input().split()))
  if(len(q)==1):
    ans = change(ans)
  else:
    if(q[1]=="1"):
      ans = q[2] + ans
    else:
      ans += q[2]
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
for i in range(int(input())):
    q=input()
    if q=='1':
        S=S[::-1]
    else:
        a,f,c=q.split()
        S=''.join((c,S)) if f=='1' else ''.join((S,c)) 
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 == ['1']:
        rev += 1
        continue
    if t[1] == '1':
        if rev % 2 == 0:
            s = [t[2]] + s
        else:
            s.append(t[2])

    else:
        if rev % 2 == 0:
            s.append(t[2])
        else:
            s = [t[2]] + s
if rev % 2 != 0:
    s.reverse()

print(''.join(s))

Submission status: Time Limit Exceeded

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

rev=0

for _ in range(q):
    temp=input().split(" ")
    
    if temp[0]=="1":
        if rev==0:
            rev=1
        else:
            rev=0
    else:
        if rev==0:
            if temp[1]=="1":
                s=temp[2]+s
            else:
                s=s+temp[2]
        else:
            if temp[1]=="2":
                s=temp[2]+s
            else:
                s=s+temp[2]

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

Submission status: Time Limit Exceeded

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

is_filp = False
for i in range(Q):
    q = input()

    if q[0] == '1':
        if is_filp:
            is_filp = False
        else:
            is_filp = True
    elif q[0] == '2':
        _, f, c = q.split(' ')
        if f == '1':
            if is_filp:
                S = S + c
            else:
                S = c + S 
        else:
            if is_filp:
                S = c + S
            else:
                S = S + c
if is_filp:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
p = 1
for i in range(q):
    x = input().split()
    if len(x) == 1:
        p *= -1
    else:
        f = int(x[1])
        c = x[2]
        if (p > 0 and f == 1) or (p < 0 and f == 2):
            s = c + s
        else:
            s = s + c
if p < 0:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for _ in range(q):
    query = input()
    if query[0] == '1':
        s = s[::-1]
    else:
        _, f, c = query.split()
        if f == '1':
            s.insert(0, c)
        else:
            s.append(c)

print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
flag = 0
cnt = 0

for i in range(q):
    t = input()
    if len(t) == 1:
        cnt += 1
        if flag == 0:
            flag = 1
        elif flag == 1:
            flag = 0
    else:
        _, a, b = (str(i) for i in t.split())
        if a == "1":
            if flag == 0:
                s = b + s
            elif flag == 1:
                s = s + b
        elif a == "2":
            if flag == 0:
                s = s + b
            elif flag == 1:
                s = b + s

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

Submission status: Time Limit Exceeded

Solution code:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

S = input()
Q = INT()

for _ in range(Q):
    Query = list(input().split())
    T = int(Query[0])
    if T == 1:
        S = S[::-1]
    else:
        F = int(Query[1])
        C = Query[2]
        if F == 1:
            S = C + S
        else:
            S = S + C
print(S)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input().rstrip()
qn = int(input())
mode = 0
for n in range(qn):
    qs = input().rstrip().split()
    if qs[0] == '1':
        mode = 1 - mode
    if qs[0] == '2':
        if qs[1] == '1':
            if mode == 0:
                s = qs[2] + s
            else:
                s = s + qs[2]
        else:
            if mode == 0:
                s = s + qs[2]
            else:
                s = qs[2] + s
if mode == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
length = len(s)
que = [[''] * 3 for i in range(q)]
for i in range(q):
    c = input()
    if c[0] == '1':
        que[i][0] = 1
    else:
        n, f, c = map(str, c.split())
        que[i][0], que[i][1], que[i][2] = int(n), int(f), c
for com in que:
    if com[0] == 1:
        s = s[::-1]
    else:
        if com[1] == 1:
            s = com[2] + s
        else:
            s += com[2]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
d = deque(s)
q = int(input())
for _ in range(q):
    buf = input().split()
    if buf[0] == '1':
        d.reverse()
    else:
        f, c = buf[1:]
        if f == '1':
            d.appendleft(c)
        else:
            d.append(c)
print(''.join(list(d)))

Submission status: Time Limit Exceeded

Solution code:
S = input()
additions = ['','']
direction = 0
Q = int(input())
for _ in range(Q):
    query = input().split()
    T = int(query[0])
    if T == 1:
        direction ^= 1
    else:
        F = int(query[1])
        C = query[2]
        additions[(F+direction+1) % 2] += C
if direction == 0:
    print(additions[0][::-1], end='')
    print(S, end='')
    print(additions[1])
else:
    print(additions[1][::-1], end='')
    print(S[::-1], end='')
    print(additions[0])

Submission status: Time Limit Exceeded

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

for i in range(q):
    buf = input().split()
    if(len(buf) == 1):
        s = s[::-1]
    else:
        if buf[1] == '1':
            s = buf[2] + s
        else:
            s = s + buf[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
input = raw_input

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

rev = False
for i in range(q):
    elms = input().split(" ")
    t = int(elms[0])
    if t == 1:
        rev = not rev
    else:
        f = int(elms[1])
        c = elms[2]

        if f == 1:
            if rev:
                s = s + c
            else:
                s = c + s
        else:
            if rev:
                s = c +s
            else:
                s = s + c
if rev:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
import queue
import copy
import bisect


def dfs(s, prev, pre):
  val[s] += Ope[s] + prev
  for i in G[s]:
    if(i == pre):
      continue
    dfs(i, val[s], s)

def bfs(sx, sy, n):
  que = deque([[sy,sx]])
  while que:
    y,x = que.popleft()
    for i,j in D:
      if(x+i<0 or y+j<0 or x+i>W-1 or y+j>H-1):
        continue
      dist = G[y+j][x+i]
      if(G[y+j][x+i] != "X"):
        if(type(G[y+j][x+i]) is str):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        elif(dist>G[y][x]+1):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        

#X = str(input()).split()
#a = [int(x) for x in input().split()]
#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]

S = str(input())
Q = int(input())
flag = 0


for i in range(Q):
  Que = str(input()).split()
  if(int(Que[0])==1):
    flag = 1 - flag
  else:
    if(flag == 1):
      if(int(Que[1])==1):
        S = S + Que[2]
      else:
        S = Que[2] + S
    else:
      if(int(Que[1])==1):
        S = Que[2] + S
      else:
        S = S + Que[2]

if(flag == 1):
  S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
re_flag = False 
start = ""
finish = ""
for i in range(Q):
    l = input()
    if l == "1":
        re_flag = not bool(re_flag)
    elif len(l) > 1:
        t, f, c = l.split()
        if f == "1":
            if re_flag:
                finish = finish + c
            else:
                start = c + start
        else:
            if re_flag:
                start = c + start
            else:
                finish = finish + c


S = start + S + finish
if re_flag:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
	x=input().split()
	if x[0]=="1": S.reverse()
	else:
		if x[1]=="1": S=[x[2]]+S
		else: S.append(x[2])

print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    s = input()[:-1]
    q = int(input())

    for _ in range(q):
        query = input()
        if query[0] == '1':
            s = s[::-1]
        else:
            if query[2] == '1':
                s = query[4] + s
            else:
                s += query[4]

    print(s)


if __name__ == '__main__':
    main()