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()
N = int(input())
Q = [input().split() for i in range(N)]

for i in range(N):
  if int(Q[i][0]) == 1:
    S = S[::-1]
  else:
    if int(Q[i][1]) == 1:
      S = Q[i][2] + S
    else:
      S = S + Q[i][2]

print(S)

Submission status: Time Limit Exceeded

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

def main():
    s = input()[:-1]
    q = int(input())
    tmp = [input().split() for _ in range(q)]

    for i in range(len(tmp)):
        if len(tmp[i]) == 1:
            s = s[::-1]
        else:
            if int(tmp[i][1]) == 1:
                s = ''.join([tmp[i][2], s])
            else:
                s = ''.join([s, tmp[i][2]])
                
    print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
a = sys.stdin.read().split('\n')[:-1]
s = list(a[0])
q = int(a[1])
f = 1
for i in range(2, q + 2):
    if a[i][0] == '1':
        f = 1 - f
    else:
        l = len(s)
        s.insert(0 if a[i][2] == '21'[f] else l, a[i][4])
if f != 1:
    s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s = input()

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

Submission status: Time Limit Exceeded

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

for _ in range(q):
    try:
        t, f, c = input().split()
        if int(f) == 1:
            s.insert(0, c)
        else:
            s.append(c)
    except ValueError:
        s = s[::-1]

print(*s, sep='')

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
A = deque(input())
B = int(input())
for i in range(B):
    C = sys.stdin.readline().split()
    if C[0] == "1":
        A.reverse()
    else:
        if C[1] == "1":
            A.appendleft(C[2])
        else:
            A.append(C[2])
print("".join(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    from sys import stdin
    from collections import deque
    S = input()
    Q = int(input())
    reverse = 0
    head = []
    tail = []
    head_deque = deque(head)
    tail_deque = deque(tail)
    
    for _ in [0] * Q:
        readline = stdin.readline
        s = readline().split()
        if int(s[0]) == 1:
            if reverse == 1:
                reverse = 0
            else:
                reverse = 1
        else:
            if int(s[1]) == 1:
                if reverse == 1:
                    tail.append(s[2])
                else:
                    head_deque.appendleft(s[2])
                    head.insert(0, s[2])
            else:
                if reverse == 1:
                    head_deque.appendleft(s[2])
                else:
                    tail.append(s[2])
    head = list(head_deque)
    head = ''.join(head)
    tail = ''.join(tail)
    if reverse == 1:
        print(tail[::-1]+S[::-1]+head[::-1])
    else:
        print(head+S+tail)
        
main()

Submission status: Time Limit Exceeded

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

from collections import deque
s = deque(s)

for qi in q:
    if qi[0] == "1":
        s.reverse()
    if len(qi) != 1:
        if qi[1] == '1':
            s.appendleft(qi[2])
        elif qi[1] == "2":
            s.append(qi[2])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
import sys
s = sys.stdin.readline().rstrip()
q = int(sys.stdin.readline())

pre = ''
suf = ''
count = 0
for i in range(q):
    ts = sys.stdin.readline().rstrip().split()
    if ts[0] == '1':
        count += 1
    else:
        t = int(ts[0])
        f = int(ts[1])
        c = ts[2]
        if f == 1:
            strs = []
            if count % 2 == 0:                
                strs = [c, pre]
                pre = ''.join(strs)
            else:
                strs = [suf, c]
                suf = ''.join(strs)
        else:
            if count % 2 == 0:
                strs = [suf, c]
                suf = ''.join(strs)
            else:
                strs = [c, pre]
                pre = ''.join(strs)
        #print(count)
        #print('pre:' + pre)
        #print('suf:' + suf)
        

strs  = []
if count % 2 == 0:
    strs = [pre, s, suf]
else:
    strs = [suf[::-1], s[::-1], pre[::-1]]

print (''.join(strs))

Submission status: Time Limit Exceeded

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

S = input()
Q = int(input())
meirei = []
for _ in range(Q):
    inp = input().split()
    meirei.append(inp)

seiri_meirei = []
i = 0
while i < Q:
    if meirei[i][0] == "1":
        if i<Q-1 and meirei[i+1] == "1":
            i+=2
        else:
            seiri_meirei.append(meirei[i])
            i+=1
    else:
        seiri_meirei.append(meirei[i])
        i+=1

for j in seiri_meirei:
    if j[0] == "2":
        if j[1] == "1":
            S = j[2] + S
        else:
            S = S + j[2]
    else:
        S=hantai(S)
print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
ope1=None
for i in range(q):
    query = input().split()
    if query[0] == '1' == ope1:
        ope1=None
        continue
    elif query[0] == '1' and ope1 is None:
        ope1='1'
        continue
    elif query[0] == '2':
        if ope1:
            s = s[::-1]
            ope1=None
        s = ''.join([query[2],s]) if query[1]=='1' else ''.join([s,query[2]])
if ope1:
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

bit = 2
for i in range(0, Q):
  inp = input().split(" ")
  
  if inp[0] == "1":
    bit += 1
  if inp[0] == "2":
    if inp[1] == "1" and bit % 2 == 1:
      S += inp[2]
    elif inp[1] == "1" and bit % 2 == 0:
      a = inp[2]
      a += S
      S = a
    elif inp[1] == "2" and bit % 2 == 1:
      a = inp[2]
      a += S      
      S = a
    elif inp[1] == "2" and bit % 2 == 0:
      S += inp[2]
      
if bit % 2 == 1:
  print(S[::-1])
else:
  print(S)

Submission status: Time Limit Exceeded

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

S = input().rstrip()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]

ans = S
flag = True

for j in range(10**5):
    for i in range(Q):
        if query[i][0] == '1':
            if flag:
                flag = False
            else:
                flag = True
            continue
        elif query[i][0] == '2':
            f = query[i][1]
            c = query[i][2].rstrip()
            if f == '1' and flag:
                ans = c + ans
            elif f == '1' and not flag:
                ans = ans + c
            elif f == '2' and flag:
                ans = ans + c
            elif f == '2' and not flag:
                ans = c + ans
print(ans)

Submission status: Time Limit Exceeded

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

for j in range(q):
    i = input().split()
    if i[0] == '1':
        s = ''.join(list(reversed(s)))
    if i[0] == '2':
        if i[1] == '1':
            s = ''.join([i[2],s])
        else:
            s = ''.join([s,i[2]])

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
s = input()
num = int(input())
for i in range(num):
  a = list(input().split())
  start = int(a[0])
  if start == 2:
    ns = int(a[1])
    if ns == 1:
      s = a[2] + s
    else:
      s = s + a[2]
  else:
    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]
  elif query[2] == '1':
    s = query[4] + s
  else:
    s  = s + query[4]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = [list(map(str,input().split())) for i in range(q)]
