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

for i in range(Q):
  q = input()
  if q == "1":
    S.reverse()
  elif q[2] == "1":
    S.insert(0,q[4])
  else:
    S.append(q[4])

S = "".join(S)
print(S)

Submission status: Time Limit Exceeded

Solution code:
def main():
  S=input()
  N=int(input())
  cnt=0
  for i in range(N):
    tmp=[i for i in input().split() ]
    if tmp[0]=='1':
      cnt+=1
    else:
      if (cnt+int(tmp[1]))%2==1:
        new=tmp[2]+S
        S=new
      else:
        new=S+tmp[2]
        S=new
  if cnt%2!=0:
    S=''.join(list(reversed(S)))

  print(S)
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

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

for i in range(Q):
  q = list(input().split())
  if int(q[0]) == 1:
    R = not R
  else:
    if int(q[1]) == 1:
      if R:
        S.insert(0, q[2])
        
      else:
        S.append(q[2])
    else:
      if R:
        S.append(q[2])
      else:
        S.insert(0, q[2])

if R:
  print(''.join(S))
else:
  print(''.join(S[::-1]))

Submission status: Time Limit Exceeded

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

S = input().rstrip()
Q = int(input())
head = ''
tail = ''
rev = False

for _ in range(Q):
    query = list(input().split())
    if query[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    elif query[0] == '2':
        f = query[1]
        c = query[2].rstrip()
        if f == '1':
            if rev:
                tail = tail + c
            else:
                head = c + head
        else:
            if rev:
                head = c + head
            else:
                tail = tail + c
if rev:
    tmp = head + S + tail
    print(tmp[::-1])
else:
    print(head + S + tail)

Submission status: Time Limit Exceeded

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

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

reverse = False

before = ""
after = ""

for _ in range(Q):
    s = input().strip()
    if s[0] == "1":
        reverse = not reverse
    else:
        _, f, c = s.split()
        if f == "1":
            if reverse:
                after = after + c
            else:
                before = c + before
        else:
            if reverse:
                before = c + before
            else:
                after = after + c

if reverse:
    print(after[::-1] + S[::-1] + before[::-1])
else:
    print(before + S + after)

Submission status: Time Limit Exceeded

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

r = 1
for i in range(1,Q+1):
    T = input()
    if len(T) == 1:
        r = -r
    else:
        if (T[2] == '1' and r == 1) or (T[2] == '2' and r == -1) :
            S = T[4] + S
        elif (T[2] == '2' and r == 1) or (T[2] == '1' and r == -1):
            S = S + T[4]
if r == -1:
    L = ''
    for j in range(len(S)):
        L += S[-j-1]
    S = L
print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
order=1
for _ in range(q):
  tmp=input()
  if tmp=='1':
    order*=-1
  else:
    t, f, c=tmp.split()
    if (int(f)*order)%3==1:
      s=c+s
    else:
      s=s+c
print(s) if order==1 else print(s[::-1])

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

cnt_reverse = False
for i in range(q):
  query = input()
  
  if query[0] == '1':
    cnt_reverse = not cnt_reverse
  elif (query[2] == '1' and not cnt_reverse) or (query[2] == '2' and cnt_reverse):
    s = query[4] + s
  else:
    s  = s + query[4]
if cnt_reverse:
  s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

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

querys = []
op1_cnt = 0
op2_cnt = 0
prefix = deque()
suffix = deque()
for i in range(Q):
    query = input()
    if len(query) == 1:
        op1_cnt += 1

        if op2_cnt != 0:
            prefix.extend(S)
            prefix.extend(suffix)
            S = ''.join(prefix)
            prefix.clear()
            suffix.clear()
            op2_cnt = 0

    else:
        op2_cnt += 1

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

        op1_cnt = 0

        t, F, C = query.split()
        if F == "1":
            prefix.appendleft(C)
        else:
            suffix.append(C)

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

if op2_cnt != 0:
    prefix.extend(S)
    prefix.extend(suffix)
    S = ''.join(prefix)


print(S)

Submission status: Time Limit Exceeded

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

inv = False
mae = ''
usiro = ''

for _ in range(q):
    tmp = list(input().split())
    if tmp == ['1']:
        inv = not inv

    elif tmp[1] == '1' and not inv:
        mae = tmp[2] + mae
    elif tmp[1] == '2' and not inv:
        usiro = usiro + tmp[2]
    elif tmp[1] == '1' and inv:
        usiro = usiro + tmp[2]
    elif tmp[1] == '2' and inv:
        mae = tmp[2] + mae

    # print(mae,s,usiro)

ans = mae + s + usiro

if inv:
    ans = ans[::-1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
hanten = 0
check = 0
for i in range(q):
    l = list(map(str, input().split()))
    if l[0] == "1":
        hanten += 1
    else:
        if l[1] == "1":
            if hanten%2 == 0:
                s = l[2] + s
            else:
                s = s + l[2]
        else:
            if hanten%2 == 0:
                s = s + l[2]
            else:
                s = l[2] + s
 
if hanten%2 == 1:
    s = s[::-1]
 
print(s)

Submission status: Time Limit Exceeded

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

inv = False
mae = ''
usiro = ''

for _ in range(q):
    tmp = list(input().split())
    if tmp == ['1']:
        inv = not inv

    elif tmp[1] == '1' and not inv:
        mae = tmp[2] + mae
    elif tmp[1] == '2' and not inv:
        usiro = usiro + tmp[2]
    elif tmp[1] == '1' and inv:
        usiro = usiro + tmp[2]
    elif tmp[1] == '2' and inv:
        mae = tmp[2] + mae

    # print(mae,s,usiro)

ans = ''.join(mae) + s + ''.join(usiro)

if inv:
    ans = ans[::-1]

print(ans)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
  T = list(input().split())

  if T[0] == '1':
    reverse_flag = not reverse_flag

  else:
    if (T[1] == '1' and reverse_flag) or (T[1] != '1' and (not reverse_flag)):
      S = T[2]+S

    else:
      S = S+T[2]

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
x = 1
l=[]
for i in range(Q):
    l=[]
    l = list(input().split())
    if l[0] == "1":
        x *= -1
    elif l[0] == "2":
        if (l[1] == "2" and x == -1) or  (l[1] == "1" and x == 1):
            S.insert(0, l[2])
        else:
            S.append(l[2])
if x == -1:
    S = S[::-1]
print(*S, sep="")

Submission status: Time Limit Exceeded

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

r = 0
sort = 0
for i in range(q):
  List=list(input().split())
  if len(List) > 1 :
    if List[1] == '1' and r == 0 :
      s = List[2] + s 
    elif List[1] == '1' and r == 1 :
      s = s + List[2] 
    elif List[1] == '2' and r == 0 :
      s = s + List[2] 
    elif List[1] == '2' and r == 1 :
      s = List[2] + s 
  else:  
    if r == 1 :
      r=0
      sort += 1
    elif len(s) > 1 and r == 0:
      r=1
      sort += 1

ans = ''
if r ==1:
  for j in range(len(s)):
    ans += s[len(s)-j-1:len(s)-j]
else:
  ans = s

print(ans)

Submission status: Time Limit Exceeded

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


inv = False
mae = ''
usiro = ''

for _ in range(q):
    tmp = list(input().split())
    if tmp == ['1']:
        inv = not inv

    elif tmp[1] == '1' and not inv:
        mae = tmp[2] + mae
    elif tmp[1] == '2' and not inv:
        usiro = usiro + tmp[2]
    elif tmp[1] == '1' and inv:
        usiro = usiro + tmp[2]
    elif tmp[1] == '2' and inv:
        mae = tmp[2] + mae

    # print(mae,s,usiro)

ans = ''.join(mae) + s + ''.join(usiro)

if inv:
    ans = ans[::-1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
order=1
for _ in range(q):
  tmp=input()
  if tmp=='1':
    order*=-1
  else:
    tmp=list(tmp.split())
    if (tmp[1]=='1' and order==1) or (tmp[1]=='2' and order==-1):
      s=tmp[2]+s
    else:
      s=s+tmp[2]
print(s) if order==1 else print(s[::-1])

Submission status: Time Limit Exceeded

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

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

print("".join(S))

Submission status: Time Limit Exceeded

Solution code:

import sys; sys.setrecursionlimit(2147483647); input = sys.stdin.readline
from math import floor, ceil, sqrt, factorial, log
from collections import Counter, defaultdict
from operator import itemgetter
INF = float('inf'); MOD = 10**9+7
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(MI())
def LIR(n): return [LI() for i in range(n)]
def S(): return input().rstrip()
def LIR(n): return [S().split() for i in range(n)]

def main():
    s = S()
    Q = I()
    Qu = LIR(Q)
    mode = 0; left = ''; right =  ''
    for que in Qu:
        if que[0] == '1': mode = (mode+1)%2
        else:
            if mode:
                if que[1] == '1': s = ''.join([s, que[2]])
                else: s = ''.join([que[2], s])
            else:
                if que[1] == '1': s = ''.join([que[2], s])
                else: s = ''.join([s, que[2]])
    if mode: print(s[::-1])
    else: print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

head = []
div = True
for i in range(n):
    q = input().split(' ')
    if q[0] == '1':
        div = not div
    elif (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 = s[::-1]
    print(s)

Submission status: Time Limit Exceeded

Solution code:
"""
D - String Formation
"""
s = input()
q = int(input())
string = []
is_normal = 1
for i in range(q):
    emir = input()
    if emir[0] == '1':
        is_normal *= -1
    elif emir[0] == '2':
        if is_normal == 1:
            if emir[2] == '1':
                s = emir[4] + s
            elif emir[2] == '2':
                s += emir[4]
        else:
            if emir[2] == '1':
                s += emir[4]
            elif emir[2] == '2':
                s = emir[4] + s
if is_normal == -1:
    s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

query = []

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


left = True
for i in query:
    if len(i) == 1:
        left = not left

    else:
        if i[1]=="1":
            if left:
                S = i[2]+S
            else:
                S += i[2]
        else:
            if left:
                S += i[2]
            else:
                S = i[2]+S

print(S) if left else print(S[::-1])

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)
left = True
for i in query:
    if len(i) == 1:
        left = not left
        # print(left)
    else:
        if i[1]=="1":
            if left:
                S = i[2]+S
            else:
                S += i[2]
        else:
            if left:
                S += i[2]
            else:
                S = i[2]+S

print(S) if left else print(S[::-1])

Submission status: Time Limit Exceeded

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

query = [input() for _ in range(q)]

right = ""
left = ""
for qu in query:
    if len(qu.split()) == 1:
        right, left = left, right
        s = s[::-1]
        continue
    num, f, c = qu.split()
    f = int(f)

    if f == 1:
        left += c
        
    else:
        right += c


print(left[::-1] + s + right)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
querry = [list(map(str, input().split())) for i in range(Q)]
for i in range(Q):
    if querry[i][0] == '1':
        S.reverse()
    elif querry[i][0] == '2':
        if querry[i][1] == '1':
            S.appendleft(querry[i][2])
        elif querry[i][1] == '2':
            S.append(querry[i][2])

ans=list(S)

print("".join(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys; sys.setrecursionlimit(2147483647); input = sys.stdin.readline
from math import floor, ceil, sqrt, factorial, log
from collections import Counter, defaultdict
from operator import itemgetter
INF = float('inf'); MOD = 10**9+7
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(MI())
def LIR(n): return [LI() for i in range(n)]
def S(): return input().rstrip()
def LIR(n): return [S().split() for i in range(n)]

def main():
    s = S()
    Q = I()
    Qu = LIR(Q)
    mode = 0
    for que in Qu:
        if que[0] == '1':
            mode += 1
        else:
            if mode%2:
                if que[1] == '1': s = ''.join([s, que[2]])
                else: s = ''.join([que[2], s])
            else:
                if que[1] == '1': s = ''.join([que[2], s])
                else: s = ''.join([s, que[2]])
    if mode%2: print(s[::-1])
    else: print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


inv = False
mae = []
usiro = []

for _ in range(q):
    tmp = list(input().split())
    if tmp == ['1']:
        inv = not inv

    elif tmp[1] == '1' and not inv:
        mae.insert(0,tmp[2])
    elif tmp[1] == '2' and not inv:
        usiro.append(tmp[2])
    elif tmp[1] == '1' and inv:
        usiro.append(tmp[2])
    elif tmp[1] == '2' and inv:
        mae.insert(0,tmp[2])

    # print(mae,s,usiro)

ans = ''.join(mae) + s + ''.join(usiro)

if inv:
    ans = ans[::-1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def main():
    s = str(input())
    q = int(input())
    s = s.strip()
    a = [''] * (10**5*2)
    a.extend(list(s))
    a.extend([''] * (10**5*2))
    start = 10**5*2
    end = start + len(s) - 1
    for i in range(q):
        query = input().split()
        if query[0] == '1':
            a.reverse()
            tmp = end
            end = len(a)-start-1
            start = len(a)-tmp-1
        elif query[0] == '2':
            if query[1] == '1':
                a[start-1] = query[2]
                start -= 1
            elif query[1] == '2':
                a[end+1] = query[2]
                end += 1
    print(''.join(a[start:end+1]))


main()

Submission status: Time Limit Exceeded

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

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

querys = []
op1_cnt = 0
op2_cnt = 0
op2_C = {"1": [], "2": []}
for i in range(Q):
    query = input()
    if len(query) == 1:
        op1_cnt += 1

        if op2_cnt != 0:
            args = op2_C["1"][::-1]
            args.extend(S)
            args.extend(op2_C["2"])
            S = ''.join(args)
            op2_C = {"1": [], "2": []}
            op2_cnt = 0

    else:
        op2_cnt += 1

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

        op1_cnt = 0

        t, F, C = query.split()
        op2_C[F].append(C)

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

if op2_cnt != 0:
    args = op2_C["1"][::-1]
    args.extend(S)
    args.extend(op2_C["2"])
    S = ''.join(args)


print(S)

Submission status: Time Limit Exceeded

Solution code:
N = input()
Q = int(input())
Qs = []
for _ in range(Q):
    Qs.append(list(map(str, input().split())) ) 
k=0
for q in Qs:
    if q[0] == '1':
        k+=1
    else:
        if k%2 ==0:
            if q[1]=='1':
                N = q[2] + N
            else:
                N = N + q[2]
        else:
            if q[1]=='2':
                N = q[2] + N
            else:
                N = N + q[2]

if k%2==1:
    ans = ''.join(list(reversed(N)))
    print(ans)
else:
    print(N)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
query = [input().split() for _ in range(Q)]
reverse = False
for q in query:
    if q[0] == '1':
        reverse = not reverse
    else:
        if q[1] == '1':
            S.append(q[2]) if reverse else S.insert(0, q[2])
        else:
            S.insert(0, q[2]) if reverse else S.append(q[2])
if reverse:
    S.reverse()
print(''.join(S))

Submission status: Time Limit Exceeded

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

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

querys = []
op1_cnt = 0
op2_cnt = 0
op2_C = {"1": [], "2": []}
for i in range(Q):
    query = input()
    if len(query) == 1:
        op1_cnt += 1

        if op2_cnt != 0:
            args = op2_C["1"][::-1]
            args.extend(S)
            args.extend(op2_C["2"])
            S = ''.join(args)
            op2_C = {"1": [], "2": []}
            op2_cnt = 0

    else:
        op2_cnt += 1

        if op1_cnt % 2 != 0 and len(S) != 1:
            S = S[::-1]

        op1_cnt = 0

        t, F, C = query.split()
        op2_C[F].append(C)

if op1_cnt % 2 != 0 and len(S) != 1:
    S = S[::-1]

if op2_cnt != 0:
    args = op2_C["1"][::-1]
    args.extend(S)
    args.extend(op2_C["2"])
    S = ''.join(args)


print(S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for _ in range(Q):
  s = list(sys.stdin.readline())
  if s[0]=="1":
    if flag:
      flag=False
    else:
      flag=True
  else:
    if s[2]=="1":
      if flag:
        S="".join( [ S,s[4] ] )
      else:
        S="".join( [ s[4],S ] )
    else:
      if flag:
        S="".join( [ s[4],S ] )
      else:              
        S="".join( [ S,s[4] ] )
        
if flag:
  S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
 
S = deque(input())
Q = int(input())
 
Q_list = [list(map(str, input().split())) for i in range(Q)]
 
for i in range(Q):
  if int(Q_list[i][0]) == 1:
    if len(S) >= 2:
      S.reverse()
  
  else:
    F = Q_list[i][1]
    C = Q_list[i][2]
    
    if int(F) == 1:
      S.appendleft(C)
    else:
      S.append(C)
      
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
N = input()
Q = int(input())
Qs = []
for _ in range(Q):
    Qs.append(list(map(str, input().split())) ) 
k=0
for q in Qs:
    if q[0] == '1':
        k+=1
    else:
        if (k%2 == 0 and q[1] == '1') or (k%2 == 1 and q[1] == '2'):
            N = q[2] + N
        else:
            N = N + q[2]

if k%2==1:
    ans = ''.join(list(reversed(N)))
    print(ans)
else:
    print(N)

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)
left = True
for i in query:
    if len(i) == 1:
        left = not left
        # print(left)
    else:
        if i[1]=="1":
            if left:
                S = i[2]+S
            else:
                S += i[2]
        else:
            if left:
                S += i[2]
            else:
                S = i[2]+S
if left:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

dir = 1

for _ in range(qn):
  q = input()
  if q[0]=="1":
    dir = 1-dir
  else:
    d, f, c = q.split()
    if f=="1":
      if dir==1:
        a=c+a
      else:
        a+=c
    else:
      if dir==1:
        a+=c
      else:
        a=c+a

if dir==0:
  a = reversed(a)

print(''.join(list(a)))

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
n = int(input())
q_list = [list(map(str, input().strip().split())) for i in range(n)]

rev = 0
for_s = ''
rev_s = ''

out = ''
out += s
for q in q_list:
    if q[0] == '1':
        rev = 1 - rev
    elif q[1] == '1':
        if rev == 0:
            #rev_s += q[2]
            out = q[2] + out
        else:
            #for_s += q[2]
            out = out + q[2]
    else:
        if rev == 0:
            #for_s += q[2]
            out = out + q[2]
        else:
            #rev_s += q[2]
            out = q[2] + out

#if rev == 0:
#    out = rev_s[::-1] + s + for_s
#else:
#    out = for_s[::-1] + s + rev_s
if rev == 1:
    out = out[::-1]
print(out)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
flag=1
for i in range(n):
    l=list(input().split())
    if len(l)==1:
        flag*=(-1)
    elif l[1]=="1" and flag==1 or l[1]=="2" and flag==-1:
        s=l[2]+s
    else:s+=l[2]
if flag==-1:print(s[::-1])
else:print(s)

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)
cnt=0
for i in range(N):
    if cnt%2==0:
        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 cnt%2==1:
        if Q[i][0]=="2":
            if Q[i][2]=="1":
                T.append(Q[i][4])
                S=list(T)
            if Q[i][2]=="2":
                T.appendleft(Q[i][4])
                S=list(T)
    if Q[i][0]=="1":
        cnt+=1
if cnt%2==1:
    S=S[::-1]
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
x = 1
for i in range(Q):
    a, *b = input().split()
    if a == "1":
        x *= -1
    elif a == "2":
        c, d = b
        if (c == "2" and x == -1) or  (c == "1" and x == 1):
            S.insert(0, d)
        else:
            S.append(d)
if x == -1:
    S = S[::-1]
print(*S, sep="")

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

def main():
    S = readline().strip().decode('utf-8')
    Q = int(readline())
    Query = [tuple(readline().decode('utf-8').split()) for _ in range(Q)]
    rev_flg = 0
    for q in Query:
        if q[0] == '1':
            rev_flg += 1
        else:
            if int(q[1])-(rev_flg%2) == 1:
                S = q[2] + S
            else:
                S += q[2]
    if rev_flg%2 == 1:
        S = S[::-1]
    print(S)

    return
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  s=input()
  q=int(input())
  turn = 2
  tmp=[]
  for i in s:
      tmp.append(i)
  for _ in [0]*q:
      query = input()
      if query[0] == "1":
          t = int(query)
          turn = 3 - turn
      else:
          t,f,c = map(str,query.split())
          if (int(f) + turn)% 2  == 0:
              tmp.append(c)
          else:
              tmp = [c] + tmp
  if turn == 2:
      print("".join(tmp))
  else:
      print("".join(tmp[::-1]))
main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import deque

S = input()
Q = int(input())
reverse_cnt = 1
index = []
s = deque([])

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse_cnt *= -1
        index = list(np.array(index) * -1)
    else:
        s.append(query[2])
        if query[1] == "1":
            index.append(-1)
        else:
            index.append(1)

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

insert = ""
add = ""

for i in index:
    if i == 1:
        add += s.popleft()
    elif i == -1:
        insert += s.popleft()

print(insert[::-1] + S + add)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

a = list(input())
n = int(input())
mode = 1
i = 0
while i < n:
    hoge = input().split()
    if hoge[0] == '1':
        mode = mode * -1
    else:
        nume = hoge[1]
        piyo = hoge[2]
        if mode == 1:
            if nume == '1':
                a = deque(a)
                a.appendleft(piyo)
                a = list(a)
            else:
                a.append(piyo)
        else:
            if nume == '2':
                a = deque(a)
                a.appendleft(piyo)
                a = list(a)
            else:
                a.append(piyo)
    i += 1

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

print(''.join(a))

Submission status: Time Limit Exceeded

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


reverse = False
start_text = ''
end_text = ''
input_texts = []
for _ in range(Q):
    input_texts.append(list(input().split()))

for input_text in input_texts:
    if len(input_text) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        if input_text[1] == '1':  # 先頭に
            if reverse:
                end_text += str(input_text[2])
            else:
                start_text = str(input_text[2]) + start_text
        else:  # 末尾に
            if reverse:
                start_text = str(input_text[2]) + start_text
            else:
                end_text += str(input_text[2])

if reverse:
    texts = list(start_text + texts + end_text)
    texts.reverse()
    print("".join(texts))
else:
    # texts = start_text + texts + end_text
    print(start_text + texts + end_text)

Submission status: Time Limit Exceeded

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


q = []
for n in range(Q):
  q.append(list(input().rstrip().split()))

#print(q)
def rev(olist):
  nlist  = olist[::-1]
  return nlist

def addhead(olist,ele):
  nlist = olist.insert(0,ele)
  return nlist
  
def addtail(olist,ele):
  nlist = olist.append(ele)
  return nlist
v = []
for n in range(Q):
  if q[n][0] == '1':
    S = rev(S)
  else:
    if q[n][1] == '1':
      addhead(S,q[n][2])
    else:
      addtail(S,q[n][2])

print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
#s=raw_input()
l=list(input())
q=int(input())

r=False
for i in range(0, q):
    t=input()
    if t[0] == "1":
        r = not r
        #s = s[::-1]
    else:
        if t[2] == "1" and r is False or t[2] == "2" and r is True:
            #s = t[4] + s
            l.insert(0,t[4])
        else:
            #s = s + t[4]
            l.append(t[4])

if r is True:
    #s = s[::-1]
    l = l[::-1]

#print s
print("".join(l))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=deque(list(input()))
q=int(input())
direction=0 #0:順 1:逆

for i in range(q):
    query=list(input().split())
    i=int(query[0])
    if i==1:
        direction=direction^1
    elif i==2:
        f,c=int(query[1]),query[2]
        if f==1 and direction==0:
            s.appendleft(c)
        elif f==1 and direction==1:
            s.append(c)
        elif f==2 and direction==0:
            s.append(c)
        elif f==2 and direction==1:
            s.appendleft(c)

if direction==0:
    print(''.join(s))
else:
    x=[]
    for i in range(len(s)):
        x.append(s[-1-i])
    print(''.join(x))