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:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	S = input()
	S = S[:-1]
	Q = inputInt()

	save = []

	flg = True
	for _ in range(Q):
		inp = input()
		inp = inp[:-1]
		inpt = inp.split()

		if len(inpt) == 1:
			if flg == True:
				flg = False
			else:
				flg = True
		else:
			oder = inpt[1]
			if oder == "1":
				if flg == True:
					S = inpt[2] + S
				else:
					S = S + inpt[2]
			else:
				if flg == True:
					S = S + inpt[2]
				else:
					S = inpt[2] + S

		if len(S) > 100000:
			save.append(S[1:-1])
			#print(save)
			S = S[0] + S[-1]
			#print(S)

	if flg == True:
		if len(save) > 0:
			tmp = "".join(save)
			ind = len(S) // 2
			print(S[:ind] + tmp + S[ind:])
		else:
			print(S)
	else:
		if len(save) > 0:
			S = S[::-1]
			tmp = "".join(save)
			tmp = tmp[::-1]
			ind = len(S) // 2
			print(S[:ind] + tmp + S[ind:])
		else:
			print(S[::-1])


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
rev=0
for i in range(q):
  query=input()
  if len(query)==1:
    rev+=1
    rev%=2
  else:
    t,f,c=map(str,query.split())
    f=int(f)%2
    if f!=rev:
      s=c+s
    else:
      s=s+c
if rev==1:
  s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
flg = False
for i in range(q):
    query = input()
    if query[0] == '1': flg = not flg
    else:
        t,f,c = query.split()
        if flg:
            s = c+s if f=='2' else s+c
        else:
            s = c+s if f=='1' else s+c
if flg: s = ''.join(list(reversed(s)))
print(s)

Submission status: Time Limit Exceeded

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

ans = list(S)
for i in range(Q) :
    query = input()
    if query == "1" :
        ans.reverse()
    else :
        a, F, C = query.split()
        if F == "1" :
            ans.insert(0, C)
        else :
            ans.append(C)

print("".join(ans))

Submission status: Time Limit Exceeded

Solution code:

s=input().rstrip()
q=int(input().rstrip())
ans=s
rev=0
for _ in range(q):
    que,*C=input().split()
    que=int(que)
    if que==2:
        f,c=C
        f=int(f)
        if f==1:
            if rev%2==0:
                s=c+s
            else:
                s+=c
        else:
            if rev%2==0:
                s+=c
            else:
                s=c+s
    else:
        rev+=1
print(s if rev%2==0 else s[::-1])

Submission status: Time Limit Exceeded

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

reverse = False
count = 0
for i in range(q):
    qu = input()
    if qu[0] == "1":
        count += 1
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        _, f, c = qu.split()
        if reverse:
            if f == "1":
                s = s + [c]
            else:
                s = [c] + s
        else:
            if f == "1":
                s = [c] + s
            else:
                s = s + [c]

if count % 2:
    s = s[::-1]

print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


def main():
    S = input().rstrip()
    Q = int(input())
    TFC = [list(map(str,input().split())) for _ in range(Q)]

    inv = False

    for now in TFC:
        T = now[0]
        if T == '1':
            if inv == False:
                inv = True
            else:
                inv = False
        elif T == '2':
            F = now[1]
            C = now[2]
            if F == '1':
                if not inv:
                    S = C + S
                elif inv:
                    S = S + C
            elif F == '2':
                if not inv:
                    S = S + C
                elif inv:
                    S = C + S

    if inv:
        S = ''.join(list(reversed(S)))

    print(S)



main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

flag=0

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

if flag%2!=0:
	S=S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S, Q = input(), int(input())
rev = False
for _ in range(Q):
  a = input().split(' ')
  if a[0] == '1':
    rev = not rev
  else:
    if (rev and a[1] == '1') or (not rev and a[1] != '1'):
      S = S + a[2]
    else:
      S = a[2] + S
if rev:
  print(S[::-1])
else:
  print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = deque(list(str(input())))
q = int(input())
que = [list(map(str, input().split())) for _ in range(q)]

for query in que:
    if len(query) == 1:
        s.reverse()
    else:
        if int(query[1]) == 1:
            s.appendleft(query[2])
        else:
            s.append(query[2])

print(*s,sep='')

Submission status: Time Limit Exceeded

Solution code:
a = input()
#l = list(a)

q = int(input())

default_order = True
for i in range(q):
  x = input().split()
  if x[0] == "1":
    default_order = default_order ^ True
  elif x[0] == "2":
    insert_type = default_order ^ False
    if x[1] == "2":
      insert_type = not(insert_type)
    if insert_type:
      #l.insert(0,x[2])
      a = x[2] + a
    else:
      #l.append(x[2])
      a = a + x[2]
