Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Time Limit Exceeded

Solution code:

S = input()
Q = int(input())
Front_flag = 1

for i in range(Q):
    Query = tuple(input().split())
    if len(Query) == 1:
        Front_flag = 1 - Front_flag
    else:
        a, b, c = Query
        if (Front_flag + int(b)) % 2 == 1:
            S += c
        else:
            S = c + S
if Front_flag:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
S = sys.stdin.readline().strip()
Q = int(sys.stdin.readline().strip())

for i in range(Q):
    Query = sys.stdin.readline()
    if len(Query) == 2:
        S = S[::-1]
    else:
        a, b, c = Query.split()
        if b == '1':
            S = c + S
        else:
            S += c
print(S)

Submission status: Time Limit Exceeded

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

tfc = [[] for _ in range(Q)]

swich = 0
for i in range(Q):
    tfc[i] = input().split()
    
    if int(tfc[i][0])==1:
        swich += 1
    else:
        if swich%2==0:
            if int(tfc[i][1])==1:
                S.insert(0,tfc[i][2])
            else:
                S.append(tfc[i][2])
        else:
            if int(tfc[i][1])==1:
                S.append(tfc[i][2])
            else:
                S.insert(0,tfc[i][2])

if swich%2==1:
    S = S[::-1]
    print("".join(S))
else:
    print("".join(S))

Submission status: Time Limit Exceeded

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

tfc = [[] for _ in range(Q)]


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

Submission status: Time Limit Exceeded

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

tfc = [[] for _ in range(Q)]


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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
for i in range(q):
    query = list(input().split())
    # print(query)
    
    if query[0]=='1':
        s.reverse()
    elif query[0]=='2' and query[1]=='1':
        s.insert(0,query[2])
    elif query[0]=='2' and query[1]=='2':
        s.append(query[2])
[print(a, end='') for a in s]

Submission status: Time Limit Exceeded

Solution code:
import collections
s=input()
l=list(s)
que=collections.deque(l)
q=int(input())
inverse=0
for i in range(0,q):
    t,*_=input().split()
    if len(_)==2:
        f=_[0]
        c=_[1]
    if t=='1':
        if inverse==1:
            inverse=0
        else:
            inverse=1
    else:
        if f=='1':
            if inverse==1:
                que.append(c)
            else:
                que.appendleft(c)
        else:
            if inverse==1:
                que.appendleft(c)
            else:
                que.append(c)
final=''
if inverse==0:
    for k in range(0, len(que)):
        final += que[k]
else:
    for k in range(len(que)-1,-1,-1):
        final += que[k]
print(final)

Submission status: Time Limit Exceeded

Solution code:
s=input()
l=list(s)
q=int(input())
inverse=0
for i in range(0,q):
    t,*_=input().split()
    if len(_)==2:
        f=_[0]
        c=_[1]
    if t=='1':
        if inverse==1:
            inverse=0
        else:
            inverse=1
    else:
        if f=='1':
            if inverse==1:
                l.append(c)
            else:
                l = [c] + l
        else:
            if inverse==1:
                l=[c]+l
            else:
                l.append(c)
final=''
if inverse==0:
    for k in range(0, len(l)):
        final += l[k]
else:
    for k in range(len(l)-1,-1,-1):
        final += l[k]
print(final)

Submission status: Time Limit Exceeded

