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=str(input())
Q=int(input())
which=0#0 normal #1 reverse
check=[list(map(str,input().split())) for i in range(Q)]
front=[]
back=[]
#print(check)
for i in range(Q):
    if check[i][0]=="1":
        which+=1
        which%=2
        continue
    if check[i][0]!="1":
        if which==0:
            if check[i][1]=="1":
                front.insert(0,check[i][2])
            if check[i][1]=="2":
                back.append(check[i][2])
        if which==1:
            if check[i][1]=="1":
                back.append(check[i][2])
            if check[i][1]=="2":
                front.insert(0,check[i][2])
F="".join(front)
B="".join(back)
S=F+S+B
if which==1:
    print("".join(S[len(S)-i-1] for i in range(len(S))))
if which==0:
    print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()

q = int(input())
r = 0
b = ""
c = ""
for i in range(q):
    A = list(input().split())
    if A[0] == '1':
        r += 1
    else:
        if (A[1] == '1' and r % 2 == 0) or (A[1] != '1' and r % 2 != 0):
            b = A[2] + b
        else:
            c += A[2]

if r % 2 == 0:
    print(b + s + c)
else:
    s = c[::-1] + s[::-1] + b[::-1]
    print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(0,q):
    line = input()
    if len(line) == 1:
        s = s[::-1]
    else:
        t, f, c = line.split(" ")
        if int(f) == 1:
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
import sys
from collections import deque
d=deque()
for _ in S:
  d.append(_)

for _ in range(Q):
  s = list(sys.stdin.readline())
  if s[0]=="1":
    d.reverse()
  else:
    if s[2]=="1":
      d.appendleft(s[4])
    else:
      d.append(s[4])
      
l=list(d)
print("".join(l))

Submission status: Time Limit Exceeded

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

query =[]

for i in range(q):
	a = list(map(str,input().split()))
	a[0] = int(a[0])
	
	if len(a) == 1:
		if a[0] == 1:
			s.reverse()
	else:
		a[1] = int(a[1])
		if a[0] == 2:
			if a[1] == 1:
				s.insert(0,a[2])
			elif a[1] == 2:
				s.append(a[2])
ans = ""
for i in range(len(s)):
	ans += s[i]
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(q):
    inp = input()
    o = int(inp.split()[0])
    if o == 1: 
        s = s[::-1]
    else:
        dmy, f, c = inp.split()
        f = int(f)
        if f == 1:
            s = c + s
        else:
            s = s + c

print(s)

Submission status: Time Limit Exceeded

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

print(s)

Submission status: Time Limit Exceeded

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

count = 0

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

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

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)]
Qlen = len(Query)
for i in range(Qlen):
    if int(Query[i][0]) == 1:
        Slist= Slist[::-1]
    else:
        if int(Query[i][1]) == 1:
            Slist = [Query[i][2]] + Slist
        else:
            Slist = Slist + [Query[i][2]]
print("".join(Slist))

Submission status: Time Limit Exceeded

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

s = input().rstrip("\n")
q = int(input())

for i 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 = s + query[2]

    #print("{}回目:{}".format(i+1,s))

print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    t_f_c = list(input().split())
    if t_f_c[0] == "1":
        s = list(s)
        s.reverse()
        s = "".join(s)
    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())
m = 0
for i in range(Q):
  q = input()
  if q == '1':
    m += 1
  else:
    _, f, c = q.split(' ')
    f = int(f)
    if (m%2 + f) % 2 == 0: #元順で末尾または逆順で先頭
      S = S + c
    else:
      S = c + S
if m % 2 == 1:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

rev = 0

for i in range(Q):
    query = input()
    ti = list(query)[0]
    if ti == '1':
        rev = (rev + 1) % 2
    else:
        ti, fi, ci = query.split()
        if (int(fi) + rev) == 2:
            s.append(ci)
        else:
            s.insert(0, ci)

if rev == 1:
    s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
def f(s):
    if s == '1':
        return '1' , '0' , '0'
    else:
        lst = s.split()
        return lst[0], lst[1], str(lst[2] )
qu = [f(input()) for i in range(q)]

def hanten(c):
    new_c = ''.join(list(reversed(c)))
    return new_c
def f1(c, d):
    return d + c
def f2(c, d):
    return c + d

for a, b, c in qu:
    if a == '1':
        s= hanten(s)
    if a == '2':
        if b == '1':
            d = c
            s = f1(s, d)
        if b == '2':
            d = c
            s = f2(s,d)
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    inp = input()
    if len(inp) == 1:
        S = S[::-1]
    else:
        T, F = map(int, inp.split()[:2])
        C = inp.split()[-1]
        if F == 1:
            S = C + S
        else:
            S = S + C