if not(default_order):
  l = list(a)
  l.reverse()
  a = ''.join(l)

#print(''.join(l))
print(a)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
for i in range(n):
  l=input().split()
  q=int(l[0])
 
  
  if q==1:
    ll=list(reversed(s))
    s="".join(ll)
  elif q==2:
    f=int(l[1])
    c=l[2]
    if f==1:
      s=c+s
    elif f==2:
      s=s+c
print(s)

Submission status: Time Limit Exceeded

Solution code:

def main(s,perse):
    query = input().split()
    query[0] = int(query[0])
    if query[0] == 1:
        return s,-perse
    else:
        query[1] = int(query[1])
        if query[1] == 1:
            if perse == 1:
                s.insert(0,query[2])
            else:
                s.append(query[2])
        if query[1] == 2:
            if perse == -1:
                s.insert(0,query[2])
            else:
                s.append(query[2])
        return s,perse
def conv(s):
    a = []
    for i in range(len(s)):
        a.append(s[i])
    return a

s = input()
s = conv(s)
q = int(input())
perse = 1
for i in range(q):
    s,perse = main(s,perse)
if perse == -1:
    s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
# from fractions import gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    S = INPUT()
    Q = INT()

    flag_order = True
    D_MAP = lambda: map(str, query.split())
    for i in range(Q):
        query = INPUT()
        if len(query.split()) == 1:
            T = 1
        else:
            T = 2
            _, F, C = D_MAP()
            F = int(F)

        if T == 1:
            if flag_order: flag_order = False
            else: flag_order = True
        if T == 2:
            if flag_order:
                if F == 1: S = C + S
                else: S = S + C
            else:
                if F == 1: S = S + C
                else: S = C + S

    print(S if flag_order else ''.join(list(reversed(S))))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

s = deque(inp())
q = iinp()

l = deque()

p = 0
o = 1

for i in range(q):
  a = inps()
  if o == 1 and a[0] == 1:
    continue
  if a[0] == "2":
    if a[1] == "1":
      if p == 0:
        l.appendleft(a[2])
        o += 1
      else:
        s.appendleft(a[2])
        o += 1
    else:
      if p == 0:
        s.append(a[2])
        o += 1
      else:
        l.append(a[2])
        o += 1
  if a[0] == "1":
    if p == 0:
      p = 1
    else:
      p = 0
    s.reverse()
    l.reverse()

if p == 0:
  for i in l:
    print(i,end="")
  for i in s:
    print(i,end="")
  print("")
else:
  for i in s:
    print(i,end="")
  for i in l:
    print(i,end="")
  print("")

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
S_list=deque()
for k in range(len(S)):
    S_list.append(S[k])
Q=int(input())
kouho=[input().split() for i in range(Q)]
"""for i in range(Q):
    kari=input().split()
    kouho.append(kari)"""
for j in range(len(kouho)):
    if kouho[j][0]=="1":
        S_list.reverse()
    else:
        if kouho[j][1]=="1":
            S_list.appendleft(kouho[j][2])
        else:
            S_list.append(kouho[j][2])
print(*S_list,sep="")

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.buffer.readline
S = input()
Q = int(input())
#S = "a"*100000
#Q = 200000
pre = ""
suf = ""

rev = 0
for i in range(Q):
    q = input().split()
    '''
    if i % 3 == 0:
        q = ('1')
    if i % 3 == 1:
        q = (2,'1','b')
    if i % 3 == 2:
        q = (2,'2','b')
        '''

    if len(q) == 1:
        rev = (rev + 1) % 2
    else:
        _, f, c = q
        f = f.decode()
        c = c.decode()
        if f == '1':
            if rev == 0:
                pre += c
            else:
                suf += c
        else:
            if rev == 0:
                suf += c
            else:
                pre += c

S = S.decode()[:-1]
ans = pre[::-1] + S + suf
if rev == 0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    S = sr()
    Q = ir()
    rev = 1
    for i in range(Q):
        T = list(sr().split())
        if int(T[0]) == 1:
            rev *= -1
        else:
            x, F, C = T
            if int(F) == 1:
                if rev == 1:
                    S = C+S
                else:
                    S = S+C
            else:
                if rev == 1:
                    S = S+C
                else:
                    S = C+S
    if rev == -1:
        S = S[::-1]
    print(S)