Solution code:
s=input()
l=list(s)
q=int(input())
for i in range(0,q):
    t,*_=input().split()
    if len(_)==2:
        f=_[0]
        c=_[1]
    if t=='1':
        for j in range(0,len(l)//2):
           l[j],l[len(l)-j-1]=l[len(l)-j-1],l[j]
    else:
        if f=='1':
            l=[c]+l
        else:
            l.append(c)
final=''
for k in range(0,len(l)):
    final+=l[k]
print(final)

Submission status: Time Limit Exceeded

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

from collections import deque

S=deque(list(S))


for i in range(Q):

    Query = list(input().split())
    
    if len(Query)==1:
        S=deque(reversed(S))
    else:
        F_i ,C_i = int(Query[1]),Query[2]
        
        if F_i==1:
        
            S.appendleft(C_i)
        
        elif F_i==2:
        
            S.append(C_i)    

print(''.join(S))

Submission status: Time Limit Exceeded

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

s = input().strip('\n')
q = int(input().strip('\n'))

j = ''.join
turn = 0
for _ in range(q):
    query = input().strip('\n')
    if len(query) == 1:
        turn += 1
    else:
        two, f, c = query.split()
        if f == '1':
            if turn % 2 == 0:
                s = j([c, s])
            else:
                s = j([s, c])
        else:
            if turn % 2 == 0:
                s = j([s, c])
            else:
                s = j([c, s])

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

Submission status: Time Limit Exceeded

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

j = ''.join
turn = 0
for _ in range(q):
    query = input()
    if len(query) == 1:
        turn += 1
    else:
        two, f, c = query.split()
        if f == '1':
            if turn % 2 == 0:
                s = j([c, s])
            else:
                s = j([s, c])
        else:
            if turn % 2 == 0:
                s = j([s, c])
            else:
                s = j([c, s])

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

Submission status: Time Limit Exceeded

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

turn = 0
for _ in range(q):
    query = input()
    if len(query) == 1:
        turn += 1
    else:
        two, f, c = query.split()
        if f == '1':
            if turn % 2 == 0:
                s = ''.join([c, s])
            else:
                s = ''.join([s, c])
        else:
            if turn % 2 == 0:
                s = ''.join([s, c])
            else:
                s = ''.join([c, s])

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
for i in range(Q):
    TFC = input()
    if TFC == "1":
        S.reverse()
    else:
        if TFC[:3]== "2 1":
            S.appendleft(TFC[4])
        else:
            S.append(TFC[4])
print("".join([i for i in S]))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

count = 0
l = []
for i in range(q):
	tmp = list(map(str,input().split()))
	if len(tmp) == 1:
		count += 1
		for j in range(len(l)):
			l[j][0] += 1
	elif tmp[1] == '1':
		l.append([1, tmp[2]])
	elif tmp[1] == '2':
		l.append([2, tmp[2]])

for i in range(len(l)):
	l[i][0] %= 2

if count % 2 == 1:
	s = s[::-1]
s = deque(s)

for i in range(len(l)):
	if l[i][0] == 0:
		s.append(l[i][1])
	else:
		s.appendleft(l[i][1])
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

head = 1
for _ in range(Q):
    cmd = input()
    if cmd == "1":
        head *= -1
    elif "2 1" == cmd[:3]:
        if head == 1:
            S.appendleft(cmd[4])
        else:
            S.append(cmd[4])
    else:
        if head == 1:
            S.append(cmd[4])
        else:
            S.appendleft(cmd[4])

ans = ""
for _ in range(len(S)):
    if head == 1:
        ans += S.popleft()
    else:
        ans += S.pop()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    queries = [input() for _ in range(Q)]

    reverse = False

    for e in queries:
        if e[0] == '1':
            reverse = not reverse
        else:
            T, F, C = e.split()
            if F == '2': F = True
            else: F = False

            if reverse ^ F:
                # 末尾に追加
                S += C
            else:
                # continue
                # 先頭に追加，ここの計算がボトルネック
                S = S[::-1]
                S += C
                reverse = not reverse
        # print(S)

    if reverse:
        S = S[::-1]

    print(S)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    queries = [input() for _ in range(Q)]

    reverse = False

    for e in queries:
        if e[0] == '1':
            reverse = not reverse
        else:
            T, F, C = e.split()
            if F == '2': F = True
            else: F = False

            if reverse ^ F:
                # 末尾に追加
                S += C
            else:
                # 先頭に追加
                S = C + S
        # print(S)

    if reverse:
        S = S[::-1]

    print(S)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


ss = list(input())
n = len(ss)-1

s = deque([])
for i in range(n):
    s.append(ord(ss[i]))

p = 1

q = int(input())
for i in range(q):
    inp = list(input())

    if inp[0] == '1':
        p *= -1
    else:
        c = ord(inp[4])
        f = inp[2]
        if (f == '1' and p == 1) or (f == '2' and p == -1):
            s.appendleft(c)
        else:
            s.append(c)


n = len(s)
if p == 1:
    for e in s:
        print(chr(e),end = '')
else:
    for i in range(n-1,-1,-1):
        print(chr(s[i]),end = '')

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
parity = 0
ans = ["", ""]

for i in range(Q):
    tmp = list(input().split())
    if tmp[0] == "1":
        parity += 1
    else:
        f, c = tmp[1], tmp[2]
        ans[(parity + int(f)) % 2] += c

plt = ""

if parity % 2 == 1:
    p = -1
else:
    p = 1

plt += S[::p]

if p == 1:
    plt = ans[1][::-1] + plt + ans[0]
else:
    plt = ans[0][::-1] + plt + ans[1]

print(plt)

Submission status: Time Limit Exceeded

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

ok = True
for i in range(q):
    query = input()
    if query[0] == '1':
        if ok:
            ok = False
        else:
            ok = True
    else:
        query = query.split()
        if ok:
            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 not ok:
    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[0] == '1':
        s = s[::-1]
    else:
        query = query.split()
        if query[1] == '1':
            s = query[2] + s
        else:
            s = s + query[2]
print(s)

Submission status: Time Limit Exceeded

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

# len(S) <= 100000
# lenq <= 2 * 10 ** 5

# q == '1' のとき、実際に反転すると O(N)かかるので反転しないで、
# どちらが先頭かだけの情報を持っておく
head = 1 # or -1
for q in Q:
    if q == '1':
        head *= -1
    else:
        _, f, c = q.split()
        if f == '1':
            if head == 1:
                S = c + S
            else:
                S = S + c
        else:  # f == '2'
            if head == 1:
                S = S + c
            else:
                S = c + S
if head == -1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

# len(S) <= 100000
# lenq <= 2 * 10 ** 5
for q in Q:
    if q == '1':
        S = S[::-1]
    else:
        _, f, c = q.split()
        if f == '1':
            S = c + S
        else:
            S = S + c

print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
for i in range(int(input())):
    a=input()
    if a[0]=="1":
        s=s[::-1]
    elif a[0]=="2":
        if a[2]=="1":
            s=s[::-1]
            s=s+a[4]
            s=s[::-1]
        else:
            s=s+a[4]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
quest = [0]*q
state = 0
# former, latter = '', ''
for i in range(q):
    que = input().split()
# for que in quest:
    if (que[0] == '1'):
        # 反転は計算量が多いので一度だけする。
        state = 1 - state
        continue
    else:
        if (state == 0 and que[1] == '1'):
            s = que[2] + s
        elif(state == 0 and que[1] == '2'):
            s = s + que[2]
        elif (state == 1 and que[1] == '1'):
            s = s + que[2]
        else:
            s = que[2] + s

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
quest = [0]*q
state = 0
for i in range(q):
    que = input().split()
# for que in quest:
    if (que[0] == '1'):
        # 反転は計算量が多いので一度だけする。
        state = 1 - state
        continue
    else:
        if (state == 0 and que[1] == '1'):
            s = que[2] + s
        elif(state == 0 and que[1] == '2'):
            s = s + que[2]
        elif (state == 1 and que[1] == '1'):
            s = s + que[2]
        else:
            s = que[2] + s

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
quest = [0]*q
for i in range(q):
    quest[i] = input().split()
state = 0
for que in quest:
    if (que[0] == '1'):
        # 反転は計算量が多いので一度だけする。
        state = 1 - state
        continue
    else:
        if (state == 0 and que[1] == '1'):
            s = que[2] + s
        elif(state == 0 and que[1] == '2'):
            s = s + que[2]
        elif (state == 1 and que[1] == '1'):
            s = s + que[2]
        else:
            s = que[2] + s

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    q = list(map(str, input().split()))
    if q==["1"]:
        flag *= -1
    else:
        if flag==1 and q[1]=="1":
            S = q[2]+S
        elif flag==1 and q[1]=="2":
            S += q[2]
        elif flag==-1 and q[1]=="1":
            S += q[2]
        else:
            S = q[2]+S

if flag==1:
    print(S)
else:
    ans = ""
    for i in reversed(S):
        ans += i
    print(ans)
    # print("".join(list(reversed(S))))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    q = list(map(str, input().split()))
    if q==["1"]:
        flag *= -1
    else:
        if flag==1 and q[1]=="1":
            S = q[2]+S
        elif flag==1 and q[1]=="2":
            S += q[2]
        elif flag==-1 and q[1]=="1":
            S += q[2]
        else:
            S = q[2]+S

if flag==1:
    print(S)
else:
    print("".join(list(reversed(S))))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
count = 0
for i in range(Q):
  inputstr = input()
  if len(S) > 1 and inputstr[0] == "1":
    count += 1
  else:
    if inputstr[2] == "1":
      if count % 2 == 0:
        S = "".join([inputstr[4], S])
      else:
        S = "".join([S, inputstr[4]])
    else:
      if count % 2 == 0:
        S = "".join([S, inputstr[4]])
      else:
        S = "".join([inputstr[4], S])

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
count = 0
for i in range(Q):
  inputstr = input()
  if len(S) > 1 and inputstr[0] == "1":
    count += 1
  else:
    if inputstr[2] == "1":
      if count % 2 == 0:
        S = inputstr[4] + S
      else:
        S = S + inputstr[4]
    else:
      if count % 2 == 0:
        S = S + inputstr[4]
      else:
        S = inputstr[4] + S

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
for i in range(Q):
    T=input()
    if len(T) == 1:
        S = S[::-1]
    else:
        if T[2]=='1': 
            S= T[4]+S
        else:
            S+=T[4]
print(S)

Submission status: Time Limit Exceeded

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

reverse = False
for i in range(Q):
    inp = input().split()
    if inp[0] == "1":
        reverse = not reverse
        #S = S[::-1]
    else:
        f, c = inp[1:]
        if int(f) == 1:
            if reverse:
                S.append(c)
            else:
                S.insert(0, c)
        else:
            if reverse:
                S.insert(0, c)
            else:
                S.append(c)
S = "".join(S)
print(S[::-1] if reverse else S)

Submission status: Time Limit Exceeded

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

reverse = False
cnt = 0
for i in range(Q):
    inp = input().split()
    if inp[0] == "1":
        cnt += 1
        reverse = (cnt % 2 != 0)
        #S = S[::-1]
    else:
        f, c = inp[1:]
        if int(f) == 1:
            if reverse:
                S.append(c)
            else:
                S.insert(0, c)
        else:
            if reverse:
                S.insert(0, c)
            else:
                S.append(c)
S = "".join(S)
print(S[::-1] if reverse else S)

Submission status: Time Limit Exceeded

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

reverse = False
cnt = 0
for i in range(Q):
    inp = input().split()
    if inp[0] == "1":
        cnt += 1
        reverse = (cnt % 2 != 0)
        #S = S[::-1]
    else:
        f, c = inp[1:]
        if int(f) == 1:
            if reverse:
                S = S + c
            else:
                S = c + S
        else:
            if reverse:
                S = c + S
            else:
                S = S + c
print(S[::-1] if reverse else S)

Submission status: Time Limit Exceeded

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

reverse = False
cnt = 0
for i in range(Q):
    inp = input().split()
    if len(inp) == 1:
        cnt += 1
        reverse = (cnt % 2 != 0)
        #S = S[::-1]
    else:
        f, c = inp[1:]
        if int(f) == 1:
            if reverse:
                S = S + c
            else:
                S = c + S
        else:
            if reverse:
                S = c + S
            else:
                S = S + c
print(S[::-1] if reverse else S)

Submission status: Time Limit Exceeded

Solution code:
import sys



prase = input()
q = int(input())
query = [None]*q

for i in range(q):
    query[i] = [n for n in input().split()]
counter =0

for i in range(1,q+1):
    if query[i-1][0] == "1":
        counter +=1

    else :
        if query[i-1][1] =="1" and counter%2 ==0:
            prase = query[i-1][2] + prase

        elif query[i-1][1] =="1" and counter%2 ==1:
            prase =  prase + query[i-1][2]

        elif query[i-1][1] =="2" and counter%2 ==0:
            prase = prase  + query[i-1][2]
        elif query[i-1][1] =="2" and counter%2 ==1 :
            prase  = query[i-1][2] + prase


if counter%2==0:
    print(prase)
    sys.exit()

else :
    prase = prase[::-1]
    print(prase)

Submission status: Time Limit Exceeded

Solution code:
import sys



s = input()
q = int(input())
query = [None]*q

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

prase = s
pr = ""
pl = ""
counter =0

for i in range(1,q+1):
    if query[i-1][0] == "1":
        counter +=1

    else :
        if query[i-1][1] =="1" and counter%2 ==0:
            prase = query[i-1][2] + prase

        elif query[i-1][1] =="1" and counter%2 ==1:
            prase =  prase + query[i-1][2]

        elif query[i-1][1] =="2" and counter%2 ==0:
            prase = prase  + query[i-1][2]
        elif query[i-1][1] =="2" and counter%2 ==1 :
            prase  = query[i-1][2] + prase


if counter%2==0:
    print(prase)
    sys.exit()

else :
    prase = prase[::-1]
    print(prase)

Submission status: Time Limit Exceeded

Solution code:
import sys



s = input()
q = int(input())
query = [None]*q
prase = [None]*(q+1)

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

prase[0] = s

for i in range(1,q+1):
    if query[i-1][0] == "1":
        prase[i] = prase[i-1][::-1]

    else :
        if query[i-1][1] == "1":
            prase[i] = query[i-1][2] + prase[i-1]
        else :
            prase[i] = prase[i-1] + query[i-1][2]

print(prase[q])

Submission status: Time Limit Exceeded

Solution code:

S = list(input())
Q = int(input())
L = [input() for i in range(Q) ]
for i , x in enumerate(L,1):
    if len(x) == 1:
        S = S[::-1]
    else:
        if '1' in x:
            l = list(x)
            S.insert(0,l[4]) #0番目にl[4]を追加
        else:
            l = list(x)
            S.append(l[4])

print(''.join(S)) #リストを文字列に変換

Submission status: Time Limit Exceeded

Solution code:
import sys



s = input()
q = int(input())
query = [None]*q
prase = [None]*(q+1)

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

prase[0] = s

for i in range(1,q+1):
    if query[i-1][0] == "1":
        prase[i] = prase[i-1][::-1]

    elif query[i-1][0] == "2":
        if query[i-1][1] == "1":
            prase[i] = query[i-1][2] + prase[i-1]
        elif query[i-1][1] == "2":
            prase[i] = prase[i-1] + query[i-1][2]

print(prase[q])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    inp = input().split()
    if len(inp) == 1:
        S = S[::-1]
    else:
        f, c = inp[1:]
        if int(f) == 1:
            S = c + S
        else:
            S = S + c
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if __name__ == "__main__":
    main()

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:
        _, f, c = query.split()
        if f == '1':
            s = c+s
        else:
            s = s+c

print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
for i in range(Q):
    Q_i = list(input().split())
    if Q_i[0] == "1":
        S.reverse()
    elif Q_i[1] == "1":
        S.appendleft(Q_i[2])
    else:
        S.append(Q_i[2])
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
for i in range(n):
  a=list(input())
  if a[0]=="1":
    s=s[::-1]
  else:
    if a[2]=="1":
      s=a[4]+s
    else:
      s=s+a[4]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = deque(list(input()))
Q = int(input())

for q in range(Q):
    query = list(input().split())
    if len(query) > 1:
        if query[1] == "1":
            S.appendleft(query[2])
        elif query[1] == "2":
            S.append(query[2])
    else:
        S.reverse()

print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
def main():
    from collections import deque
    import sys
    def input():
        return sys.stdin.readline()[:-1]
    S=input()
    Q=int(input())
    A=[input().split() for _ in range(Q)]
    d=deque(S)
    d_re=d.reverse
    d_left=d.appendleft
    d_right=d.append
    for x in A:
        if x[0]=="1":
            d_re()
        else:
            if x[1]=="1":
                d_left(x[2])
            else:
                d_right(x[2])

    print("".join(d))    

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from collections import deque
    S=input()
    Q=int(input())
    A=[input().split() for _ in range(Q)]
    d=deque(S)
    for x in A:
        if x[0]=="1":
            d.reverse()
        else:
            if x[1]=="1":
                d.appendleft(x[2])
            else:
                d.append(x[2])

    print("".join(d))    

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
s = list(S)
Q = int(input())
p = [[0 for i in range(3)]for j in range(Q)]
x = 1
for i in range(Q):
    p[i] = list(map(str,input().split()))
def add(s,x,f,c):
    if x ==1:
        if f == 1:
            s.insert(0,str(c))
        else:
            s.append(str(c))
    else:
        if f == 1:
            s.append(str(c))
        else:
            s.insert(0,str(c))
for i in range(Q):
    if int(p[i][0]) == 1:
        x = -x
    else:
        add(s,x,int(p[i][1]),p[i][2])
if x == 1:
    print(''.join(s))
else:
    s.reverse()
    print(''.join(s))

Submission status: Time Limit Exceeded

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

for i in range(0,Q,1):
    q = input()
    if q[0]=="1":
        S=S[::-1]

    elif q[0]=="2":
        num,F,C = q.split(sep=" ")
        if F=="1":
            S=C+S
        elif F=="2":
            S=S+C
print(S)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s = input()
q = int(input())
rbef = str()
aft = str()
sgn = 1
for i in range(q):
    qi = np.array(input().split(),dtype=str)
    if qi[0] == "1":
        sgn *= -1
    else:
        if (qi[1] == "1" or sgn == -1) and (qi[1] == "2" or sgn == 1):
            rbef += qi[2]
        else:
            aft += qi[2]

bef = ''.join(list(reversed(rbef)))
ans = bef+s+aft

if sgn == 1:
    print(ans)
else:
    rans = ''.join(list(reversed(ans)))
    print(rans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s = input()
q = int(input())
sgn = 1
for i in range(q):
    qi = np.array(input().split(),dtype=str)
    if qi[0] == "1":
        sgn *= -1
    else:
        if (qi[1] == "1" or sgn == -1) and (qi[1] == "2" or sgn == 1):
            s = qi[2] + s
        else:
            s = s + qi[2]

if sgn == 1:
    print(s)
else:
    news = ''.join(list(reversed(s)))
    print(news)

Submission status: Time Limit Exceeded

Solution code:

def main():
    S = input()
    Q = int(input())
    qi = [[0 for j in range(3)] for i in range(Q)]
    
    for i in range(Q):
        n = input()
        if n == 1:
            qi[i][0] = 1
        else:
            qi[i]= n.split()

        if qi[i][0] == "1":
            S = S[::-1]
        elif qi[i][0] == "2":
            if qi[i][1] == "1":
                S = str(qi[i][2]) + S
            elif qi[i][1] == "2":
                S = S + str(qi[i][2]) 
    print(S)

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
"""

def main():
    S = input()
    Q = int(input())
 
    qi = [[0 for j in range(3)] for i in range(Q)]
    for i in range(Q):
        n = input()
        if n != 1:
            qi[i]= n.split()
        else:
            qi[i][0] = 1

    #for i in range(Q):

        if qi[i][0] == "1":
            S = S[::-1]
        elif qi[i][0] == "2":
            if qi[i][1] == "1":
                S = str(qi[i][2]) + S
            elif qi[i][1] == "2":
                S = S + str(qi[i][2]) 
    print(S)

if __name__ == "__main__":
	main()