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 = list(input())
q = int(input())
ans = ""

for i in range(q):
    l = list(map(str,input().split()))
    t = int(l[0])
    if t == 1:
        s.reverse()
    elif t == 2:
        f = int(l[1])
        c = str(l[2])
        if f == 1:
            s.insert(0,c)
        else:
            s.append(c)

print(''.join(s))

Submission status: Time Limit Exceeded

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

rev = 0

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        rev += 1
    else:
        if (t[1] == "1" and rev % 2 == 0) or (t[1] == "2" and rev % 2 != 0):
            s = "".join([t[2],s])
        else:
            s = "".join([s,t[2]])

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

Submission status: Time Limit Exceeded

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

str_list_ = [input().split(' ') for q in range(Q)]

for str_list in str_list_:
    if str_list[0]=='1':
        S=S[::-1]
    elif str_list[0]=='2':
        if str_list[1]=='1':
            S='%s%s'%(str_list[2], S)
        elif str_list[1]=='2':
            S='%s%s'%(S, str_list[2])
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

count = 1
for i in range(q):
#     print(s)
    q_y_i = input().split()
#     print(q_y_i)
#     print(count)
    if q_y_i[0] == '1':
        count +=1
    else:
        if count % 2 == 1:
            if q_y_i[1] == '1':
                s = q_y_i[2] + s
#                 print('p1')
            elif q_y_i[1] == '2':
                s = s + q_y_i[2]
#                 print('p2')
        elif count % 2 == 0:
            if q_y_i[1] == '1':
                s = s + q_y_i[2]
#                 print('p3')
            elif q_y_i[1] == '2':
                s = q_y_i[2] + s
#                 print('p4')

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

Submission status: Time Limit Exceeded

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


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


print(s)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for i in range(Q):
    x = input()
    if x == '1':
        S = S[::-1]
    else:
        X = x.split()
        F = int(X[1])
        C = X[2]
        if F == 1:
            S.insert(0,C)
        if F == 2:
            S.append(C)
print(''.join(S))

Submission status: Time Limit Exceeded

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

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:
                s = s + row[2]
            else:
                s = row[2] + s
        else:
            if reverse:
                s = row[2] + s
            else:
                s = s + row[2]

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
st = S
for i in range(1, Q+1):
    query = input()
    if len(query) == 1:
        st = st[::-1]
    else:
        n, F, C = query.split()
        # n = int(n)
        # F = int(F)
        if F == '1':
            st = C + st
        else:
            st = st + C
print(st)

Submission status: Time Limit Exceeded

Solution code:
s = input()

N = int(input())

rev_flag = False

for _ in range(N):
  cmd = input()
  if cmd[0] == '1':
    rev_flag = True if rev_flag == False else False
    
  else:
    if (cmd[2] == '1' and rev_flag == False) or (cmd[2] == '2' and rev_flag == True):
      s = cmd[4:] + s
    else:
      s = s + cmd[4:]

if rev_flag == True:
  print(''.join(list(reversed(s))))
else:
  print(s)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

    r_flg = True

    for i in range(q):
        query = input()

        # query 1
        if query[0] == '1':
            r_flg = not r_flg
        
        # query 2
        else:
            # Fi = 1
            if query[2] == '1':
                if r_flg:
                    s = query[4] + s
                else:
                    s = s + query[4]
            # Fi = 2
            else:
                if r_flg:
                    s = s + query[4]
                else:
                    s = query[4] + s

        # print('cont:{}, s:{}, r_flg:{}'.format(i, s, r_flg))

    if not r_flg:
        s = ''.join(reversed(s))

    print(s)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q =int(input())
q =[]*Q


for h in range(Q):
  q.append(input().split())
  
  
for i in range(Q):
  if q[i][0] == "1":
    S = S[::-1]
    continue
  
  elif q[i][0] == "2":
    
    if q[i][1] == "1":#先頭に追加
      S = q[i][2] + S
      continue
    elif q[i][1] == "2":#末尾に追加
      S += q[i][2]
      continue
  
    
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
for _ in range(int(input())):
    query = input().split()
    if query[0] == '1':
        s = s[::-1]
    else:
        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:
