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())
for _ in range(Q):
    query = input().split()
    if len(query) == 1:
        S = S[::-1]
        # print('Ti=1: ' + S)
    else:
        F = int(query[1])
        C = query[2]
        if F == 1:
            S = C + S
        elif F == 2:
            S = S + C
        # print('Ti=2: ' + S)

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

rev_s = False
for i in range(q):
   tmp = list(input().split())
   if len(tmp) == 1:
      if rev_s == True:
         rev_s = False
      else:
         rev_s = True
   else:
      f, c = int(tmp[1]), tmp[2]
      if (rev_s == False and f == 1) or (rev_s == True and f == 2):
         s = c + s
      else:
         s = s + c

if rev_s == True:
   print(s[::-1])
else:
   print(s)

Submission status: Time Limit Exceeded

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

def main():
    s = list(input())
    q = int(input())
    list_ = []
    for _ in range(q):
        list_.append(list(map(str, input().split())))

    for i in range(q):
        if list_[i][0] == '1':
            s.reverse()
        elif list_[i][0] == '2':
            if list_[i][1] == '1':
                s[0:0] = list_[i][2]
            elif list_[i][1] == '2':
                s.append(list_[i][2])

    print(''.join(s))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#At coder 2020/3/8 C
import math
S=input()
Q=int(input())

jj=0
for ii in range(Q):
    aa=list(map(str,input().split())) 
    if '1'==aa[0] and jj==1:
        jj=0
    elif '1'==aa[0] and jj==0:
        jj=1
    else:
        if ('1'==aa[1] and jj==0) or ('2'==aa[1] and jj==1):
                S=aa[2]+S
        else:
                S=S+aa[2]
if jj==0:
    ans=S
else:
    ans=S[::-1]
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    tmp = []
    for i in range(q):
        tmp.append(input().split())

    for t in tmp:
        if len(t) == 1:
            s = s[::-1]
        else:
            f = int(t[1])
            c = t[2]
            if f == 1:
                s = c + s
            else:
                s = s + c

    print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
ans = S
flag = 0
for i in range(Q):
  q = list(map(str, input().split()))
  if(len(q)==1):
    if(flag==0):
      flag = 1
    else:
      flag = 0
  else:
    if(q[1]=="1"):
      if(flag==0):
        ans = ''.join([q[2], ans])
      else:
        ans = ''.join([ans, q[2]])
    else:
      if(flag==0):
        ans = ''.join([ans, q[2]])
      else:
        ans = ''.join([q[2], ans])
if( flag%2 == 0 ):
  print(ans)
else:
  print(ans[::-1])

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 = ''.join(list(reversed(s)))
  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 = input()
Q = int(input())
temS = S
for i in range(Q):
  inp = input()
  if int(inp[0]) == 1:
    temS = temS[::-1]
  else:
    _, F, C = inp.split()
    if int(F) == 1: temS = C + temS
    else: temS += C
print(temS)

Submission status: Time Limit Exceeded

Solution code:
s=input()
count_1=0
num =int(input())


for x in range(num):
    i=input().rstrip().split()
    if i[0]=="1":
        count_1+=1
        continue
        
    elif i[1]=="1" and count_1%2==0:
        s=i[2]+s
        continue
   
    elif i[1]=="1" and count_1%2==1:
        s=s+i[2]
        continue

    elif i[1]=="2" and count_1%2==0:
        s=s+i[2]
        continue

    elif i[1]=="2" and count_1%2==1:
        s=i[2]+s
        continue
if count_1%2==1:
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(str(input()))
num = int(input())
q = [""] * num
f = [""] * num
c = [""] * num

for i in range(num):
    q[i] = str(input().split())

for i in range(num):
    if q[i][2] == "1":
        s = s[::-1]
    #    print(s)
    elif q[i][7] == "1":
        s.insert(0,q[i][12])
    elif q[i][7] == "2":
        s.append(q[i][12])
ans = ""
for i in s:
    ans += i
print(ans)
#print(q[0][12])
#print(q[0])

Submission status: Time Limit Exceeded

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