mae = ""
ushiro = ""
for i in range(q):
    q = query[i]
    if q[0] == "1" :
        mae, ushiro  = ushiro, mae
        mae = mae[::-1]
        ushiro = ushiro[::-1]
        s = s[::-1]
        continue
    else:
        if q[1] == "1":
            mae = q[2] +mae 
        else:
            ushiro = ushiro + q[2]
print(mae + s  + ushiro)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
N=int(input())
Q=[input() for i in range(N)]
T=deque(S)
for i in range(N):
    if Q[i][0]=="2":
        if Q[i][2]=="1":
            T.appendleft(Q[i][4])
            S=list(T)
        if Q[i][2]=="2":
            T.append(Q[i][4])
            S=list(T)
    if Q[i][0]=="1":
        S=list(T)
        S=S[::-1]
        T=deque(S)
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
#文字列
s=list(input())
n=int(input())
for i in range(n):
    li = input().split()
    if(li[0]=="1"):
        s.reverse()
    else:
        if(li[1]=="1"):
            s.insert(0, li[2])
        else:
            s.append(li[2])

print("".join(s))

Submission status: Time Limit Exceeded

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

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        s.reverse()
    else:
        if t[1] == "1":
            s.insert(0,t[2])
        else:
            s.append(t[2])

print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
# input1 = list(map(int, (input().split())))

S = input()
N = int(input())
c = False
for i in range(N):
    Q = list(input().split())
    a = int(Q[0])
    if a == 1:
        c = not c
    elif a == 2:
        if c == True:
            S = S[::-1]
            c = False
        b = int(Q[1])
        if b == 1:
            S = Q[2] + S
        elif b == 2:
            S = S + Q[2]
if c == True:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

d = deque(input())
Q = int(input())


for _ in range(Q):
    i = input()
    if len(i) >= 2:
        i, F, C = i.split()
    if int(i) == 1:
        d.reverse()
        continue
    else:
        if int(F) == 1:
            d.appendleft(C)
            continue
        d.append(C)