print(S)

Submission status: Time Limit Exceeded

Solution code:
letter=list(input())
n=int(input())
for i in range(n):
    ope=input().split(" ")
    o1=int(ope[0])
    if o1 ==1:
        letter.reverse()
    else:
        o2,o3_s=int(ope[1]),ope[2]
        if o2==1:
            for j in range( 1,len(o3_s)+1):
                letter.insert(0,o3_s[len(o3_s)-j])
        else:
            for j in range(len(o3_s)):
                letter.append(o3_s[j])
print("".join(letter))

Submission status: Time Limit Exceeded

Solution code:
syoki_str = input()
syori_cnt = int(input())

for i in range(syori_cnt):
    line = input().split(" ")
    if line[0] == "1":
        new_str_l = list(reversed(syoki_str))
        new_str = ''.join(new_str_l)
        syoki_str = new_str
    else:
        if line[1] == "1":
            syoki_str = line[2] + syoki_str
        else:
            syoki_str = syoki_str + line[2]

print(syoki_str)

Submission status: Time Limit Exceeded

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

for _ in range(q):
  qs = input()
  if qs == '1':
    s = s[::-1]
  else:
    t, f, c = qs.split()
    if f == '1':
      s = c + s
    else:
      s += c
print(s)

Submission status: Time Limit Exceeded

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

r_flag = False

for i in range(Q):
    cmd = raw_input().split()
    if cmd[0] == '1':
#        S.reverse()
        if r_flag == False:
            r_flag = True
        else:
            r_flag = False
    else: # cmd[0] == 2
        if (cmd[1] == '1' and r_flag == False) or (cmd[1] == '2' and r_flag == True):
#            S.insert(0, cmd[2])
            S = cmd[2] + S
        else:
#            S.append(cmd[2])
            S = S + cmd[2]

if r_flag == False:
#    print ''.join(S)
    print S
else:
#    S.reverse()
#    print ''.join(S)
    print S[::-1]

Submission status: Time Limit Exceeded

Solution code:
S=input()
for i in range(int(input())):
    T = input()
    if T == '1':
        S = S[::-1]
    else:
        ta = T.split()
        F = int(ta[1])
        C = ta[2]
        if F == 1:
           S = C + S
        else:
           S = S + C

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
num = int(input())
t = "X"
reverse = False

for _ in range(num):
    row = input().split()
    if len(row) == 1:
        if reverse == False:
            reverse = True
        else:
            reverse = False
    else:
        if row[1] == "1":
            if reverse:
                t = t + row[2]
            else:
                t = row[2] + t
        else:
            if reverse:
                t = row[2] + t
            else:
                t = t + row[2]

if reverse:
    t = t.replace("X", s)
    print(t[::-1])
else:
    t = t.replace("X", s)
    print(t)

Submission status: Time Limit Exceeded

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

topFlg = True
queries = [list(map(str, input().split())) for _ in range(Q)]

for query in queries:
    if query[0] == "1":
        topFlg = not (topFlg)
    else:
        if query[1] == "1":
            S = query[2] + S if topFlg else S + query[2]
        else:
            S = S + query[2] if topFlg else query[2] + S

if not topFlg:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
    query = input()
    if query == '1':
        s = s[::-1]
    else:
        t,f,c = query.split()
        if f == '1':
            s = c + s
        elif f == '2':
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
s1="";s2=""
adds=[s1,s2]
sta=0
for i in range(q):
    a=tuple(input().split(" "))#;print(a)
    if a[0]=="1":
        sta^=1
    else:
        if a[1]=="1":
            if not sta:
                s1=a[2]+s1
            else:
                s2+=a[2]
        if a[1]=="2":
            if sta:
                s1=a[2]+s1
            else:
                s2+=a[2]                
ans=s1+s+s2
#print(s1,s,s2)
if sta:
    print(ans[::-1])
else:print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  q = list(input())
  if q[0] == '1':
    is_reversed = True if is_reversed == False else False
  else:
    if q[2] == '1' and is_reversed:
      S.append(q[4])
    elif q[2] == '1': 
      S.insert(0, q[4])
    elif is_reversed:
      S.insert(0, q[4])      
    else:
      S.append(q[4])
      
if is_reversed:
  S.reverse()
  
print(''.join(S))

Submission status: Time Limit Exceeded

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

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

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

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]
	S = list(S)
	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
	S = "".join(S)
	if flg == True:
		print(S)
	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())

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

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    s = input()
    q = int(input())
    qall = []
    for _ in range(q):
        inp = input()
        if inp == "1":
            s = s[::-1]
        else:
            z = inp.split()
            b = int(z[1])
            c = z[2]
            if b == 1:
                s = c + s
            else:
                s = s + c
    print(s)