for i in range(n):
    k=input()
    
    if k[0]=="1" :
        ans=ans[::-1]
    elif k[2]=="2":
        ans=ans+k[4]
    
    elif k[2]=="1":
        ans=k[4]+ans
   

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import chain

s = input()
q = int(input())
for i in range(1, q+1):
    a, b, c, *_ = chain(input().split(), [None] * 3)
    if int(a) == 1:
        s = s[::-1]
    else:
        if int(b) == 1:
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
queries=[input().split() for _ in range(q)]
rev=0
sentou=''
matubi=''

for query in queries:
  if len(query)==1:
    rev=(rev+1)%2
  else:
    f=int(query[1])-1
    c=query[2]
    if not rev^f:
      sentou=c+sentou
    else:
      matubi=matubi+c
s=sentou+s+matubi
if rev:
  s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

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

for i in range(a):
    c=input()
    if c[0]=='1':
        s=''.join(list(reversed(s)))
    else:
        if c[2]=='1':
            s=c[4]+s
        else:
            s=s+c[4]
            
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for b in Query:
    if b[0] == '1':
        # S = S[::-1]
        S = ''.join(list(reversed(S)))
    else:
        if b[1] == '1':
            S = b[2] + S
        else:
            S = S + b[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

s = np.array(list(input("")))
Q = int(input())
c = 0
for i in range(Q):
    t = input()
    if len(t) == 1:
        c += 1
    elif t[2] == "1":
        if c%2 == 0:
            s = np.insert(s,0,t[4])
        else:
            s = np.append(s,t[4])
    else:
        if c%2 == 0:
            s = np.append(s,t[4])
        else:
            s = np.insert(s,0,t[4])

if c%2 == 0:
    for i in range(len(s)):
        print(s[i],end="")
else:
    k = s[::-1]
    for i in range(len(s)):
        print(k[i],end="")

Submission status: Time Limit Exceeded

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

a = input()

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

if mode == -1:
    a = a[::-1]

print(a)

Submission status: Time Limit Exceeded

Solution code:

s = input()
q = int(input())
#query = []
#for i in range(q):
 #   query.append(input())

def rev(s):
    return "".join(list(reversed(s)))



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


print(s)

Submission status: Time Limit Exceeded

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

A = []
B = []
count = 0

for i in range(Q):
    k = input()
    if k == "1":
        count += 1
    else:
        if (int(k[2]) + count) % 2 == 1:
            A = A + [k[4]]
        else:
            B = B + [k[4]]

if count % 2 == 0:
    A.reverse()
    S = A + S + B
else:
    B.reverse()
    S.reverse()
    S = B + S + A

print(*S, sep="")

Submission status: Time Limit Exceeded

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    a, *ret = sys.stdin.readline().rstrip()
    ret.insert(0, a)
    Q = int(sys.stdin.readline().rstrip())

    for q in range(Q):
        line = sys.stdin.readline().split()
        if line[0] == '1':
            ret.reverse()
        elif line[0] == '2' and line[1] == '1':
            ret.insert(0, line[2])
        else:
            ret.append(line[2])

    print("".join(ret))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
a=input()
q=int(input())
for i in range(q):
    b=input().split()
    if b[0]=="1":
        a=''.join(list(reversed(a)))
    else:
        if b[1]=="1":
            a=b[2]+a
        else:
            a+=b[2]
print(a)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math

s = stdin.readline().rstrip()
n = int(stdin.readline().rstrip())

for i in range(n):
    q = stdin.readline().rstrip().split()
    if int(q[0]) == 1:
        s = s[::-1]
    if int(q[0]) == 2:
        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:
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 = deque(reversed(S))
    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 = list(input())
qnum = int(input())

a = [input() for i in range(qnum)]
for i in a:
    t = i.split()
    if t[0]=="1":
        s.reverse()
    else:
        if t[1] == "1":
            s.insert(0,t[2])
        else:
            s.append(t[2])
ans = ''.join(s)
print(ans)

Submission status: Time Limit Exceeded

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

s = list(s)
# s = s[::-1]
for _ in range(q_):
    q = input()
    if q[0] == '1':
        s = s[::-1]
    else:
        q = q.split(' ')
        if q[1] == '1':
            s.insert(0,q[-1])
        else:
            s.extend(q[-1])
print(''.join(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[2] == "1":
        S = q[4] + S
    else:
        S = S + q[4]
print(S)

Submission status: Time Limit Exceeded

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

for _ in range(q):
    query = input().split()
    t = query[0]
    if t == "1":
        rev = not rev
        continue
    f = query[1]
    c = query[2]
    if not rev and f == "2" or rev and f == "1":
        s.append(c)
    else:
        s.insert(0, c)
if rev:
    s = reversed(s)
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = deque(input())
q = int(input())
for _ in range(q):
    query = input().split()
    if query[0] == '1':
        s.reverse()
    else:
        if query[1] == '1':
            s.appendleft(query[2])
        else:
            s.append(query[2])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
  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()
N = int(input())
Q =[]
while True:
    try:
        Q.append(list(map(str,input().split())))
    except:
        break;
flag = True
for i in range(N):
    if Q[i][0]=="1":
        flag = not(flag)                
    else: 
        if (Q[i][1] == "2") == (flag == True):
            S += Q[i][2]#先頭
        else :
            S1 = Q[i][2]+ S
            S = S1

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def main():
  s = deque(list(input()))
  q = int(input())

  cnt = 0
  for _ in range(q):
    t = input()
    if t[0] == "1":
      cnt += 1
    else:
      if cnt%2 == 1:
        s.reverse()
        cnt = 0
      _,f,c = t.split(" ")
      if f == "1":
        s.appendleft(c)
      else:
        s.append(c)
  if cnt%2 == 1:
    s.reverse()
  print("".join(list(s)))

if __name__=="__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
def main():
	s = str(input())
	q = int(input())
	qu = [list(map(str,input().split())) for x in range(q)]
	shiji = 1
	rcount = 0
	for i in range(q):
		if qu[i][0] != '1':
			if shiji == 1:
				if qu[i][1] == '1':
					s = qu[i][2] + s
				else:
					s = s + qu[i][2]
			else:
				if qu[i][1] == '2':
					s = qu[i][2] + s
				else:
					s = s + qu[i][2]
		else:
			shiji *= -1
			rcount += 1
	if rcount % 2 == 0:
		print(s)
	else:
		s = list(s)
		s.reverse()
		print(''.join(map(str,s)))

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

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

flag = 0
for i in range(q):
    x = list(input().split())
    if int(x[0]) == 1:
        if flag ==0:
            flag = -1
        else:
            flag = 0
        continue
    if int(x[1]) +flag == 1:
        s.insert(0, x[2])
    else:
        s.append(x[2])
ans = ''.join(s)
if flag == -1:
    print(ans[::-1])
else:
    print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
#queries = []
for _ in range(0, q):
    query = input()
    if len(query) == 1:
 #       queries.append([int(query)])
        s = s[::-1]
    else:
        query = query.split(' ')
        query = [int(query[0]), int(query[1]), query[2]]
#        queries.append(query)
        if query[1] == 1:
            s = query[2] + s
        else:
            s = s + query[2]



#for i in range(0, q):
#    if queries[i][0] == 1:
#        s = s[::-1]
#    else:
#        if queries[i][1] == 1:
#            s = queries[i][2] + s
#        else:
#            s = s + queries[i][2]

print(s)

Submission status: Time Limit Exceeded

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

S = input().rstrip()
Q = int(input())

ans = S
for _ in range(Q):
    query = list(input().split())
    if query[0] == '1':
        ans = ans[::-1]
        continue
    elif query[0] == '2':
        f = query[1]
        c = query[2].rstrip()
        if f == '1':
            ans = c + ans
        else:
            ans = ans + c
print(ans)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

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

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(Q):
    Q = [i for i in input().split()]
    if Q[0] == "1":
        S = S[::-1]
    else:
        if Q[1] == "1":
            S.insert(0,Q[2])
        elif Q[1] == "2":
            S.append(Q[2])

print("".join(S))

Submission status: Time Limit Exceeded

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


pos = 0

for q in range(Q):
  myinput = list(input().split())
  #print(myinput)
  if len(myinput) == 1:
      pos += 1
  else:
      if myinput[1] == '1':
          if pos% 2 == 0:
            #正方向、先頭
            S = myinput[2] + S    
          elif pos%2 !=0:
            #逆方向、先頭
            S += myinput[2]   
      elif myinput[1] == '2':
          if pos% 2 == 0:
              #正方向、末尾
            S += myinput[2]    
          elif pos%2 !=0:
               #逆方向、末尾
                 S = myinput[2] + S    

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

Submission status: Time Limit Exceeded

Solution code:
S=list(str((input())))
Q=int(input())
for i in range(Q):
    T=list(map(str,input().split()))
    if T[0]=="1":
        S.reverse()
    if T[0]=="2":
        if T[1]=="1":
            S.insert(0,T[2])
        if T[1]=="2":
            S.append(T[2])

print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
S = list(input())
N = int(input())
t = [0] * N
f = [0] * N
c = [0] * N
for i in range(N):
    li = list(input().split())
    if len(li) == 1:
        t[i] = li[0]
        f[i] = 0
        c[i] = 0
    if len(li) == 2:
        t[i], f[i] = li[0]
        c[i] = 0
    if len(li) == 3:
        t[i], f[i], c[i] = li
        

for j in range(N):
    if t[j] == '1':
        S = S[::-1]
    else:
        if f[j] == '1':
            S[0:0] = c[j]
        elif f[j] == '2':
            S.extend(c[j])
print(''.join(S))

Submission status: Time Limit Exceeded

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

S=list(input()[:-1])
Q=int(input()[:-1])
f=0

for _ in range(Q):
    q=input().split()
    if int(q[0])==1:
        f = (f+1)%2
    if int(q[0])==2:
        if int(q[1])==1:
            if f==0:
                SS=[q[2]]
                SS.extend(S)
                S=SS
            else:
                S.append(q[2])
        else:
            if f==0:
                S.append(q[2])
            else:
                SS=[q[2]]
                SS.extend(S)
                S=SS
if f==0:
    print("".join(S))
else:
    print("".join(S[::-1]))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    query = input().split()
    if len(query) == 1:
        S = S[::-1]
        # print('Ti=1: ' + S)
    else:
        F = int(query[1])
        C = query[2]
        if F == 1:
            S = ''.join([C, S])
        elif F == 2:
            S = ''.join([S, C])
        # print('Ti=2: ' + S)

print(S)

Submission status: Time Limit Exceeded

Solution code:

s = input()
n = int(input())

flag = 0
for i in range(n):
    Q = input()
    if int(Q[0]) == 2:
        F = int(Q[2])
        C = Q[4]
        #print(F, C)
        if (F + flag) % 2 == 1:
            s = C + s
        else:
            s = s + C
    else:
        flag = (flag + 1) % 2 

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

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7
    S = input()
    Q = II()
    r = False
    for _ in range(Q):
        query = input().split()
        if query[0] == '1':
            r ^= True
        else:
            if query[1] == '1':
                    if r:
                        S = S + query[2][::-1]
                    else:
                        S = query[2] + S
            else:
                if r:
                    S = query[2][::-1] + S
                else:
                    S = S + query[2]
    print(S[::-1] if r else S)
                



    

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
R = 1
for _ in range(Q):
  inp = input()
  if inp[0]=='1':
    R *= -1
  else:
    o, f, c = inp.split()
    if f=='1' and R==-1 or f=='2' and R==1:
      S[len(S):] = (c)
    else:
      S = [c]+S
S = S if R==1 else reversed(S)
S = ''.join(S)
print(S)

Submission status: Time Limit Exceeded

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

def addflont(s,c):
    return c+s
def addback(s,c):
    return s+c

reverse = 0

for i in range(q):
    t = input()
    if t[0]=="1":
        reverse += 1
    if t[0]=="2":
        if t[2]=="1":
            if reverse%2==0:
                s = addflont(s,t[4])
            else:
                s = addback(s,t[4])
        elif t[2]=="2":
            if reverse%2==0:
                s = addback(s,t[4])
            else:
                s = addflont(s,t[4])

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    if len(x[i]) == 1:
        s = s[::-1]
    else:
        if x[i][1] == '1':
            s = x[i][2] + s
        elif x[i][1] == '2': 
            s = s + x[i][2]
print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    #t
    t=input().split()

    if t[0] == "1":
        s = s[::-1]
    elif t[0] == "2":
        #t[1] = f
        if t[1] == "1":
            s = t[2] + s
        elif t[1] == "2":
            s = s + t[2]

print(s)

Submission status: Time Limit Exceeded

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

    s = input()
    s = s[0:len(s)-1]
    Q = int(input())

    flg = 0 # 0:sonomama 1:gyakuten
    for _ in range(Q):
        q = input().split()
        if q[0] == "1":
            if flg == 0:
                flg = 1
            else:
                flg = 0
        elif q[0]=="2":
            tmp = q[2]
            if (q[1] == "1" and flg ==0) or (q[1] == "2" and flg ==1):
                tmp += s
                s = tmp
            elif (q[1] == "2" and flg == 0) or (q[1] == "1" and flg == 1):
                s += tmp

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = list(input())
q = int(input())

for i in range(q):
    buf = input().split()
    if(len(buf) == 1):
        s.reverse()
    else:
        if buf[1] == '1':
            s = deque(s)
            s.appendleft(buf[2])
            s = list(s)
        else:
            s.append(buf[2])

print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
count = 0
for i in range(Q):
    Query = list(input().split())
    if Query[0] == '1':
        count += 1 #countが偶数:正 奇数:逆
    else: # T = '2'
        if Query[1] == '1':
            if count % 2 == 0:
                S = Query[2] + S
            else:
                target = Query[2][::-1]
                S = S + target
        else: # F = '2'
            if count % 2 == 0:
                S += Query[2]
            else:
                target = Query[2][::-1]
                S = target + S
if count % 2 == 0:
    print(S)
else:
    print(S[::-1])

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]
    else:
        if q[1]=='1':
            #Fi=1のとき
            s=q[2]+s
        elif q[1]=='2':
            s=s+q[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    a, *ret = sys.stdin.readline().rstrip()
    ret.insert(0, a)
    Q = int(sys.stdin.readline().rstrip())

    for q in range(Q):
        line = sys.stdin.readline().split()
        if line[0] == '1':
            ret.reverse()
        elif line[0] == '2' and line[1] == '1':
            ret.insert(0, line[2])
        else:
            ret.append(line[2])

    print("".join(ret))



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(q):
#    a = list(map(str,input().split()))
    a = str(input())
    if a[0] == "2":
        if a[2] == "1":
            s = str(a[4]) + s
        else:
            s = s + str(a[4])
    else:
        s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = deque(list(input()))
q = int(input())
query = [input() for i in range(q)]

for i in range(q):
  temp = query[i].split()
  if int(temp[0]) == 1:
    s.reverse()
  else:
    if int(temp[1]) == 1:
      s.appendleft(temp[2])
    else:
      s.append(temp[2])

print(''.join(s))

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 == 0:
  for i in S:
    print(i, end="")
else:
  for i in range(len(S)-1, -1, -1):
    print(S[i], end="")
print("")

Submission status: Time Limit Exceeded

Solution code:
S = input()
Slist = [S[i] for i in range(len(S))]
Q = int(input())
Query = [list(input().split()) for i in range(Q)]
for idx, value in enumerate(Query):
    if int(value[0]) == 1:
        Slist= Slist[::-1]
    else:
        if int(value[1]) == 1:
            Slist = [value[2]] + Slist
        else:
            Slist.append(value[2])
print("".join(Slist))

Submission status: Time Limit Exceeded

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

for _ in range(Q):
  q = list(input())
  if q[0] == "1":
    S = S[::-1]
  else:
    if q[2] == "1": S.insert(0, q[4])
    else: S.append(q[4])

print("".join(S))

Submission status: Time Limit Exceeded

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

for i in range(Q):
  Query = input()
  if len(Query) == 1:
    S = S[::-1]
  else:
    arr = Query.split()
    if int(arr[1]) == 1:
      S = arr[2] + S
    else:
      S = S + arr[2]

print(S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

forb = 1

for i in range(q):
    order = str(input())
    if len(order) == 1:
        forb = forb * (-1)

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

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