print(*d,sep='')

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
S = list(input())
S.pop(-1)
n = int(input())
s = []
for i in range(n):
    array = list(map(str,input().split()))
    s.append(array)
for i in range(n):
    if s[i][0] == "1":
        S.reverse()
    else:
        if s[i][1] == "1":
            S.insert(0,s[i][2])
        else:
            S.append(s[i][2])
print("".join(S))

Submission status: Time Limit Exceeded

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

q_list = []

rev_c = 0
count = 0

for _ in range(q):
  v = input()
  q_list.append(v)
  if v == '1':
    rev_c += 1

if rev_c % 2 == 1:
  s = s[::-1]
  
for v in q_list:
  if v == '1':
    count += 1
  else:
    num = int(v[2])
    char = v[4]
    if (rev_c-count+num)%2==1:
      s = ''.join([char,s])
    else:
      s = ''.join([s,char])
print(s)

Submission status: Time Limit Exceeded

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

Qu =[]
for i in range(Q):
    Qu.append(list(map(str,input().split())))

for i in range(Q):
    if Qu[i][0] == '1':
        S = S[::-1]
        #S.reverse()
    elif Qu[i][0] == '2':
        if Qu[i][1] == '1':
            S = Qu[i][2] + S
        elif Qu[i][1] == '2':
            S = S + Qu[i][2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
flag = True
for _ in range(n):
    q = list(map(str, input().split()))
    if len(q) == 1:
        if flag:
            flag = False
        else:
            flag = True
    if len(q) == 3:
        if q[1] == "1":
            if flag:
                s = list(q[2])+s
            else:
                s = s+list(q[2])
        else:
            if flag:
                s = s+list(q[2])
            else:
                s = list(q[2])+s
if flag == False:
    s.reverse()
print("".join(s))

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']
d = ['S']
rev = 0

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


if rev % 2 == 1:
    s = ''.join(list(reversed(s)))
"""
while len(d):
    tmp = d.popleft()
    if tmp == 'S':
        ans.append(s)
    else:
        ans.append(tmp)
"""
d = [s if i == 'S' else i for i in d]

print(*d,sep='')

Submission status: Time Limit Exceeded

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

def main():
    s = input()
    q = int(input())
    list_ = []
    for _ in range(q):
        list_.append(list(map(str, input().split())))

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

    print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

S = input()
Q = int(input())
X = []
for i in range(Q):
    tmp = LIST()
    X.append(tmp)

for x in X:
    if x[0] == '1':
        S = ''.join(list(reversed(S)))
    elif x[0] == '2':
        if x[1] == '1':
            S = str(x[2]) + S
        elif x[1] == '2':
            S = S + str(x[2])
print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
re=False
for i in range(0,q):
    Q=input().split(' ')
    if int(Q[0])==1:
        re=not re
        continue
    if int(Q[0])==2:
        if (int(Q[1])==1 and (not re)) or (int(Q[1])==2 and re):
            s=Q[2]+s
            continue
        if (int(Q[1])==1 and re) or (int(Q[1])==2 and (not re)):
            s=s+Q[2]
if re==True:
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def solve():
    S = list(input())
    M = int(input())

    s = deque(S)
    
    for _ in range(M):
        q = list(input().split())
    
        if q[0] == '1':
            s.reverse()
        else:
            ad = q[2]
            if q[1] == '1':
                s.appendleft(ad)
            else:
                s.append(ad)
    ans = ''.join(s)
    return ans

print(solve())

Submission status: Time Limit Exceeded

Solution code:
S=input()
n=int(input())
l = [list(input()) for _ in range(n)]
z=1
for i in range(n):
    if l[i][0]=='1':
        #new_S = S[::-1]
        #S=new_S
        z=z*(-1)
    else:
        a=l[i][4]
        if l[i][2]=='1'and z==1:
            S='' . join([a,S])
        elif l[i][2]=='2'and z==1:
            S='' . join([S,a])
        elif  l[i][2]=='1'and z==-1:
            S='' . join([S,a])
        else:
            S='' . join([a,S])
if z==1:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
def main(): 
    s=list(input())
    q=int(input())
    query = [input().split() for i in range(q)]

    for query_list in query:
        if query_list[0] == '1':
            s = s[::-1]
        else:
            if query_list[1] == '1':
                s.insert(0, query_list[2])
            else:
                s.append(query_list[2])

    print(''.join(s))

main()

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for i in range(Q):
    T = list(input().split())
    if len(T) > 1:
        if T[1] == '1':
            # 先頭に追加
            S.insert(0, T[2])
        else:
            # 末尾に追加
            S.append(T[2])
    else:
        S.reverse()

print(''.join(S))

Submission status: Time Limit Exceeded

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

from collections import deque
s = deque(s)

for qi in q:
    if qi[0] == "1":
        s.reverse()
    if len(qi) != 1:
        if qi[1] == '1':
            s.appendleft(qi[2])
        elif qi[1] == "2":
            s.append(qi[2])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
import sys
 
 
S = input()
Q = int(input())
h = 0
 
for _ in range(Q):
    A = sys.stdin.readline()
    if A[0] == '1' and len(S)!=1:
        h = h + 1
    else:
        if h % 2 == 0:
            if A[2] == '1':
                S = A[4]+S
            else:
                S = S+A[4]
        else:
            if A[2] == '1':
                S = S+A[4]
            else:
                S = A[4]+S
 
if h % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
# queries = []
# for _ in range(Q):
#   q = list(input().split()) # 文字列のリスト
#   queries.append(q)

r_count = 0
# for query in queries:
for query in range(Q):
  query = list(input().split())
  t = query[0]
  if (t == '1'):
    r_count += 1
  elif (t == '2'):
    f = query[1]
    new_S = ""
    if(f == '1' and (r_count % 2 == 0)):
      new_S = query[2] + S
    elif(f == '1' and (r_count % 2 == 1)):
      new_S = S + query[2]
    elif(f == '2' and (r_count % 2 == 0)):
      new_S = S + query[2]
    elif(f == '2' and (r_count % 2 == 1)):
      new_S = query[2] + S
    S = new_S

if (r_count % 2 == 1):
  for i, s in enumerate(reversed(S)):
    if (i == len(S) - 1):
      print(s)
    else:
      print(s, end="")
else:
  print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
counter=0
for i in range(Q):
    query=input().split()

    if "1"==query[0]:

        counter+=1
    else:
        if counter>0:
            if counter%2==1:
                S=S[::-1]
            counter=0
        if query[1]=="1":
            S=query[2]+S


        else:
            S+=query[2]


if counter>0:
    if counter%2==1:
        S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

cnt = 0

for i in range(Q):
    if len(L[i]) == 1:
        cnt += 1
    else:
        if (int(L[i][1])+cnt)%2 == 0:
            S = S+L[i][2]
        else:
            S = L[i][2]+S

if cnt%2 == 0:
    print(S)
else:
    new_S = ''.join(list(reversed(S)))
    print(new_S)

Submission status: Time Limit Exceeded

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

for i in range(n):
    a = input()
    if a == "1":
        f += 1
    else:
        b,c,d = a.split()
        if f%2 == 0:
            if c == "1":
                s = d + s
            else:
                s = s + d
        else:
            if c == "1":
                s = s + d
            else:
                s = d + s

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

Submission status: Time Limit Exceeded

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

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



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

  if query == '1':
    now_reverse = not now_reverse
    continue

  
  _,f,c = query.split()
  if f == '1' and now_reverse:
    s = "".join([s,c])
  elif f == '1' and not now_reverse:
    s = "".join([c,s])
  elif f == '2' and now_reverse:
    s = "".join([c,s])
  else:
    s = "".join([s,c])
  
if now_reverse:
  s = reversed(s)

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

Submission status: Time Limit Exceeded

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

hanten = False
for i in range(q):
    query = input().split()
    if query[0] == '1':
        hanten = not hanten
    else:
        if query[1] == '1':
            if hanten:
                s = s + query[2]
            else:
                s = query[2] + s
        else:
            if hanten:
                s = query[2] + s
            else:
                s = s + query[2]

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

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
q=int(input())
for i in range(q):
    x=list(input())
    if len(x)==1:
        s=list(reversed(s))
    else:
        if x[2]=='1':
            s.insert(0,x[4])
        else:
            s.append(x[4])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

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

# Queries = []
# for _ in range(Q):
#     Queries.append(list(input().split()))
    
# for Query in Queries:
#     T = int(Query[0])
#     if T == 1:
#         S = S[::-1]
#     else:
#         F = int(Query[1])
#         C = Query[2]
#         if F == 1:
#             S = C + S
#         else:
#             S = S + C
# print(S)

swap = 1
head = ""
tail = ""
for _ in range(Q):
    Query = list(input().split())

    if Query[0] == "1":
        swap *= -1
    
    else:
        F, C = Query[1], Query[2]
        if F == "1":
            if swap == 1:
                head = C + head
            else:
                tail = tail + C
        else:
            if swap == -1:
                head = C + head
            else:
                tail = tail + C            

print((head + S + tail)[::swap])

Submission status: Time Limit Exceeded

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

left = ""
right = ""

for i in range(Q):
  s = T[i]
  if(int(s[0]) == 2):
    if(int(s[2]) == 1):
      left = s[4] + left
    else:
      right = right + s[4]
  else:
    S = left + S + right
    S = S[::-1]
    left = ""
    right = ""
S = left + S + right

print(S)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

for i in range(Q):
    query.append(input().split())
# for i in query:
#     print(i)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(0, q):
    query = input()
    if query[0] == '1':
        s = s[::-1]
    elif query[2] == '1':
        s = ''.join([query[4],s])
    else:
        s = ''.join([s,query[4]])
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
Query = []
cnt_re = 0
for i in range(Q):
  tmp = input().split()
  Query.append(tmp)
  if (len(tmp) == 1):
    cnt_re += 1
prev = ''
after = ''
if (cnt_re % 2 != 0):
  S = list(reversed(S))
for q in Query:
  if (len(q) == 3):
    if q[1] == '1':
      if (cnt_re % 2 == 0):
        prev = q[2] + prev
      else:
        after = after + q[2]
    else:
      if (cnt_re % 2 == 0):
        after = after + q[2]
      else:
        prev = q[2] + prev
  else:
    cnt_re -= 1
print(prev + ''.join(list(S)) + after)

Submission status: Time Limit Exceeded

Solution code:

S=list(str((input())))
Q=int(input())
t=0
for _ in range(Q):
    T=list(map(str,input().split()))
    if T[0]=="1":
        t+=1
    else:
        if t%2 != 0:
            S.reverse()
            t=0
        if T[1]=="1":
            S.insert(0,T[2])
        else:
            S.append(T[2])


if t%2 != 0:
    S.reverse()

print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

for i in range(q):
    uery=list(map(str,input().split()))
    
    if len(uery)==1:
        s.reverse()
        
    elif len(uery)>1:
        if uery[1]=="1":
            s.insert(0,uery[2])
            
        elif uery[1]=="2":
            s.append(uery[2])

print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
letters = list(input())
n = int(input())
for i in range(0,n):
    command = list(input().split())
    if command[0] == "1":
        letters.reverse()
    
    else:
        if command[1] == "2":
            letters.append(command[2])
        else:
            letters.insert(0, command[2])


ans = "".join(letters)
print(ans)

Submission status: Time Limit Exceeded

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

#list = [input().split() for l in range(q)]
for i in range(q):
    list = input().strip().split()
    if(list[0]=='1'):
        hanten = not hanten
    elif((list[1]=='1' and not hanten) or (list[1]=='2' and hanten)):
        s = list[2] + s
    else:
        s = s + list[2]

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
  S = input()
  Q = int(input())
  query = []
  for _ in range(Q):
    query.append(input().split())
 
  ans = S
  for i in range(Q):
    if len(query[i])==1:
      ans = ans[::-1]
    else:
      if(query[i][1]=="1"):
        t = [query[i][2],ans]
        #ans = ''.join(query[i][2])
        ans = "".join(t)
      else:
        t = [ans,query[i][2]]
        #ans = ''.join(query[i][2])
        ans = "".join(t)
  print(ans)
  
if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
for i in range(int(input())):
    query = input()
    if query[0] == "1":
        s = s[::-1]
        continue

    if query[2] == "1":
        s = query[-1] + s
        continue
    s += query[-1]

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

Submission status: Time Limit Exceeded

Solution code:
##D
S = str(input())
Q = int(input())
Query = [str(input()) for i in range(Q)]
##print(Query)
ans = S
for j in range(len(Query)):
    if Query[j][0] == "1":
        ans = ans[::-1]
    elif Query[j][0] == "2":
        if Query[j][2] == "1":
            ans = str(Query[j][4])+ans
        elif Query[j][2] == "2":
            ans = ans+ str(Query[j][4])
print(ans)