Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

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

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

If T_i = 1: reverse the string S.

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

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

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

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

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

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

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

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

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

Operation 2: Reverse S. S becomes ap.

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

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

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

Sample Output 2

aabc

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

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



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


Sample output:
cpa

Submission status: Time Limit Exceeded

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


# 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():
    # Failed to predict input format
    S = [c for c in input()]
    Q = int(input())
    direction = 1

    for _ in range(Q):
        query = input().split()
        t = int(query[0])

        if t == 1:
            direction = direction *  -1

        elif t == 2:
            f = int(query[1])
            c = query[2]
            if f == 1:
                # add to front
                if direction == 1:
                    S = [c] + S
                # add to back
                if direction == -1:
                    S = S + [c]
            elif f == 2:
                # add to back
                if direction == 1:
                    S = S + [c]
                # add to front
                if direction == -1:
                    S = [c] + S
    if direction == -1:
        S = S[::-1]

    print(''.join(S))
    pass

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q =int(input())
q =[]*Q
for h in range(Q):
  q.append(input().split())
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())
 
f = 0
 
for _ in range(Q):
    q = input()
    if int(q[0]) == 1:
        f = abs(f-1)
        continue
    
    _, F, c = q.split()
    if f+int(F) % 2 == 1:
        s = c + s
    else:
        s = s + c
        
if f:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse = 0
head = []
tail = []
for i in range(Q):
    s = input().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(map(str, input()))
Q = int(input())
f = 0
for i in range(Q):
  Query = input().split()
  if Query[0] == '1':
    f = 1 - f
  else:
    if int(Query[1]) - f == 1:
      S = [Query[2]] + S
    else:
      S = S + [Query[2]]
if f == 1:
  S.reverse()
print(''.join(S))

Submission status: Time Limit Exceeded

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

a=[input().split() for i in range(Q)]


ans=S
for i in range(Q):
    if a[i][0]=="2":
        if a[i][1]=="1":
            ans=a[i][2]+ans
        else:
            ans=ans+a[i][2]
    else:
        re_ans=""
        for j in range(len(ans)):
            re_ans=re_ans+ans[len(ans)-1-j]
        ans=re_ans
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


S = deque(list(input()))
Q = int(input())
queries = []
for _ in range(Q):
    query = input().split()
    if len(query) == 3:
        queries.append([int(x) for x in query[:2]] + [query[2]])
    else:
        if len(queries[-1]) == 1:
            queries.pop()
        else:
            queries.append([int(x) for x in query])

for query in queries:
    if len(query) == 3:
        if query[1] == 1:
            S.appendleft(query[2])
        else:
            S.append(query[2])
    else:
        S.reverse()
print(''.join(S))

Submission status: Time Limit Exceeded

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

order = []
state = True

for n in range(N):
    order.append(input().split())


for n in range(N):
    if order[n][0] == '2':
        if (order[n][1] == '1') == state:
            S = order[n][2] + S
        else:
            S = S + order[n][2]
    elif order[n][0] == '1':
        if state:
            state = False
        else:
            state = True


if state:
    out = S
else:
    out = S[::-1]

print(out)

Submission status: Time Limit Exceeded

Solution code:
S = input().rstrip('\r\n')
Q = int(input())
for _ in range(Q):
    query = input().rstrip('\r\n')
    t, f, c = '', '', ''
    if len(query) == 1:
        t = query
    else:
        t, f, c = query.split(' ')
    if t == '1':
        S = S[::-1]
    else:
        if f == '1':
            S = c + S
        else:
            S = S + c
print(S)

Submission status: Time Limit Exceeded

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

h = 0
for i in range(n):
    qq = list(input().split())
    if qq[0] == '1':
        h += 1
    else:
        if qq[1] == '1':
            if h % 2 ==1:
                s = s + qq[2]
            else:
                s = qq[2] + s
        else:
            if h % 2 ==1:
                s = qq[2] + s
            else:
                s = s + qq[2]

if h % 2 ==1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

count = 0
ans = S

for i in range(Q):
    T = input().split()
    if len(T)==1:
        count+=1
        continue

    if count%2==0:
        if T[1] == '1': ans = T[2] + ans
        else: ans += T[2]
    else:
        if T[1] == '1': ans += T[2]
        else: ans = T[2] + ans

if count%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):
    quary = input().split()
    if quary[0]=='1':
        S = S[::-1]
    else:
        if quary[1]=='1':
            S = quary[2] + S
        else:
            S = S + quary[2]

print(S)

Submission status: Time Limit Exceeded

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

add_to_last = []
add_to_beginning = []

mode = 1
for i in range(Q):
  query = input()
  if query=='1':
    mode *= -1
  else:
    _, F, C = query.split()
    if F=='1':
      if mode==1:
        add_to_beginning.insert(0,C)
      else:
        add_to_last.append(C)
    else:
      if mode==1:
        add_to_last.append(C)
      else:
        add_to_beginning.insert(0,C)