resolve()

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
flg=1
for i in range(q):
  inp=input()
  if inp=='1':
    flg*=-1
  else:
    inp=inp.split()
    f,c=inp[1],inp[2]
    if f=='1'and flg==1:
      s=c+s
    elif f=='2' and flg==-1:
      s=c+s
    else:
      s+=c
if flg==-1:
  s = ''.join(list(reversed(s)))
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():return sys.stdin.readline().strip()
S=input()
Q=int(input())
for i in range(Q):
    q=input()
    if q=='1':
        S=S[::-1]
    else:
        a,f,c=q.split()
        S=''.join((c,S)) if f=='1' else ''.join((S,c)) 
print(S)

Submission status: Time Limit Exceeded

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

S = input().strip()
Q = int(input())
for i in range(Q):
    q = input().split()
    if q[0] == '1':
        S = S[::-1]
    elif q[0] == '2':
        if q[1] == '1':
            S = '{}{}'.format(q[2], S)
        elif q[1] == '2':
            S = '{}{}'.format(S, q[2])

print(S)

Submission status: Time Limit Exceeded

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

rev = 0
for i in range(q):
	g = input()
	if len(g) == 1:
		rev += 1
	else:
		f = int(g[2])
		c = g[4]
		if f == 2:
			if rev%2 == 1:
				s = c + s
			else:
				s = s + c
		else:
			if rev%2 == 0:
				s = c + s
			else:
				s = s + c
if rev%2 == 1:
	s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

print(''.join(S))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
  qn = input()
  if len(qn) == 1:
    s = s[::-1]
    #print(s)
  else:
    qm, f, c = qn.split()
    if f == '1':
      s = c + s
    else:
      s = s + c
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import collections
from collections import deque
#n = int(input())
#a, b = map(int, input().split())
s = input()
#x = list(map(int, input().split()))
q = int(input())
d = deque([])
for i in range(len(s)):
    d.append(s[i])

rev = 1
for i in range(q):
    que = list(map(str, input().split()))
    if que[0] == "1":
        rev *= -1

    else:
        if rev == 1:
            if que[1] == "1":
                d.appendleft(que[2])
            else:
                d.append(que[2])

        else:
            if que[1] == "1":
                d.append(que[2])
            else:
                d.appendleft(que[2])

if rev == 1:
    for i in range(len(d)):
        print(d[i], end="")
else:
    for i in range(len(d)):
        print(d[len(d) - i - 1], end="")

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys

input = lambda: sys.stdin.readline().rstrip()

s = deque(list(input()))
q = int(input())

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

if rev_cnt % 2 == 1:
    res = ''.join([s[i] for i in range(len(s) - 1, -1, -1)])
else:
    res = ''.join([s[i] for i in range(len(s))])

print(res)

Submission status: Time Limit Exceeded

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

x = []
for i in range(q):
    x.append(input().split())
for i in range(q-1, -1, -1):
    if x[i] == '1' and  x[i-1] == '1':
        x[i] = '0'
        x[i - 1] = '0'
    if x[i] == '1':
        for j in range(i-1, -1 , -1):
            if x[j][0] == '2':
                if x[j][1] == '1':
                    x[j][1] == '2'
                else:
                    x[j][2] == '1'
for i in range(q):
    if x[i][0] == '0':
        continue
    elif x[i][0] == '1':
        s = s[::-1]
    elif x[i][1] == '1':
        s = x[i][2] + s
    else:
        s = s + x[i][2]
print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    query = input()
    if query == "1":
        s = s[::-1]
    else:
        x, q2_1, q2_2 = query.split()
        if q2_1 == "1":
            s.insert(0, q2_2)
        else:
            s.append(q2_2)
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
s=input()
num =int(input())
for i in range(num):
    q=input().rstrip().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:
S = input()
N = int(input())
parity = True
for i in range(N):
  query = input().split()
  if query[0] == "1":
    if parity == True:
      parity = False
    else:
      parity = True
  if query[0] == "2":
    if parity:
      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 parity == False:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 
from collections import deque

def resolve():
    S = input()
    Q = int(input())
    Query = [input().split() for _ in range(Q)]

    S_deque = deque()
    for s in S:
        S_deque.append(s)

    for q in Query:
        if q[0]=='1':
            S_deque.reverse()
        elif q[0]=='2':
            if q[1]=='1':
                S_deque.appendleft(q[2])
            elif q[1]=='2':
                S_deque.append(q[2])

    print(*S_deque, sep='')