S = input()
Q = int(input())
revcount = 0
 
ans = [S]
T = [0,0,""]
for i in range(Q):
  TS = input()
  if TS == "1":
    revcount += 1
  else:
    T = TS.rstrip().split(" ")
    if (int(T[1]) == 1 and revcount % 2 == 0) or (int(T[1]) == 2 and revcount % 2 == 1):
      ans.insert(0,T[2])
    else:
      ans.append(T[2])
ans2 = "".join(ans)
if revcount % 2 == 1:
  print(ans2[::-1])
else:
  print(ans2)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
iteration = int(input())
sousa_list = []
for i in range(0,iteration):
    sousa = input().split()
    sousa_list.append(sousa)

for c in range(0,iteration):
    if sousa_list[c][0] == "1":
        s = s[:: -1]
    else:
        if sousa_list[c][1] == "1":
            s = sousa_list[c][2] + s
        else:
            s = s + sousa_list[c][2]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

is_reversed = False

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

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

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

Ac = []
for i in range(Q):
    Ac.append(list(input().split()))

for i in range(Q):
    if Ac[i][0]=='1':
        S = ''.join(list(reversed(S)))

    else:
        if Ac[i][1]=='1':
            S= Ac[i][2]+S
        else:
            S= S+Ac[i][2]
print(S)

Submission status: Time Limit Exceeded

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

    rev = 1
    for i in range(q):
        que = sys.stdin.readline().split()
        if que[0] == "1":
            rev *= -1

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

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


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

main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
q = int(input())
query = [input().split() for i in range(q)]

d = deque(['S'])

#d = ['S']

rev = 0

for i in range(q):
    if query[i][0] == '2':
        if query[i][1] == '1':
            d.appendleft(query[i][2])
        else:
            d.append(query[i][2])
    else:
        d.reverse()
        rev += 1

ans = deque()

if rev % 2 == 1:
    s = ''.join(list(reversed(s)))


for i in range(len(d)):
    if d[i] == 'S':
        ans.append(s)
    else:
        ans.append(d[i])

print(*ans,sep='')

Submission status: Time Limit Exceeded

Solution code:

# -*- coding: utf-8 -*-
S = input()
Q = int(input())

rev = False

for q in range(Q):
    i = input().split()
    if i[0] == "1":
        rev = not rev
    elif not rev and i[1] == "1" or rev and i[1] == "2":
        S = ''.join([i[2], S])
    else:
        S = ''.join([S + i[2]])

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

Submission status: Time Limit Exceeded

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

turn=0
for _ in range(Q):
    q = str(input())
    if len(q)==1:
        turn = (turn+1)%2
    else:
        _, f,c = q.split()
        if turn == 0:
            if f =='1':
                s=c+s
            elif f =='2':
                s=s+c
        else:
            if f =='1':
                s=s+c
            elif f =='2':
                s=c+s
        # s.append(c)
    # print(s)
if turn == 0:        
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

for i in range(q):
    a = input()
    
    if a[0] == '1':
        rev += 1
    if a[0] == '2':
        if (int(a[2]) + rev) % 2 == 1:
            s = a[4] + s
        else:
            s += a[4]
    
if rev%2 == 0:
    print(s)
else:
    s = list(reversed(s))
    ans = "".join(s)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
Qry = [input().split() for i in range(Q)]
for q in Qry:
    if q[0] == '1':
        #S = [S[-1]] + S[1:-1] + [S[0]]
        S = S[::-1]
    else:
        if q[1] == '1':
            S = [q[2]] + S
        else:
            S += [q[2]]
print(*S, sep='')

Submission status: Time Limit Exceeded

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

rev = False

