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:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

s=ss()
q=ii()

arr=list(s)
chk=0
for i in range(q):
    x=input()
    if x=="1":
        if chk==1:
            chk=0
        else:
            chk=1
    
    else:
        j,k,l=map(str,x.split())

        if chk==0 and k=="1":
            arr=list(l)+arr
        elif chk==0 and k=="2":
            arr=arr+list(l)
        elif chk==1 and k=="1":
            arr=arr+list(reversed(list(l)))
        elif chk==1 and k=="2":
            arr=list(reversed(list(l)))+arr

if chk==0:
    print("".join(arr))
else:
    print("".join(reversed(arr)))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
import math
jun = 1
for _ in range(q):
  
  tmp = input()
  if len(tmp)==1:
    jun = math.ceil(jun*(-1/2)+1/2)
  else:
    _,f,c = tmp.split()
    f = int(f)
    if jun+f ==2:
      s = c+s
    else:
      s += c
if jun==1:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

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

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

        if tfc[0] == '1':
            s = s[::-1]
        else:
            f = tfc[1]
            c = tfc[2]
            if f == '1':
                s = c + s
            else:
                s = s + c

    print(s)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    for _ in range(q):
        qu = tuple(map(str, input().split()))
        if len(qu) == 1:
            s = s[::-1]
        else:
            if qu[1] == '1':
                s = qu[2] + s
            else:
                s = s + qu[2]

    print(s)
main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import copy

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

Que = [0]* Q
for i in range(Q):
    Que[i] = list(map(str, input().split()))

answer = copy.copy(S)
for i in range(Q):
    T = Que[i][0]
#    print(T)
    if int(T) == 1:
        answer = ''.join(list(reversed(answer)))
    else:
        F = Que[i][1]
        if int(F) == 1:
            answer = Que[i][2] + answer
        else:
            answer = answer + Que[i][2]
print(answer)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flag_reversed = False
ret = S
for _ in range(Q):
    temp = input()
    if (len(temp) == 1):
        flag_reversed = not flag_reversed
    else:
        T, F, C = [x for x in temp.split(" ")]
        F = int(F)
        add_begin = True
        if (F == 2):
            add_begin = False
        if (flag_reversed):
            add_begin = not add_begin
        if (add_begin):
            ret = C + ret
        else:
            ret += C

if (flag_reversed):
    ret = ret[::-1]