if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
qv=[list(input().split()) for _ in range(q)]
for i in range(q):
  if len(qv[i])==1:
    s=s[::-1]
  else:
    _,f,c=qv[i]
    if f=="1":
      s=c+s
    elif f=="2":
      s+=c
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
s=input().replace('\n','')
q=int(input())
for i in range(q):
    query=input()
    if int(query[0])==1:
        s=s[::-1]
    else:
        query=query.replace('\n','')
        _,f,c=map(str,query.split())
        if int(f)==1:
            s=c+s
        else:
            s=s+c
print(s)

Submission status: Time Limit Exceeded

Solution code:
def solve():
    a = input()
    a = list(a)
    q = int(input())
    
    for _ in range(q):
        _q = [i for i in input().split()]
        t, f, c =0, 0, 0
        t = int(_q[0])
        if t == 2:
            f, c = int(_q[1]), _q[2]
        
        if t == 1:
            a = list(reversed(a))
        elif t == 2:
            if f == 1:
                a = list(reversed(a))
                a.append(c)
                a = list(reversed(a))
            elif f == 2:
                a.append(c)
    
    print("".join(a))


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

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

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

print(''.join(S) if count % 2 == 0 else ''.join(S[::-1]))

Submission status: Time Limit Exceeded

Solution code:
#D
#入力
S=input()
Q=int(input())
Query=[]
for i in range(Q):
    Query.append(list(input().split()))
    
    
#処理

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

Submission status: Time Limit Exceeded

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

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:
		a,f,c = a.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())
T = [0]*Q
for i in range(0,Q):
  T[i] = list(map(str, input().split()))

nS = ""
for i in range(0,Q):
  if(T[i][0] == "1"):
    S = ''.join(list(reversed(S))) 
  else:
    if(T[i][1] == "1"):
      S = T[i][2]+S
    elif(T[i][1] =="2"):
      S = S+T[i][2]

print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
    if que[i][0] == "1":
        s = s[::-1]
    elif que[i][0] == "2":
        if que[i][1] == "1":
            sub = [que[i][2], s]
            s = ''.join(sub)
        elif que[i][1] == "2":
            sub = [s,que[i][2]]
            s = ''.join(sub)
print(s)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

S = [s for s in S]

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

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

if is_filp:
    S.reverse()
    print(''.join(S))
else:
    print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
s= input()
q= int(input())
inv = False
for i in range(q):
    p = input()
    if p[0]=='1':
        if inv:inv=False
        else:inv=True
    elif p[0]=='2':
        _,f,c= p.split()
        if f=='1':
            if inv:s = s+c
            else: s = c+s
        elif f=='2':
            if inv:s=c+s
            else:s = s + c
if inv:print(s[::-1])
else:print(s)

Submission status: Time Limit Exceeded

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

qs = []
for i in range(q):
    qs.append(input().split(' '))

rev = False
for i in range(q):

    if qs[i][0] == '1':
        rev = not rev
    else:
        if qs[i][1] == '1' and rev:
            s = s + qs[i][2] 
        elif qs[i][1] == '1' and not rev:
            s = qs[i][2] + s
        elif qs[i][1] == '2' and rev:
            s = qs[i][2] + s
        elif qs[i][1] == '2' and not rev:
            s = s + qs[i][2] 


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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = input()
que = deque(list(s))
q = int(input())
for _ in range(q):
    l = input()
    if l[0] == "1":
        s = s[::-1]
        que  = deque(list(s))
    else:
        if l[2] == "1":
            que.appendleft(l[4])
        else:
            que.append(l[4])
        s = list(que)
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
s = input()
g = 0
q = int(input())
for i in range(q):
	d = input().split(" ")
	if d[0] == "1":
		if g == 0:
			g = 1
		else:
			g = 0
	if d[0] == "2":
		f = d[1]
		if g == 0:
			if f == "1":
				s = d[2] + s
			else:
				s = s + d[2]
		else:
			if f == "1":
				s = s + d[2]
			else:
				s = d[2] + s
if g == 1:
	ans = s[::-1]
	print(ans)
else:
	print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
#At coder 2020/3/8 C
import math
S=input()
Q=int(input())
for ii in range(Q):
    aa=list(map(str,input().split())) 
    if '1'==aa[0]:
        S=S[::-1]
    else:
        if '1'==aa[1]:
                S=aa[2]+S
        else:
                S=S+aa[2]

ans=S

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  list_ = list(input().split())
  T = int(list_[0])
  if T == 1:
    S.reverse()
  else:
    F, C = int(list_[1]), list_[2]
    if F == 1:
      S.insert(0, C)
    else:
      S.append(C)
ans = ''.join(S)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

    if query[0] == "1":
        S = S[::-1]
    else:
        if query[1] == "1":
            S = query[2] + S
        else:
            S += query[2]
print(S)