resolve()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
S = input()
Q = int(input())
reverse_cnt = 0
index = np.array([0 for _ in range(Q)])
s = []
for i in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse_cnt += 1
        index = index * -1
    else:
        s.append(query[2])
        if query[1] == "1":
            index[i] = -1
        else:
            index[i] = 1

index = [i for i in index if i != 0]

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

for k, j in zip(index, s):
    if k == 1:
        S += j
    else:
        S = j + S

print(S)

Submission status: Time Limit Exceeded

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

roll = False
ans = [s]
preNum = 0

for i in range(q) :
    inp = input()
    if inp[:1] == '1' :
        roll = not roll
        #print(roll)
    else :
        t, f, c = inp.split()
        #print(ans)
        #print(t, f, c)
        if (f == '1' and (not roll)) or (f == '2' and roll) :
            ans = ans[::-1]
            #print(ans, 'rev')
            ans.append(c)
            #print(ans, '+C')
            ans = ans[::-1]
            #print(ans, 'ver')
            preNum += 1
        else :
            ans.append(c)
#print(ans)
if roll :
    ans[preNum] = ans [preNum][::-1]
    ans = ans[::-1]

#print(ans)

print("".join(ans))

Submission status: Time Limit Exceeded

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

for i in range(Q):
  if P[i][0] == '1':
    S = S[::-1]
    
  if P[i][0] == '2' and P[i][1] == '2':
    S += P[i][2]
  elif P[i][0] == '2' and P[i][1] == '1':
    S = P[i][2] + S
    
print(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
    
    if t[i] == '1':
        S = S[::-1]
    else:
        if f[i] == '1':
            S[0:0] = c[i]
        elif f[i] == '2':
            S.extend(c[i])
        


print(''.join(S))

Submission status: Time Limit Exceeded

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

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

if num%2 == 0:
  print(ans)
else:
  print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]

ans=[]
for s in S:
  ans.append(s)
for q in query:
  if len(q)==1:
    ans=ans[::-1]
  else:
    _,f,c=q
    if f=='1':
      ans.insert(0,c)
    else:
      ans.append(c)
print(*ans,sep='')

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    r = s[::-1]
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            s,r = r,s
        else:
            if query[1] == "1":
                s = "".join([query[2],s])
                r = "".join([r,query[2]])
            if query[1] == "2":
                s = "".join([s,query[2]])
                r = "".join([query[2],r])
    print(s)



if __name__ == '__main__':
    main()

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
    else:
        if mode == 1:
            if hoge[1] == '1':
                a = hoge[2] + a
            else:
                a = a + hoge[2]
        else:
            if hoge[1] == '2':
                a = hoge[2] + a
            else:
                a = a + hoge[2]

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

print(a)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
inv = 1
for i in range(q):
  t = input()
  if t[0] == '1':
    inv *= -1 #s = s[::-1]
  else:
    _, f, c = map(str, t.split())
    if (f =='1' and inv == 1) or (f =='2' and inv == -1):
      s = c+s
    else:
      s = s+c

print(s[::inv])

Submission status: Time Limit Exceeded

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

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:
		print(s[::-1])

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

S = input()
Q = int(input())
# print("S:{}".format(S))
# print("Q:{}".format(Q))
query = []

for i in range(Q):
    query.append(input().split())
# for i in query:
#     print(i)
new_s=""
leng= len(S)
for i in query:
    if len(i) == 1:
        S = S[::-1]
    else:
        if i[1]=="1":
            S = i[2]+S
        else:
            S = S+i[2]
print(S)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:

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

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



for i in range(q):
    inp = input().split()
    if inp[0] == "1":
        s = s[::-1]
    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())

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 = reverse^1
    if t[0]=="2":
        if t[2]=="1":
            if reverse==0:
                s = addflont(s,t[4])
            else:
                s = addback(s,t[4])
        elif t[2]=="2":
            if reverse==0:
                s = addback(s,t[4])
            else:
                s = addflont(s,t[4])

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

Submission status: Time Limit Exceeded

Solution code:
#D - String Formation
S = list(str(input()))
Q = int(input())
q = [input().split() for i in range(Q)]

#print(S)
#print(q)

for i in range(Q):
    tmp1 = ""
    if q[i][0] == '1':
        S.reverse()
       # tmp1 = S[0]
       # S[0] = S[-1]
       # S[-1] = tmp1
    else:
        if q[i][1] == "1":
            S.insert(0,q[i][2])
        else:
            S.append(q[i][2])
ans = ''.join(S)
print(ans)

Submission status: Time Limit Exceeded

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