if mode==1:
  print("".join(add_to_beginning), end="")
  print(S, end="")
  print("".join(add_to_last))
else:
  print("".join(add_to_last[::-1]), end="")
  print(S[::-1], end="")
  print("".join(add_to_beginning[::-1]))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Q_list = list()
for i in range(Q):
    Q_list.append(input().split())

S_list = [i for i in S]
def operate(i):
    global S_list
    if Q_list[i][0] == '1':
        S_list = list(reversed(S_list))
    else:
        Q_i = [k for k in Q_list[i][2]]
        if Q_list[i][1] == '1':
            S_list = Q_i + S_list
        else:
            S_list = S_list + Q_i
 
for i in range(Q):
    operate(i)
print(''.join(S_list))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def calculate(s,q,arr):
    s = list(s)
    for ar in arr:
        if int(ar[0]) == 1:
            s = list(reversed(s))

        if int(ar[0]) == 2:
            if int(ar[1]) == 1:
                s.insert(0,ar[2])
            else:
                s.append(ar[2])

    print("".join(s))



calculate(S,Q,ARR)

Submission status: Time Limit Exceeded

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

for query in query_q:
    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()
Q = int(input())
L = [list(input().split()) for i in range(Q)]

ans = S
hanten = 0
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 = [l[2], ans]
        ans = ''.join(t)
    else:
        t = [ans, 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 = list(map(int,input().split()))[0]

flg=1
count=0
for i in range(Q):
    Query = input().split()
    if Query[0]=='1':
        flg*=-1
        count+=1
        continue
    if flg==1:
        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 count%2==0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

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

T_list = []
for q in range(Q):
    T = stdin.readline()[:-1]

    if T == '1':
        S = S[::-1]

    else:
        t, f, c = T.split()

        if f == '1':
            S = c + S
        else:
            S = S + c

print(S)

Submission status: Time Limit Exceeded

Solution code:
str = input()
N = int(input())
q = list([input().split()])
for i in range(N-1):
    q_next = input().split()
    if len(q[-1]) == 1 and len(q_next) == 1:
        del q[-1]
        continue
    else:
        q.append(q_next)
for i in q:
    if (i[0] == '1'):
        str = str[::-1]
    else:
        if (i[1] == '1'):
            str = i[2]+str
        else:
            str = str + i[2]
print(str)

Submission status: Time Limit Exceeded

Solution code:
def Main(S, Q, queries):
    ans = S
    flipped = False
    T1 = 0
    for q in queries:
        if int(q[0]) == 1:
            if not flipped:
                flipped = True
            else:
                flipped = False
            continue
        else:
            if int(q[1]) == 1:
                if flipped:
                    ans += q[2]
                else:
                    a = q[2] + ans
                    ans = a
            else:
                if flipped:
                    a = q[2] + ans
                    ans = a
                else:
                    ans += q[2]
    if flipped:
        l = list(ans)
        l.reverse()
        ans = "".join(l)
    return ans
        

def main():
    S = str(input())
    Q = int(input())
    queries = []
    for i in range(Q):
        queries.append(list(map(str, input().split())))
    print(Main(S, Q, queries))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# @Time    : 2020/3/1 19:59
# @Author  : DIRICHLET
# @File    : main.py
a=[]
def myin(fun=int):
    global a
    a=list(map(fun,input().split()))
#myin(int)
s=input()
f=0
n=int(input())
for i in range(n):
    a=input().split()
    if len(a)==1:
        f=1-f
    else:
        if (f+int(a[1]))%2==1:
            s=a[2]+s
        else:
            s+=a[2]
if f==1:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
#def input():
#    return sys.stdin.readline()[:-1]
S = input()
Q = int(input())
for i in range(0, Q):
    s = input()
    if s[0] == "1":
        S = S[::-1]
    else:
        if s[2] == "1":
            S = s[-1] + S
        else:
            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 = ''.join([x[2] ,s])
    else:
        s = ''.join([s ,x[2]])
if flag == -1:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

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

print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    order = str(input())
    if len(order) == 1:
        s = s[::-1]

    elif order[2] == '1':
        s = order[4] + s
    else:
        s = s + order[4]

print(s)

Submission status: Time Limit Exceeded

Solution code:
def m():
    s = input()
    q = int(input())
    inv = 0
    pre = ''
    sub = ''

    for i in range(q):
        raw = input().split()
        if len(raw) == 1:
            inv = inv ^ 1
        else:
            f = int(raw[1])-1
            c = raw[2]
            if inv==f:
                pre = c + pre
            else:
                sub = sub + c
    
    if inv==0:
        print(pre+s+sub)
    else:
        print(sub[::-1]+s[::-1]+pre[::-1])

m()

Submission status: Time Limit Exceeded

Solution code:
import sys
a = sys.stdin.read().split('\n')[:-1]
s = a[0]
q = int(a[1])
f = 1
for i in range(2, q + 2):
    if a[i][0] == '1':
        f = 1 - f
    else:
        if a[i][2] == '21'[f]:
            s = a[i][4] + s
        else:
            s += a[i][4]
if f == 1:
    print(s)
else:
    b = ''
    for c in reversed(s):
        b += c
    print(b)

Submission status: Time Limit Exceeded

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

last = 1

for i in range(n):
    if l[i][0] == '1':
        last = last * -1
    else:
        if l[i][1] == '1' and last > 0:
            s = l[i][2] + s
            sc += l[i][2]
        elif l[i][1] == '2' and last > 0:
            s += l[i][2]
            sc = l[i][2] + sc
        elif l[i][1] == '1' and last < 0:
            s += l[i][2]
            sc = l[i][2] + sc
        else:
            s = l[i][2] + s
            sc += l[i][2]

if last > 0:
    print(s)
else:
    print(sc)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
flag = True
for i in range(Q):
    q = list(input().split())
    # print(i,flag)
    if q[0] == "1":
        flag = not flag
    else:
        if flag:
            if q[1] == "1":
                S.insert(0,q[2])
            else:
                S.append(q[2])
        else:
            if q[1] == "1":
                S.append(q[2])
            else:
                S.insert(0,q[2])
if not flag:
    S.reverse()
for s in S:
    print(s,end='')

Submission status: Time Limit Exceeded

Solution code:
S = list(str(input()))
Q = int(input())
r = 1
for q in range(Q):
  Query = str(input())
  if list(Query)[0] == '1':
    r *= -1
  else:
    if (list(Query)[2] == '1' and r == 1) or (list(Query)[2] == '2' and r == -1):
      S = list(Query)[4:] + S
    else:
      S = S + list(Query)[4:]
print(''.join(S[::r]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = deque(input())

Q = int(input())

for i in range(Q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        S.reverse()
    elif int(q[0]) == 2:
        if int(q[1]) == 1:
            S.appendleft(q[2])
        elif int(q[1]) == 2:
            S.append(q[2])

print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Qu = []
i = 1
while True:
    val = input()
    i += 1
    Qu.append(val)
    if i == Q+1:
        break
    
for i in range(Q):
    if len(Qu[i]) == 1:
        S = S[::-1]
    else:
        a1,a2,a3= Qu[i].split()
        a2 = int(a2)
        if a2 ==1 :
            S = a3 + S
        if a2 ==2:
            S = S + a3
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverseFlag = False
for i in range(Q):
    tmp = input()
    if tmp[0] == '1':
        reverseFlag = not reverseFlag
    else:
        tmpt, tmpf, tmpc = tmp.split(' ')
        if tmpf == '1' and reverseFlag == False or tmpf == '2' and reverseFlag == True:
            S = ''.join([tmpc, S]) 
        else:
            S = ''.join([S, tmpc])

if reverseFlag == True:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
q = [input().split() for l in range(n)]
for i in range(n):
  if q[i][0] == '1':
    s = s[::-1]
  elif q[i][0] == '2':
    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=str(input())
q=int(input())

for i in range(q):
    l=list(map(str, input().split()))
    if int(l[0]) ==1:
        s = s[::-1]
    else:
        if int(l[1]) ==1:
            s= l[2]+s
        else:
            s= s+l[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())

str_list_ = [input().split(' ') for q in range(Q)]

for str_list in str_list_:
    if str_list[0]=='1':
        S=S[::-1]
    elif str_list[0]=='2':
        if str_list[1]=='1':
            S='{}{}'.format(str_list[2], S)
        elif str_list[1]=='2':
            S='{}{}'.format(S, str_list[2])
print(S)

Submission status: Time Limit Exceeded

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


for _ in range(Q):
  s = list(sys.stdin.readline())
  if s[0]=="1":
    S=S[::-1]
  else:
    if s[2]=="1":
      S="".join([s[4],S])
    else:
      S="".join([S,s[4]])
      
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

d=deque(s)
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:
s = str(input())
n = int(input())

right = ""
left = ""

flag = 0
for i in range(n):
    q = list(map(str,input().split()))
    if len(q) == 1:
        flag = 1 - flag 
    elif q[1] == "1":
        if flag == 0:
            left = q[2] + left
        else:
            right = right + q[2]
    else:
        if flag == 0:
            right = right + q[2]
        else:
            left = q[2] + left


if flag == 0:
    print(left + s + right)
elif flag == 1:
    print(right[::-1] + s[::-1] + left[::-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=str(input())

q=int(input())
for i in range(q):
    a=input().strip()
    a=list(a)
    #print(a)
    if int(a[0])==1:
        #print("DFGHJ")
        s=s[::-1]
    if int(a[0])==2:
        #print(a[2])
        s=list(s)
        if int(a[2])==1:
            s=deque(s)
            #print(s)
            s.appendleft(str(a[4]))
            #print(s)
            s=list(s)
            #print("DFGH",s)
        else:
            s.append(str(a[4]))
        s=''.join(s)
        #print(s)        
print(s)

Submission status: Time Limit Exceeded

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

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for query in queries:
  t = query[0]
  if (t == '1'):
    new_S = S[::-1]
    S = new_S
  elif (t == '2'):
    f = query[1]
    if(f == '1'):
      new_S = query[2] + S
      S = new_S
    elif(f == '2'):
      new_S = S + query[2]
      S = new_S

print(S)

Submission status: Time Limit Exceeded

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

Query = []
for i in range(Q):
  Query.append(list(map(str, input())))

for i in range(Q):
  if len(Query[i]) == 5:
    Query[i].pop(1)
    Query[i].pop(2)
    Query[i][0], Query[i][1] = int(Query[i][0]), int(Query[i][1])
  else:
    Query[i][0] = int(Query[i][0])
    if i != 0:
      if Query[i-1][0] == 1 and Query[i][0] == 1:
        Query[i-1][0], Query[i][0] = 0, 0
    
for i in range(Q):
  if Query[i][0] == 0:
    pass
  elif Query[i][0] == 1:
    S = S[::-1]
  else: #Query[i][0] == 2
    if Query[i][1] == 1:
      S = Query[i][2] + S
    else: #Query[i][1] == 2
      S = S + Query[i][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':
            if r_flag == False:
                S.insert(0, cmd[2])
            else:
                S.append(cmd[2])
        else:
            if r_flag == False:
                S.append(cmd[2])
            else:
                S.insert(0, cmd[2])
if r_flag == False:
    print ''.join(S)
else:
    S.reverse()
    print ''.join(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
  _list = input().split(" ")
  if int(_list[0]) == 1:
    S = S[::-1]
  else:
    if int(_list[1]) == 1:
      S = ''.join([_list[2], S])
    else:
      S = ''.join([S, _list[2]])
print(S)

Submission status: Time Limit Exceeded

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

result = list(S)
reverse_flag = False

for i in range(Q):
    # print(i + 1)
    q = query_list[i]
    # print(q)
    t = int(q[0])
    if t == 1:
        reverse_flag = not reverse_flag
    else:
        t, f, c = q.split()
        f = int(f)
        c = list(c)
        if f == 1:
            if reverse_flag:
                result = result + c
            else:
                result = c + result
        else:
            if reverse_flag:
                result = c + result
            else:
                result = result + c
    # print(t, f, c, result)
    # print("--------")
if reverse_flag:
    result = result[::-1]
#result = str(result)
result = "".join(result)
print(result)

Submission status: Time Limit Exceeded

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

div = True
for q in qs:
    if q[0] == '1':
        div = not div
    elif (q[1] == '1' and div) or (q[1] == '2' and not div):
        s = q[2] + s
    else:
        s = s + q[2]


if div:
    print(s)
else:
    s = ''.join(list(reversed(s)))
    print(s)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
g = 0
for i in range(q):
  query = input()
  if (query == "1"):
    g = 1-g
  else:
    queries = query.split()
    f = int(queries[1])-1
    c = queries[2]
    if (g == f):
      s = c + s
    else:
      s = s + c
if (g == 1):
  s = "".join(list(s)[::-1])
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys

ans = str(input())
n = int(input())

for i in range(n) :
    x = input().split(" ")
    if int(x[0]) == 1 :
        ans = ans[::-1]
    else :
        if int(x[1]) == 1 :
            ans = str(x[2]) + ans
        else :
            ans = ans + str(x[2])

print(ans)

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

    return s


print(main(S, Q, QUERY))

Submission status: Time Limit Exceeded

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

for _ in range(q):
  query = list(input().split())
  if len(query) == 1:
    #反転
    ans = ans[::-1]
  else:
    if int(query[1]) == 1:
      # 先頭
      ans = query[2] + ans
    else:
      # 後方
      ans = ans + query[2]

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:

# -*- coding: utf-8 -*-
S = input()
Q = int(input())

rev = False

for q in range(Q):
    i = input()
    if i[0] == "1":
        rev = not rev
    elif not rev and i[2] == "1" or rev and i[2] == "2":
        S = i[4] + S
    else:
        S = S + i[4]

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = []

for _ in range(Q):
    Query += [input()]

for i, q in enumerate(Query):
    if q == "1":
        S = S[::-1]
    else:
        Ti, Fi, Ci = list(q.split())
        if Fi == "1":
            S = Ci + S
        elif Fi == "2":
            S = S + Ci
    # print(f"i:{i} S:{S}")

print(S)