for i in range(Q):
    # クエリを受け取る
    query = input().split()
    # 反転の操作
    if query[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    else:
        # 追加する操作
        F = query[1]
        # 先頭に追加する場合
        if F == '1':
            # 反転しているとき
            if rev:
                # 末尾に追加
                S = S + query[2]
            # 反転していないとき
            else:
                # 先頭に追加
                S = query[2] + S
        # 末尾に追加する場合
        else:
            # 反転しているとき
            if rev:
                # 先頭に追加
                S = query[2] + S
            # 反転していないとき
            else:
                # 末尾に追加
                S = S + query[2]
if rev:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def main():
    S = input()
    S = deque([c for c in S])
    Q = int(input())
    rvCt = 0
    for _ in range(Q):
        query = input().split()
        if query[0] == '1':
            rvCt += 1
        else:
            if rvCt % 2 == 1:
                S.reverse()
            rvCt = 0
            if query[1] == '1':
                S.appendleft(query[2])
            else:
                S.append(query[2])

    if rvCt % 2 == 1:
        S.reverse()
    
    print(''.join(S))

main()

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
checker=-1#-1で正向き、1で反転
for _ in range(Q):
  B=input().split()
  if len(B)==3:
    T,F,C=B
  else:
    T=B[0]
    F,C="",""
    
  if T=="1":
    checker*=-1
  elif T=="2":
    if all([F=="1",checker==-1]):
      S=C+S
    elif all([F=="1",checker==1]):
      S=S+C
    elif all([F=="2",checker==-1]):
      S=S+C
    elif all([F=="2",checker==1]):
      S=C+S
print(S if checker==-1 else "".join(list(reversed(S))))

Submission status: Time Limit Exceeded

Solution code:
def main():
  s=input()
  q=int(input())
  count=0
  for _ in [0]*q:
    a=list(input().split())
    if len(a)==1:
      count+=1
    else:
      if count%2==0:
        if a[1]=='1':
          s=a[2]+s
        else:
          s=s+a[2]
      else:
        if a[1]=='1':
          s=s+a[2]
        else:
          s=a[2]+s
  if count%2==1:
    s=s[::-1]
  print(s)
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

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

f = 0

for _ in range(Q):
    q = input()
    if int(q[0]) == 1:
        f = abs(f-1)
        continue
    
    _, F, c = q.split()
    if f+int(F) % 2 == 1:
        s = c + s
    else:
        s = s + c
        
if f:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
S = raw_input()
Q = int(raw_input())
for _ in range(Q):
    q = raw_input()
    if q!='1':
        if q[2]=='1':
            S = q[4]+S
        else:
            S = S+q[4]
    else:
        S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

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

reverse = False

ans = S

for i in range(Q):
    query = input()
    if query == "1":
        reverse = not reverse

    else:
        a,b,c = list(query.split())
        if (b == "1" and not reverse) or (b == "2" and reverse):
            t = [c,ans]
            ans = ''.join(t)
        else:
            t = [ans,c]
            ans = ''.join(t)

if reverse:
    print(ans[::-1])
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import deque
s = tuple(input())
q = int(input())
a = []
for i in range(q):
    a.append([str(i) for i in input().split()])

for i in range(q):
    if a[i][0] == '1':
        #s.reverse()
        s = tuple(reversed(s))
    if a[i][0] == '2':
        s = deque(s)
        if a[i][1] == '1':
            s.appendleft(a[i][2])
        if a[i][1] == '2':
            s.append(a[i][2])
print(''.join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

s = input()
q = int(input())
ans = []
one_count = 0
for i in range(q):
    ans.append(list(input().split()))
for i in ans:
    if int(i[0])==1:
        one_count += 1
    else:
        if one_count%2==0:
            if int(i[1]) == 1:
                #ans_q.insert(0,i[2])
                s = i[2]+s
            else:
                s = s+i[2]
                #ans_q.insert(len(ans_q),i[2])
        else:
            if int(i[1]) == 1:
                #ans_q.insert(len(ans_q),i[2])
                s = s+i[2]
            else:
                #ans_q.2insert(0,i[2])
                s = i[2]+s
if one_count%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for i in range(Q):
  A = input().split()
  if A[0] == "1":
    S = S[::-1]
  else:
    if A[1] == "1":
      a = deque(S)
      a.appendleft(A[2])
      S = list(a)
    else:
      a = deque(S)
      a.append(A[2])
      S = list(a)
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
flag = 0b0
for i in range(q) :
    t = input()
    if len(t) == 5 :
        if (int(t[2]) - flag) == 1 :
            s = t[4] + s
        else :
            s = s + t[4]
    else :
        flag ^= 1
if flag == 0 :
    print(s)
else :
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
count = 1
while count < q+1:
    line = input()
    if "2" in line:
        t, f, c = line.split()
        if f == "1":
            s = c + s
        else:
            s = s + c
    else:
        s = s[::-1]
    count += 1
print(s)

Submission status: Time Limit Exceeded

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

#solve
ans = S
for i in range(Q):
  Query = input().split()
  if int(Query[0]) == 1:
    ans = ans[::-1]
  if int(Query[0]) == 2:
    if int(Query[1]) == 1:
      ans = Query[2] + ans
    if int(Query[1]) == 2:
      ans += Query[2]
print(ans)

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

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

st=False#順方向

for _ in range(Q):
    query=input()
    if len(query)==1:
        st= not st
    else:
        _,F,C=query.split()
        F=int(F)
        if st==False:
            if F==1:
                S=C+S
            else:
                S=S+C
        else:
            if F==1:
                S=S+C
            else:
                S=C+S

if st==False:
    ans=S
else:
    ans=S[::-1]
print(ans)

Submission status: Time Limit Exceeded

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

ans = S
hanten = 0
for l in L:
    if len(l) == 1:
        hanten += 1
    elif (l[1] == '1' and hanten % 2 == 0) or (l[1] == '2' and hanten % 2 == 1):
        ans = l[2] + ans
    else:
        ans = ans + l[2]
if hanten % 2 == 1:
    ans = ''.join(list(reversed(ans)))
print(ans)

Submission status: Time Limit Exceeded

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

a = input()

n = int(input())

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

print(str(a))

Submission status: Time Limit Exceeded

Solution code:
def ddd(s , q , ql):

    ans = s
    for i in range(q):
        if int(ql[i][0]) == 1:
            ans = ans[::-1]
        else:
            if int(ql[i][1]) == 1:
                ans = ql[i][2] + ans
            else:
                ans = ans + ql[i][2]

    return ans

def main():
    s = str(input())
    q = int(input())
    ql = [list(map(str , input().split())) for i in range(q)]
    print(ddd(s , q , ql))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#ABC158 D - String Formation

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

for i in range(Q):
    q = input().split(' ')
    if(q[0] == '1'):
        S = S[::-1]

    else:
        if(q[1] == '1'):
            #temp = [q[2]]
            S = q[2] + S
        else:
            S = S + q[2]

print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
  query = input().split()
  if query[0] == '2':
    if query[1] == '1':
      s = '{}{}'.format(query[2], s)
    else:
      s = '{}{}'.format(s, query[2])
  else:
    s = ''.join(reversed(s))
    
print(s)

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 = ''.join([s,S])
    else:
        S = ''.join([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(list(reversed(S)))
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()
r = 0
head = ''
tail = ''
for i in range(n):
    q = input().split()

    if(q[0] == '1'):
        # reverse
        r += 1
    else:
        if(q[1] == '1'):
            if(r%2 ==0):
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if(r%2 ==0):
                s = s + q[2]
            else:
                s = q[2] + s
    

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


print( s )

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
qs = [input().split(' ') for i in range(n)]

div = True
for q in qs:
    if q[0] == '1':
        div = not div
    else:
        if (q[1] == '1' and div) or (q[1] == '2' and not div):
            s = q[2] + s
        else:
            s = s + q[2]


if div:
    print(s)
else:
    s = ''.join(list(reversed(s)))
    print(s)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    ipt = input().split()
    if ipt[0] == "1":
        S = "".join(reversed(S))
    else:
        f = int(ipt[1])
        c = ipt[2]
        if f == 1:
            S = c + S
        else:
            S += c

print(S)

Submission status: Time Limit Exceeded

Solution code:
def Main(S, Q, queries):
    ans = list(S)
    flipped = False
    T1 = 0
    for q in queries:
        if int(q[0]) == 1:
            if not flipped:
                flipped = True
            else:
                flipped = False
            continue
        else:
            if int(q[1]) == 1:
                if flipped:
                    ans.append(q[2])
                else:
                    ans.insert(0, q[2])
            else:
                if flipped:
                    ans.insert(0, q[2])
                else:
                    ans.append(q[2])
    if flipped:
        ans.reverse()
    return "".join(ans)
        

def main():
    S = str(input())
    Q = int(input())
    queries = []
    for i in range(Q):
        queries.append(list(map(str, input().split())))
    print(Main(S, Q, queries))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
	S = input()
	Q = int(input())

	c = 1

	for i in range(Q):
		I = input()
		if len(I) == 1:
			c = c * (-1)
		else:
			T, F, C = I.split()
			f = int(F)
			if f == 1 and c == 1:
				S = C + S
			elif f == 1 and c == -1:
				S = S + C
			elif f == 2 and c == -1:
				S = C + S
			else:
				S = S + C

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

	print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
list_op = []
for i in range(0, Q):
    op = [x for x in input().split()]
    op[0] = int(op[0])
    if op[0] == 2:
        op[1] = int(op[1])
    list_op.append(op)

for op in list_op:
    if op[0] == 1:
        S = S[::-1]
    else:
        if op[1] == 1:
            S = op[2] + S
        else:
            S = S + op[2]

print(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 = S[::-1]
    elif q[1] == "1":
        S = q[2] + S
    else:
        S = S + q[2]
        
print(S)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    S = input()
    Q = int(input())
    Q_list = []
    tmp_before = ""

    for i in range(Q):
        tmp = input()
        if tmp_before == '1' and tmp == '1':
            Q_list.pop(-1)
            tmp_before = ""
            continue
        Q_list.append(tmp)
        tmp_before = tmp

    for q in Q_list:
        q_list = list(q.split())
        if q_list[0] == '1':
            S = S[::-1]
        else:
            F = q_list[1]
            C = q_list[2]
            if F == '1':
                S = C + S
            else:
                S = S + C

    print(S)


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = [input() for i in range(q)]

for i in range(q):
  temp = query[i].split()
  if int(temp[0]) == 1:
    s = ''.join(list(reversed(s)))
  else:
    if int(temp[1]) == 1:
      s = '{}{}'.format(temp[2], s)
    else:
      s = '{}{}'.format(s, temp[2])

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
# Q_lst = []
# for i in range(Q):
#     Q_lst.append(input().split())
# for i in range(len(Q_lst)):
#     if Q_lst[i] == ["1"]: #T == 1
#         S = S[::-1]
#     elif Q_lst[i][1] == "1":    #T == 2, F == 1
#         S = Q_lst[i][2] + S
#     else:   #F == 2
#         S = S + Q_lst[i][2]
reverse = 1
for i in range(Q):
    a = input().split()
    if a == ["1"]:
        reverse *= -1
    elif a[1] == "1":
        if reverse == 1:
            S = a[2] + S
        else:
            S = S + a[2]
    else:
        if reverse == 1:
            S = S + a[2]
        else:
            S = a[2] + S
if reverse == -1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    T = list(input().split())
    if len(T) == 1:
        S = S[::-1]
    else:
        if T[1] == '1':
            # 先頭に追加
            S = "{0}{1}".format(T[2], S)
        else:
            # 末尾に追加
            S = "{0}{1}".format(S, T[2])
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = list(map(int,input().split()))[0]

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

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
n=int(input())
for i in range(n):
    l=list(map(str,input().split()))
    if int(l[0])==1:
        s=list(reversed(s))
    elif int(l[0])==2:
        if int(l[1])==1:
            s.insert(0, l[2])
        if int(l[1])==2:
            s.append(l[2])

print("".join(s))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
  s = input().split()
  if s[0] == "1": # 反転
    S = S[::-1]
  elif s[0] == "2":
    if s[1] == "1": # 先頭
      S = s[2] + S
    elif s[1] == "2": # 末尾
      S = S + s[2]
print(S)