#一番最後に1度だけ反転すればよい
reverse_count = 0
for i in range(q):
  y = input().split()
  if y[0] == '1':
    #反転は最後に行う
    reverse_count += 1
    continue
  elif y[0] == '2':
    if y[1] == '1':
      #文字列の先頭に追加。反転回数が奇数なら、末尾に追加と同義
      if reverse_count % 2 == 1:
        s = s + y[2]
      else:
        s = y[2] + s
    elif y[1] == '2':
      #文字列の末尾に追加。反転回数が奇数なら、先頭に追加と同義
      if reverse_count % 2 == 1:
        s = y[2] + s
      else:
        s = s + y[2] 

#最後に一度だけ反転させる
if reverse_count % 2 == 0:
  #元の順序に戻るので、そのまま
  print(s)
else:
  print(s[::-1])

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=a[::-1]
    else:
        if b[1]=="1":
            a=b[2]+a
        else:
            a+=b[2]
print(a)

Submission status: Time Limit Exceeded

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

S = "".join(S)
if count%2 == 0:
    print(S.replace("S", s))
else:
    s = s[::-1]
    print(S.replace("S", s))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    Query = input()
    if Query[0] == "1":
        Flag = not Flag
    else:
        QueryList = Query.split()
        if Flag:
            if QueryList[1] =="1":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]
        else:
            if QueryList[1] =="2":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]

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

Submission status: Time Limit Exceeded

Solution code:
def hantai(s):
    new_s = ""
    for i in s:
        new_s = i+new_s
    return new_s

S = input()
Q = int(input())
for _ in range(Q):
    inp = input().split()
    if inp[0] == "2":
        if inp[1] == "1":
            S = inp[2] + S
        else:
            S = S + inp[2]
    else:
        S=hantai(S)
print(S)

Submission status: Time Limit Exceeded

Solution code:
def main():
  s=list(input())
  q=int(input())
  ok=1
  for i in range(q):
    query=list(input().split())
    if query[0]=='1':
      ok=(ok+1)%2
    else:
      f,c=query[1],query[2]
      if ok:
        if f=='1':
          s=[c]+s
        else:
          s+=[c]
      else:
        if f=='1':
          s=s+[c]
        else:
          s=[c]+s
  if ok:
    print(''.join(s))
  else:
    print(''.join(s[::-1]))
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  S=input()
  S_list=list([])
  for i in range(len(S)):
    S_list+=[S[i]]
  c=0
  Q=int(input())
  for i in range(Q):
    s=list(map(str,input().split()))
    if s[0]=='1':
      c+=1
    else:
      if s[1]=='1':
        if c%2==0:
          S_list=[s[2]]+S_list
        else:
          S_list+=[s[2]]
      else:
        if c%2==0:
          S_list+=[s[2]]
        else:
          S_list=[s[2]]+S_list
  if c%2==1:
    S_list.reverse()
  ans=""
  for i in range(len(S_list)):
    ans=ans+S_list[i]
  print(ans)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

now_reverse = False
l = [s]
tmp = ''

for _ in range(n):
  query = input()

  if query == '1':
    now_reverse = not now_reverse
    l.append(-1)
    tmp = 0
    continue

  
  _,f,c = query.split()
  if f == '1' and now_reverse:
    s = "{}{}".format(s,c)
  elif f == '1' and not now_reverse:
    s = "{}{}".format(c,s)
  elif f == '2' and now_reverse:
    s = "{}{}".format(c,s)
  else:
    s = "{}{}".format(s,c)
  
if now_reverse:
  s = reversed(s)

for c in s:
  print(c,end='')
print()

Submission status: Time Limit Exceeded

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

#last = 1

for i in range(n):
    if l[i][0] == '1':
        save = s
        s = sc
        sc = save
    elif l[i][1] == '1':
        s = l[i][2] + s
        sc += l[i][2]
    else:
        s += l[i][2]
        sc = l[i][2] + sc

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys

s=input()
q=int(input())
for i in range(q):
    Q = input().split()
    if len(Q)==1:
        s=''.join(list(reversed(s)))
    elif 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())
flip = -1
strs_f = []
strs_b = []

for i in range(q):
    query = list(map(str, input().split()))
    if query[0] == '1':
        flip = flip * -1
    else:
        if query[1] == '1':
            if flip == -1:
                strs_f.insert(0,query[2])
            else:
                strs_b.append(query[2])
        else:
            if flip == -1:
                strs_b.append(query[2])
            else:
                strs_f.insert(0,query[2])
 
strs_f = ''.join(strs_f)
strs_b = ''.join(strs_b)
ans = strs_f + s + strs_b

if flip == 1:
    ans = ans[::-1]
print(ans)