print(ret)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
for i in range(n):
    q = list(map(str, 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()
q = int(input())

rev = False
for _ in range(q):
    query = input().split()

    if query[0] == '1' and not(rev):
        rev = True
    elif query[0] == '1' and rev:
        rev = False

    elif query[0] == '2' and query[1] == '1' and not(rev):
        s = query[2] + s
    elif query[0] == '2' and query[1] == '1' and rev:
        s += query[2]

    elif query[0] == '2' and query[1] == '2' and not(rev):
        s += query[2]
    elif query[0] == '2' and query[1] == '2' and rev:
        s = query[2]+s

if rev:
    s=s[::-1]

print(s)

Submission status: Time Limit Exceeded

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

s = list(s)
flag = True
for query in Q:
    if len(query) == 1:
        flag = not flag
    else:
        if query[1] == '1':
            if flag:
                s.insert(0, query[2])
            else:
                s.append(query[2])
        elif query[1] == '2':
            if flag:
                s.append(query[2])
            else:
                s.insert(0, query[2])

if not flag:
    s.reverse()

print("".join(s))

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    q = input().split(" ")
    if q[0] == "1":
        S = ''.join(list(reversed(S)))
    else:
        if q[1] == "1":
            S = q[2] + S
        else:
            S += q[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	S = IS()
	Q = II()
	c = 0
	head=[]
	tail=[]
	ans = S
	for _  in range(Q):
		query = IS()
		if(len(query)==1):
			c = (c + 1)%2
		else:
			T,F,C = query.split()
			T = int(T)
			F = int(F)
			if F==1:
				if(c==0):
					ans = C + ans
				else:
					ans = ans + C
			else:
				if(c==1):
					ans = C + ans
				else:
					ans = ans + C
	if c == 0:
		ans = ans
	else:
		ns = ans[::-1]
		ans = ns
	print(ans)




if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

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


def solve(S, Q, Qvery):
    fw = True
    for qvery in Qvery:
        if qvery[0] == 1:
            if fw:
                fw = False
            else:
                fw = True
        elif qvery[0] == 2:
            if qvery[1] == 1 and fw or qvery[1] == 2 and not fw:  # 先頭に追加
                S = qvery[2] + S
            else:  # 末尾に追加
                S = S + qvery[2]
        else:
            print("Parse error")

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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = str(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    Qvery = []
    for i in range(Q):
        T = int(next(tokens))
        if T == 1:
            Qvery.append([1])
        elif T == 2:
            Qvery.append([2, int(next(tokens)), str(next(tokens))])
    solve(S, Q, Qvery)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
is_reverse=False
for i in range(q):
    qi=list(input().split())
    if(len(qi)==1):
        is_reverse = not is_reverse
    elif(qi[1]=="1"):
        if(is_reverse):
            s=s+qi[2]
        else:
            s=qi[2]+s
    else:
        if(is_reverse):
            s=qi[2]+s
        else:
            s=s+qi[2]

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

Submission status: Time Limit Exceeded

Solution code:

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

while(q > 0):
    try:
        t, f, c=map(str, input().split()) 
        if int(f) == 1:
            s = c + s
        elif int(f) == 2:
            s = s + c
    except:
        s = s[::-1]
    q-=1
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = []
for i in range(Q):
    A = input().split(" ")
    A = [int(a) if i == 0 or i == 1 else a for i, a in enumerate(A)]
    Query.append(A)

for q in Query:
    if len(q) == 1:
        S = S[::-1]
    else:
        if q[1] == 1:
            S = q[2] + S
        else:
            S = S + q[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


s = input()

n = ii()

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

    if(q[0] == '1'):
        # reverse
        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()
Q = int(input())

res = S

def add_char(s, f, c):
  if(f == "1"):
    return c + s
  else:
    return s + c

for _ in range(Q):
  q = input().split()
  
  if(q[0] == '1'): # Ti = 1
    res = res[::-1]
  
  else: #Ti = 2
    res = add_char(res, q[1], q[2])


print(res)

Submission status: Time Limit Exceeded

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



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

query=[""]*Q
for i in range(Q):
    query[i]=input()

#print(query)

for i in query:
    a=i.split(" ")
    if(len(a)==1):
        S=S[::-1]
    else:
        if(a[1]=="1"):
            S=a[2]+S
        else:
            S+=a[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
q=int(input())
RV=0
for i in range(q):
    IN=input()
    if IN=="1":
        RV+=1
    else:
        t,f,c=IN.split()
        if RV%2==0:
            if f=="1":
                S=c+S                
            else:
                S=S+c
        else:
            if f=="1":
                S=S+c
            else:
                S=c+S
if RV%2==0:
    print(S)
else:
    S=S[::-1]
    print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
ans=""
for i in range(len(S)):
    ans=S[i]+ans
Q=int(input())
tmp=0
r=[]
l=[]

for i in range(Q):
    que=input().split()
    if que[0]=='1':
        tmp=1-tmp
    if que[0]=='2':
        if que[1]=='1':
            if tmp==0:
                #S=que[2]+S
                l.append(que[2])
                #ans=ans+que[2]
            else:
                #S=S+que[2]
                r.append(que[2])
                #ans=que[2]+ans

        else:
            if tmp==0:
                #S=S+que[2]
                r.append(que[2])
                #ans=que[2]+ans
            else:
                #S=que[2]+S
                l.append(que[2])
                #ans=ans+que[2]
#print(que)
"""
for i in range(Q):
    if que[i][0]=='1':
        tmp=1-tmp
    if que[i][0]=='2':
        if que[i][1]=='1':
            if tmp==0:
                S=que[i][2]+S
            else:
                S=S+que[i][2]
        else:
            if tmp==0:
                S=S+que[i][2]
            else:
                S=que[i][2]+S
"""
#print(tmp)
for i in range(len(r)):
    S=S+r[i]
    ans=r[i]+ans
for i in range(len(l)):
    S=l[i]+S
    ans=ans+l[i]
if tmp==1:
    print(ans)
    exit()

print(S)

Submission status: Time Limit Exceeded

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

if reverseFlag == True:
    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:
    query = query.split()
    f, c = int(query[1]), query[2]
    if f == 1:
      s = c + s
    else:
      s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
# from fractions import gcd
# mod = 10 ** 9 + 7
# N = int(input())
# a = list(map(int,input().split()))
# a,b,c = map(int,input().split())
# ans = [0] * N
# math.ceilで切り上げ
# dp = [[0] * 4 for i in range(3)] #2次元配列初期化
# dp = [[[0] * 2 for i in range(3)] for j in range(5)]
# (ord('A'))でASCII出力 Aは65
import math
import statistics
import string


def intinput():
    return int(input())
def listintinput():
    return list(map(int, input().split()))
def splitintinput():
    return map(int, input().split())
def factorialsurplus(x, y, z):  # xからyまでの階乗をzで割ったあまりを求める
    ret = 1
    for i in range(x, y + 1):
        ret = (ret * i) % z
    return (ret)
def isprime(x):
    if x == 1:
        return False
    if x == 2:
        return True
    sq = int(math.sqrt(x))
    for i in range(2,sq+1):
        if x % i == 0:
            return False
    return True
def hcfnaive(a,b):#最大公約数 math.gcdが使えない時の手段として入れとく。pythonが更新されたらいらなくなるはず
    if(b==0):
        return a
    else:
        return hcfnaive(b,a%b)

s = ['']
s[0] = input()
Q = int(input())
rev_count = 0

for i in range(Q):
    a = input()
    if a[0] == "1":
        rev_count += 1
    else:
        b = a.split()
        if rev_count % 2 == 0:
            if b[1] == "1":
                s.insert(0,b[2])
            else:
                s.append(b[2])
        else:
            if b[1] == "1":
                s.append(b[2])
            else:
                s.insert(0,b[2])

if rev_count % 2 == 0:
    print(''.join(s))
else:
    a = ''.join(s)
    print(a[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
sys.setrecursionlimit(int(1e6))

S = input()
Q = int(input())
ans = S
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        ans = ans[::-1]
    elif query[1] == '1':
        ans = query[2] + ans
    else:
        ans = ans +query[2]

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

V = list(input())
S = deque()
S.extend(V)
Q = int(input())

for i in range(Q):
    c = sys.stdin.readline().split()
    #print('s:', S)
    #print('c:', c)

    if c[0] == '1':
        S.reverse()
    else:
        if c[1] == '1':
            S.appendleft(c[2])
        else:
            S.append(c[2])

print(''.join(S))

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

queryList = []

for n in range(Q):
    query = input().split(" ")
    queryList.append(query)

for query in queryList:
    if len(query) == 1:
        S.reverse()
    else:
        if query[1] == "1":
            S.insert(0, query[2])
        else:
            S.append(query[2])

print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
ans = s
for i in range(q):
    arr = list(map(str, input().split()))
    if arr[0] == '1':
        ans = ans[::-1]
    else:
        if arr[1] == '1':
            ans = arr[2] + ans
        else:
            ans += arr[2]
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
alph="abcdefghijklmnopqrstuvwxyz"
from collections import deque
import sys
input=sys.stdin.readline
moji=deque([])
s=input().rstrip()
for i in range(len(s)):
	for j in range(26):
		if s[i]==alph[j]:
			moji.append(j)
			break
q=int(input())
rev=0
for _ in range(q):
	qw=input().split()
	if qw[0]=="1":
		rev+=1
	else:
		f=qw[1]
		c=qw[2]
		for j in range(26):
			if c==alph[j]:
				x=j
				break
		if f=="1":
			if rev%2==0:
				moji.appendleft(x)
			else:
				moji.append(x)
		else:
			if rev%2==0:
				moji.append(x)
			else:
				moji.appendleft(x)
ans=""
for i in range(len(moji)):
	ans+=alph[moji[i]]
if rev%2==0:
	print(ans)
else:
	print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:

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

# print(S, Q)

queries = []
for i in range(Q):
    query = input()
    queries.append(query)


isReversed = False
for query in queries:
    if query == "1":
        isReversed = not isReversed
        # print("reverse", S)
        # S = S[::-1]
    else:
        t, f, c = query.split()
        # print(t, f, c)
        d = f=="1"
        if isReversed: d = not d
        if d:
            S = c + S
        else:
            # print("append c to tail of ", S)
            S = S + c

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
ud = False
ud_count = 0
for i in range(q):
    query = list(input().split())
    if int(query[0]) == 1:
        ud = not(ud)
        ud_count += 1
    else:
        if (int(query[1])==1 and ud==False) or (int(query[1])==2 and ud==True):
            s = query[2] + s
        else:
            s += query[2]
if ud_count%2:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
  query = input().split()
  if query[0] == "1":
    reverse_flag = not reverse_flag
  else:
    if (query[1] == "1") ^ reverse_flag:
      s = query[2] + s
    else:
      s += query[2]

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

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(Q):
    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:
s = list(str(input()))
Q = int(input())

for _ in range(Q):
    q = str(input())
    if len(q)==1:
        # print('re')
        s.reverse()
    else:
        _, f,c = q.split()
        if f =='1':
            s.insert(0,c)
        elif f =='2':
            s.append(c)
        # s.append(c)
    # print(s)
        
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

a = deque(list(input()))
n = int(input())
for i in range(n):
  s = list(input().split())
  if s[0] == "1":
    a.reverse()
  else:
    if s[1] == "1":
      a.appendleft(s[2])
    else: a.append(s[2])  
ans = "".join(a)      
print(ans)

Submission status: Time Limit Exceeded

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

def add_car(S,s,is_left,is_rev):
    if is_rev:
        is_left = not(is_left)
    if is_left:
        S = s+S
    else:
        S = S+s
    return S

is_rev = False
for q in range(Q):
    x = input().split(' ')
    if len(x) == 1:
        is_rev = not is_rev
    else:
        S = add_car(S,x[2],x[1]=='1',is_rev)

if is_rev:
    S = ''.join(reversed([s for s in S]))
print(S)

Submission status: Time Limit Exceeded

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

flag = False

for i in range(q):

    data = input()

    if int(data[0]) == 1:
        
        if flag:
            flag = False

        else:
            flag = True

    else:
        c = data[4]

        if flag:
            s = s[::-1]
            flag = False
        
        if int(data[2]) == 1:

            s = c + s

        else:
            s += c

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

Submission status: Time Limit Exceeded

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

num_turn = 0

for i in range(Q):
    query = input()
    if query == '1':
        num_turn += 1
    else:
        _, f, c = query.split()
        f = int(f)
        if f == 1:
            if num_turn%2==0:
                S = c + S
            else:
                S = S + c
        else:
            if num_turn%2==0:
                S = S + c
            else:
                S = c + S

if num_turn%2==1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

current = a

for i in range(Q):
  tmp = input().split()
  if tmp[0] == '1':
    current = current[::-1]
  if tmp[0] == '2':
    if tmp[1] == '1':
      current = tmp[2] + current
    if tmp[1] == '2':
      current = current + tmp[2]
      
print(current)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(list(input().strip()))
Q = int(input())
cnt = 0
for _ in range(Q):
    q = list(input().split())
    if int(q[0])==1:
        cnt += 1
    else:
        if cnt%2==0:
            if int(q[1])==1:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if int(q[1])==1:
                S.append(q[2])
            else:
                S.appendleft(q[2])
if cnt%2==0:
    print("".join(S))
else:
    x=""
    for i in range(len(S)-1,-1,-1):
        x += S[i]
    print(x)

Submission status: Time Limit Exceeded

Solution code:
S = input()
k = []
Q = int(input())
now = 0
ans = ""
mode = 0
for i in range(Q):
    K = input().split()
    if K[0] == "1":
        if mode == 1:
            mode = 0
        else:
            mode = 1
    else:
        if K[1] == "1":
            if mode == 0:
                #k[left] = K[2]
                S = K[2]+ S
            else:
                S = S+K[2]
        else:
            if mode == 0:
                S = S+K[2]
            else:
                S = K[2]+S
if mode == 1:
    for i in range(1,len(S)+1):
        ans += S[-i]
    print(ans)
else:
    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 = list(map(str,input().split()))
    if t[0]=="1":
        reverse += 1
    if t[0]=="2":
        if t[1]=="1":
            if reverse%2==0:
                s = addflont(s,t[2])
            else:
                s = addback(s,t[2])
        elif t[1]=="2":
            if reverse%2==0:
                s = addback(s,t[2])
            else:
                s = addflont(s,t[2])

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

Submission status: Time Limit Exceeded

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

for i in range(n):
  a = input().split()
  if int(a[0]) == 1:
    s = s[::-1]
  elif int(a[1]) == 1:
      s = a[2] + s
  elif int(a[1]) == 2:
      s = s + a[2]
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = readline().decode().rstrip()
q = int(readline())

head = ""
tail = ""
flip = 0
for _ in range(q):
    que = readline().decode().split()
    if len(que) == 1:
        head, tail = tail[::-1], head[::-1]
        flip += 1

    else:
        if que[1] == '1':

            head = ''.join([que[2], head])

        else:
            tail = ''.join([tail, que[2]])

if flip % 2 == 0:
    print(''.join([head, S, tail]))
else:
    print(''.join([head, S[::-1], tail]))

Submission status: Time Limit Exceeded

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

reverse_flag = False
for i in range(n):
    line = list(map(str, input().split()))
    if line[0] == '1':
        if reverse_flag:
            reverse_flag = False
        else:
            reverse_flag = True
    elif line[0] == '2':
        if line[1] == '1':
            if reverse_flag:
                s.append(line[2])
            else:
                s.insert(0, line[2])
        elif line[1] == '2':
            if reverse_flag:
                s.insert(0, line[2])
            else:
                s.append(line[2])

if reverse_flag:
    print(''.join(s[::-1]))
else:
    print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s = input()
Q = int(input())
direction = 1
for q in range(Q):
    t = [i for i in input().split()]
    if len(t) == 1:
        direction *= -1
    else:
        t[1] = 2*(int(t[1])%2) - 1
        if direction * t[1] == 1:
            s = t[2] + s
        else:
            s += t[2]
if direction < 0:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


a = list(input())
a.pop(-1)

Q = int(input())
count = 0
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        count += 1
        # a.reverse()
    else:
        if count % 2 == 0:
            if q[1] == "1":
                # a.insert(0, q[2])
                b = deque(a)
                b.appendleft(q[2])
                a = list(b)
            else:
                a.append(q[2])
        else:
            if q[1] == "1":
                a.append(q[2])
            else:
                # a.insert(0, q[2])
                b = deque(a)
                b.appendleft(q[2])
                a = list(b)

if count % 2 != 0:
    a.reverse()
print("".join(a))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
a = input()
q = int(input())
key = [0]*len(a)
for i in range(len(a)):
    key[i] = a[i]

p = deque(key)
for i in range(q):
    qinput = [str(x) for x in input().split()]
    if len(qinput) == 1:
        p.reverse()
    else:
        k = qinput[1]
        if k == "1":
            p.appendleft(qinput[2])
        else:
            p.append(qinput[2])
print("".join(p))