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()
# Python3 program to Split string into characters 
def split(word): 
    return [char for char in word]  
def convert(s): 
  
    # initialization of string to "" 
    new = "" 
  
    # traverse in the string  
    for x in s: 
        new += x  
  
    # return string  
    return new 
      
      
# Driver code 
word = S
splits=split(S)
Q=int(input())
flipcounter=0
for i in range(0,Q):
  inputforround=input()
  if inputforround=='1':
    flipcounter=flipcounter+1
  else:
    T,F,C=map(str,inputforround.split())
    if (int(F)+flipcounter)%2==1:
      splits.insert(0,C)
    else:
      splits.append(C)
if flipcounter%2==1:
  splits.reverse()
ans=convert(splits)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
S=input().splitlines()[0]
Q = int(input())
flipcnt=0
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        flipcnt += 1
        continue
    else:
        if (int(q[1]) + flipcnt) % 2 == 1:
            #前
            S=q[2]+S
        else:
            S=S+q[2]
    #print(S)

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

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
a = str(input())
q = int(input())
s = [input().split() for i in range(q)]
x = 0
for i in range(q):
    if int(s[i][0]) == 1:
        x+=1
    elif int(s[i][0])==2 and (x+int(s[i][1]))%2==0:
        a = a+s[i][2]
    elif int(s[i][0])==2 and (x+int(s[i][1]))%2==1:
        a = s[i][2]+a
if x%2==1:
    a=a[::-1]
print(a)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
a = [i for i in input()]
q = int(input())
s = [input().split() for i in range(q)]
for i in range(q):
    A = []
    if int(s[i][0]) == 1 and len(a)>1:
        for j in range(len(a)):
            A.append(a[len(a)-j-1])
        a = A
    elif int(s[i][0]) == 1 and len(a)==1:
        continue
    elif int(s[i][0])== 2 and int(s[i][1])==1:
        a.insert(0,s[i][2])
    else:
        a.append(s[i][2])
print(''.join(a))

Submission status: Time Limit Exceeded

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

ans = collections.deque(S)

for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        ans.reverse()
    else:
        if query[1] == '1':
            ans.appendleft(query[2])
        else:
            ans.append(query[2])
            

print("".join(ans))

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    q = input().split()
    if q[0]=='1':
        head = head*-1
    else:
        if q[1]=='1':
            h = head*1
        else:
            h = head*-1
        
        if h==1:
            S = q[2] + S
        else:
            S = S + q[2]
if head == -1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
# D

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

#q = []
#for _ in range(Q):
#  q += [input().split(" ")]

hc = 0

for i in range(Q):
  #qi = q[i]
  qi = input().split(" ")
  if len(qi)==1:
    #S = S[::-1]
    if hc==0:
      hc = 1
    else:
      hc = 0
  else:
    fi = int(qi[1])
    ci = qi[2]
    if hc ==0:
      if fi==1:
        S = ci + S
      else:
        S = S + ci
    else:
      if fi==1:
        S = S + ci
      else:
        S = ci + S

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

Submission status: Time Limit Exceeded

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

hanten = 0
t = 0
for i in quer:
    quary = i
    if quary[0]=='1':
        #S = S[::-1]
        hanten = hanten ^ 1
    else:
        t = (int(quary[1])-1) ^ hanten
        if t:
            S = S + quary[2]
        else:
            S = quary[2] + S
    #print(S)
if hanten:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S= input()
q=int(input())
h=0
for i in range(q):
    a=input()


    if a[0]=="1":
        h+=1
    else:
        if h%2==0:
            if a[2] == "1":
                S=a[4]+S
            else:
                S=S+a[4]
        else:
            if a[2] == "2":
                S=a[4]+S
            else:
                S=S+a[4]

if h%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):
    a=input()


    if a[0]=="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:
S = input()
Q = int(input())
Query = []
for i in range(Q):
    Query.append(list(input().split()))
for i in Query:
    if len(i) == 1:
        S = S[::-1]
    else:
        if int(i[1]) == 1:
            S = i[2] + S
        else:
            S = S + i[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
han = False
for i in range(q):
    x = list(input())
    if len(x) == 1:
        if han:
            han = False
        else:
            han = True
        #s.reverse()
    else:
        if han:
            if x[2] == "1":
                s.append(x[4])
            else:
                s.insert(0, x[4])
        else:
            if x[2] == "1":
                s.insert(0, x[4])
            else:
                s.append(x[4])
if han:
    s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
rev = False
for i in range(Q):
  l = input().split()
  if l[0] == '1':
    rev = not rev
  else:
    if rev:
      left = l[2]
      right = S
    else:
      left = S
      right = l[2]
    if l[1] == '1':
      S = right + left
    else:
      S = left + right
if rev:
  print(S[::-1])
else:
  print(S)

Submission status: Time Limit Exceeded

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

def main():
    S = input()[:-1]
    Q = int(input())
    li = ['' for _ in range(2)]
    now = 0
    for _ in range(Q):
        x = input()
        if(x[0] == '2'):
            if(x[2]=='1'):
                li[1-now] += x[4]
            else:
                li[now] += x[4]
        else:
            now = 1-now

    print(li[1-now][::-1]+S[::-(now*2-1)]+li[now])
    

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def main():
    S = input()[:-1]
    Q = int(input())
    now = 1
    for _ in range(Q):
        x = input()
        if(x[0] == '2'):
            if(x[2]=='1'):
                if(now == 1):
                    S = x[4] + S
                else:
                    S += x[4]
            else:
                if(now == 1):
                    S += x[4]
                else:
                    S = x[4] + S
        else:
            now *= -1

    print(S[::now])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

print(S[::now])

Submission status: Time Limit Exceeded

Solution code:
s = input()

n = int(input())

#y = []
#for i in range(n):
#    x = input()
#    y.append(x)
#
#print(y)
#print(y[0].split())
for i in range(n):
    x = input()
    if int(x[0]) == 1:
        s = s[::-1]
    else:
        x = x.split()
        if int(x[1]) == 1:
            s = x[2] + s

        else:
            s = s + x[2]


print(s)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
cnt = 0
for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        cnt += int(a[0])
    elif (cnt + int(a[1])) % 2 == 1:
        S = a[2] + S
    elif (cnt + int(a[1])) % 2 == 0:
        S = S + a[2]

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

Submission status: Time Limit Exceeded

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

query_list = [input().split() for _ in range(q)]

for query in query_list:
    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 = str(input())
Q = int(input())
cnt = 0
for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        cnt += int(a[0])
    elif (cnt + int(a[1])) % 2 == 1:
        S = a[2] + S
    elif (cnt + int(a[1])) % 2 == 0:
        S = S + a[2]

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

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
a_cnt = 2
for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        a_cnt += int(a[0])
    elif a[0] == '2' and a_cnt % 2 == 0:
        if a[1] == '1':
            S = a[2] + S
        else:
            S = S + a[2]
    elif a[0] == '2' and a_cnt % 2 == 1:
        if a[1] == '1':
            S = S + a[2]
        else:
            S = a[2] + S

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

Submission status: Time Limit Exceeded

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

# deq=deque()
# for s in S:
#     deq.append(s)

rev=False
front=""
last=""
# ans+=S

for _ in range(Q):
    query=input().split()
    if query[0]=="1":
        rev = not rev
        # print(rev)
    else:
        if rev:
            query[1]=str(3-int(query[1]))
        if query[1]=="1":
            # deq.appendleft(query[2])
            front=query[2]+front
        else:
            # deq.append(query[2])
            last+=query[2]

# if rev:
#     deq.reverse()
# # ans=""
# for i in range(len(deq)):
#     ans+=deq[i]

if not rev:
    print(front+S+last)
else:
    print(last[::-1]+S[::-1]+front[::-1])

Submission status: Time Limit Exceeded

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

# deq=deque()
# for s in S:
#     deq.append(s)

rev=False
ans=""
ans+=S
for _ in range(Q):
    query=input().split()
    if query[0]=="1":
        rev = not rev
        # print(rev)
    else:
        if rev:
            query[1]=str(3-int(query[1]))
        if query[1]=="1":
            # deq.appendleft(query[2])
            ans=query[2]+ans
        else:
            # deq.append(query[2])
            ans=ans+query[2]

# if rev:
#     deq.reverse()
# # ans=""
# for i in range(len(deq)):
#     ans+=deq[i]

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

Submission status: Time Limit Exceeded

Solution code:

from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from functools import reduce

from collections import deque, Counter, defaultdict
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import statistics
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm(a, b):
  return a*b//gcd(a, b)

def main():
  S = input()
  Q = int(input())
  TFC = list(list(input().split()) for _ in range(Q))
  rev = 0
  for i in range(Q):
    if TFC[i][0] == '1':
      rev ^= 1
    else:
      if TFC[i][1] == '1':
        if rev == 0:
          S = TFC[i][2] + S
        else:
          S = S + TFC[i][2]
      else:
        if rev == 0:
          S = S + TFC[i][2]
        else:
          S = TFC[i][2] + S
  print(S if rev == 0 else S[::-1])
  
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:

from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from functools import reduce

from collections import deque, Counter, defaultdict
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import statistics
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm(a, b):
  return a*b//gcd(a, b)

def main():
  S = input()
  Q = int(input())
  TFC = list(list(input().split()) for _ in range(Q))
  #q = deque(S)
  for i in range(Q):
    if TFC[i][0] == '1':
      S = S[::-1]
    else:
      if TFC[i][1] == '1':
        S = TFC[i][2] + S
      else:
        S = S + TFC[i][2]
  print(S)
  
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
Q=int(input())

deq=deque()
for s in S:
    deq.append(s)
rev=False

for _ in range(Q):
    query=input().split()
    if query[0]=="1":
        rev = not rev
    else:
        if rev:
            query[1]=str(3-int(query[1]))
        if query[1]=="1":
            deq.appendleft(query[2])
        else:
            deq.append(query[2])

if rev:
    deq.reverse()
ans=""
for i in range(len(deq)):
    ans+=deq[i]
print(ans)

Submission status: Time Limit Exceeded

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

Query=[]
for i in range(Q):
    Query.append(list(input().split()))
#print(Query)

check=1
for j in range(Q):
    if len(Query[j])==1:
        check*=-1      
        #print(S)
    else:
        if Query[j][1]=='1':
            if check==1:
                S=Query[j][2]+S
            else:
                S+=Query[j][2]
            #print(S)
        else:
            if check==1:
                S+=Query[j][2]
            else:
                S=Query[j][2]+S
            #print(S)

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
Q=int(input())

ans=deque()
for s in S:
    ans.append(s)
rev=False

for _ in range(Q):
    query=input().split()
    if query[0]=="1":
        rev = not rev
    else:
        if rev:
            query[1]=str(3-int(query[1]))
        if query[1]=="1":
            ans.appendleft(query[2])
        else:
            ans.append(query[2])
    # print(ans)
if not rev:
    for i in range(len(ans)):
        print(ans[i],end="")
else:
    for i in range(len(ans)-1,-1,-1):
        print(ans[i],end="")

Submission status: Time Limit Exceeded

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

Query=[]
for i in range(Q):
    Query.append(list(input().split()))
#print(Query)

for j in range(Q):
    if len(Query[j])==1:
        S=S[::-1]
        #print(S)
    else:
        if Query[j][1]=='1':
            S=Query[j][2]+S
            #print(S)
        else:
            S+=Query[j][2]
            #print(S)

print(S)

Submission status: Time Limit Exceeded

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

reverse = False
for i in query:
    if i[0] == '1':
        if reverse:
            reverse = False
        else:
            reverse = True
        continue
    else:
        if reverse:
            if i[1] == '1':
                s.append(i[2])
            else:
                s.insert(0,i[2])
        else:
            if i[1] == '1':
                s.insert(0,i[2])
            else:
                s.append(i[2])

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

Submission status: Time Limit Exceeded

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

reverse = False
for i in query:
    if len(i) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        if reverse:
            if i[1] == '1':
                s = s + i[2]
            else:
                s = i[2] + s
        else:
            if i[1] == '1':
                s = i[2] + s
            else:
                s = s + i[2]
if reverse:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

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

for i in query:
    if len(i) == 1:
        s = s[::-1]
    else:
        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())
query = [input().split() for _ in range(q)]

for i in query:
    if len(i) == 1:
        s = s[::-1]
    else:
        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())
q_list = []
for i in range(Q):
    q_list.append(input().split(" "))
    if q_list[i][0] == "1":
        S = S[::-1]
    elif q_list[i][1] == "1":
        S = q_list[i][2] + S
    else :
        S = S + q_list[i][2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
a = list(input())
Q = int(input())
A = []
d = deque(a)
for i in range(Q):
  b = list(input().split())
  if len(b) == 1:
    d.reverse()
  elif b[1] == "1":
    d.appendleft(b[2])
  elif b[1] == "2":
    d.append(b[2])
print("".join(d))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
a = list(input())
Q = int(input())
B = []
C = []
A = [list(input().split()) for _ in range(Q)]
d = deque(a)
for i in range(len(A)):
  if len(A[i]) == 1:
    d.reverse()
  elif A[i][1] == "1":
    d.appendleft(A[i][2])
  elif A[i][1] == "2":
    d.append(A[i][2])
print(''.join(d))

Submission status: Time Limit Exceeded

Solution code:
a = list(input())
Q = int(input())
B = []
C = []
A = [list(input().split()) for _ in range(Q)]
for i in range(len(A)):
  if len(A[i]) == 1:
    a.reverse()
  elif A[i][1] == "1":
    a.insert(0,A[i][2])
  elif A[i][1] == "2":
    a.append(A[i][2])
print(''.join(a))

Submission status: Time Limit Exceeded

Solution code:
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

from collections import deque 
s=s0()
n=n0()
q=s2(n)

d=deque("###")

f=False
for a in q:
    if a[0]=="1":
        f=not(f)
        d.reverse()
    else:
        if a[2]=="1":
            d.appendleft(a[4])
        else:
            d.append(a[4])
a,b=(''.join(list(d))).split("###")
print(a+s[::-1]+b if f else a+s+b)

Submission status: Time Limit Exceeded

Solution code:
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

from collections import deque 
s=deque(s0())
n=n0()
q=s2(n)

for a in q:
    if a[0]=="1":
        s.reverse()
    else:
        b=a.split()
        if b[1]=="1":
            s.appendleft(b[2])
        else:
            s.append(b[2])
print(''.join(list(s)))

Submission status: Time Limit Exceeded

Solution code:
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

s=s0()
n=n0()
q=s2(n)

for a in q:
    if a[0]=="1":
        s=s[::-1]
    else:
        b=a.split()
        if b[1]=="1":
            s="{}{}".format(b[2],s)
        else:
            s="{}{}".format(s,b[2])
print(s)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(q):
    q_i = input()
    if len(q_i) == 1:
        count += 1
    else:
        lis = [i for i in q_i.split()]
        x = count
        x += int(lis[1])
        if x % 2 == 0:
            s += lis[2]
        else:
            s = lis[2] + s

if count % 2 == 1:
    ans = ""
    for i in range(len(s)):
        ans += s[-i-1]
else:
    ans = s
print(ans)

Submission status: Time Limit Exceeded

Solution code:

S = str(input())
Q = int(input())
query=[]

# for line in range(Q):
# 	query.append(list(input().split()))

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=S+q[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:

S = str(input())
Q = int(input())
query=[]

for line in range(Q):
	query.append(list(input().split()))

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

Submission status: Time Limit Exceeded

Solution code:

S = str(input())
Q = int(input())
query=[]

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Q_list = [list(input().split()) for n in range(Q)]
d = 1

for _list in Q_list:
    if int(_list[0]) == 1:
        d *= - 1
    else:
        if int(_list[1]) == 1:
            if d == 1:
                S = _list[2] + S
            else:
                S = S + _list[2]
        else:
            if d == 1:
                S = S + _list[2]
            else:
                S = _list[2] + S
if d == - 1:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Q_list = [list(input().split()) for n in range(Q)]
front = [1, 0]

for _list in Q_list:
    if int(_list[0]) == 1:
        first = front[0]
        front[0] = front[1]
        front[1] = first
    else:
        if int(_list[1]) == 1:
            if front[0] == 1:
                S = _list[2] + S
            else:
                S = S + _list[2]
        else:
            if front[0] == 1:
                S = S + _list[2]
            else:
                S = _list[2] + S
if front[0] == 0:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
t=True
a=str()
b=str()
for i in range(n):
    q=input().split()
    if(len(q)==1):
        t=not t
        a=a[::-1]
        b=b[::-1]
        continue
    if(int(q[1])==1):
        if(t):a=q[2]+a
        else:b=q[2]+b
    if(int(q[1])==2):
        if(t):b=b+q[2]
        else:a=a+q[2]
if(t):print(a+s+b)
else:print(b+s[::-1]+a)

Submission status: Time Limit Exceeded

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

for _list in Q_list:
    if int(_list[0]) == 1:
        S = S[::-1]
    else:
        if int(_list[1]) == 1:
            S = _list[2] + S
        else:
            S = S + _list[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
for i in range(n):
    q=input().split()
    if(len(q)==1):
        s=s[::-1]
        continue
    if(int(q[1])==1):s=q[2]+s
    if(int(q[1])==2):s=s+q[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

S = input()
N = int(input())
top = 0 #0:Sの先頭が先頭、1:Sの末尾が先頭

skip = 0
for i in range(N):
    Q = list(map(str, input().split()))
    if Q[0] == "1":
        if top == 0:
            top = 1
        else:
            top = 0
    else:
        if top == 0:
            if Q[1] == "1":
                S = Q[2] + S
            else:
                S = S + Q[2]
        else:
            if Q[1] == "1":
                S = S + Q[2]
            else:
                S = Q[2] + S
if top == 1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

S = input()
N = int(input())

skip = 0
for i in range(N):
    Q = list(map(str, input().split()))
    if Q[0] == "1":
        S = S[::-1]
    else:
        if Q[1] == "1":
            S = Q[2] + S
        else:
            S = S + Q[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

S = input()
N = int(input())
Q = list(input().split() for _ in range(N))

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

S = input()
N = int(input())
Q = list(input().split() for _ in range(N))

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

S = input()
N = int(input())
Q = list(input().split() for _ in range(N))

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

print(S)

Submission status: Time Limit Exceeded

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

direction = 1

for i in range(Q):
    q = list(map(str, input().split())) 
    if q[0] == "1":
        direction *= (-1)
        #S = S[::-1]
    elif q[0] == "2":
        if direction > 0:
            if q[1] == "1":
                S = q[2] + S
            elif q[1] == "2":
                S = S + q[2]
        elif direction < 0:
            if q[1] == "1":
                S = S + q[2]
            elif q[1] == "2":
                S = q[2] + S 

if direction < 0:
    S = S[::-1]           

print(S)

Submission status: Time Limit Exceeded

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

direction = 1

for i in range(Q):
    q = input()
    if q[0] == "1":
        direction *= (-1)
        #S = S[::-1]
    elif q[0] == "2":
        if direction > 0:
            if q[2] == "1":
                S = q[4] + S
            elif q[2] == "2":
                S = S + q[4]
        elif direction < 0:
            if q[2] == "1":
                S = S + q[4]
            elif q[2] == "2":
                S = q[4] + S 

if direction < 0:
    S = S[::-1]           

print(S)

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

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

# define 0 as head top, 1 as tale top
result = [0, s]

for query in qs:
    if query[0] == "1":
        result[0] += 1
    else:
        f, c = query[1:]
        if f == "1":
            if result[0]%2 == 0:
                result[1] = c + result[1]
            else:
                result[1] += c
        else:
            if result[0]%2 == 1:
                result[1] = c + result[1]
            else:
                result[1] += c
                
if result[0] % 2:
    print(result[1][::-1])
else:
    print(result[1])