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:
from collections import deque
s = deque(list(input()))
q = int(input())
for i in range(q):
    line = list(input().split())
    t = int(line[0])
    if t == 1:
        s.reverse()
    else:
        if int(line[1]) == 1:
            s.appendleft(line[2])
        else:
            s.append(line[2])
print("".join(s))

Submission status: Time Limit Exceeded

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

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''


def main():
    import sys
    input = sys.stdin.readline
    s = input().rstrip("\n")
    q = int(input())
    han = 0
    qq=[input().split() for i in range(q)]

    for que in qq:
        if len(s) == 1 and que[0] == "1":
            continue

        if que[0] == "1":
            han += 1
            #print(han, s, que)
        else:
            if han % 2 == 0:  # 反転なし
                if que[1] == "1":
                    s = que[2]+s
                else:
                    s += que[2]
            else:  # あり
                if que[1] == "1":
                    s +=que[2]
                else:
                    s = que[2]+s
            #print(han, s, que)

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
Q=int(input())
re=True
cnt=0
for i in range(Q):
    q=list(map(str,input().split()))
    if q[0]=='1':
        cnt+=1
        if re==True:
            re=False
        else:
            re=True
    else:
        if re==True:
            if q[1]=='1':
                S.insert(0,q[2])
            else:
                S.append(q[2])
        else:
            if q[1]=='2':
                S.insert(0,q[2])
            else:
                S.append(q[2])
if cnt%2==1:
    S.reverse()
print(*S,sep='')

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

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

direction = 1
for i in range(Q):
    T = input()
    if T == '1':
        direction = direction * -1
    else:
        T = T.split()
        if T[1] == '1':
            if direction == 1:
                S = T[2] + S
            else:
                S = S + T[2]
        else:
            if direction == 1:
                S = S + T[2]
            else:
                S = T[2] + S

S = S[::direction]

print(S)

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

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

cnt = [0]*(q+5)
arr=[]
l=0
r=0

for i in range(q):
    t=input().split()
    if t[0]=='1':
        r+=1
        cnt[0]+=1 
        cnt[l]-=1 
    else:
        arr.append([t[2],int(t[1])])
        l+=1 
if r%2==1:
    s=s[::-1]
sm=0
for i in range(len(arr)):
    sm+=cnt[i]
    if sm%2==1:
        if arr[i][1]==1:arr[i][1]=2
        elif arr[i][1]==2:arr[i][1]=1 
for i in range(len(arr)):
    if arr[i][1]==1:
        s=arr[i][0]+s

    else:s=s+arr[i][0]
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] == '2':
            s = s + l[2]
        else:
            s = s[::-1] + l[2]
            s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


def main():
    S = input().rstrip()
    Q = int(input())
    query = [None] * Q
    for i in range(Q):
        query[i] = input().split()

    ans = S
    is_reversed = False
    for q in query:
        if int(q[0]) == 1:
            is_reversed = not is_reversed
        else:
            F, C = int(q[1]), q[2]
            if F == 1:
                if is_reversed:
                    ans = "".join([ans, C])
                else:
                    ans = "".join([C, ans])
            else:
                if is_reversed:
                    ans = "".join([C, ans])
                else:
                    ans = "".join([ans, C])

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def z(z_dic, v: int):
    if v in z_dic:
        return z_dic[v]
    z = 0
    if v == 2:
        z = 1
    elif v > 2:
        z = int(v*(v-1)/2)
    z_dic.setdefault(v, z)
    return z


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

    reverse = False
    for _ in range(q):
        line_ls = input().split()
        # 長さが1ならTi=1
        if len(line_ls) == 1:
            reverse = not reverse
            continue

        fi = line_ls[1]
        ci = line_ls[2]
        if fi == "1":
            if not reverse:
                s = ci + s
            else:
                s += ci
        else:
            if not reverse:
                s += ci
            else:
                s = ci + s

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

    print(s)




main()

Submission status: Time Limit Exceeded

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

inv = False
for i in range(Q):
  inp = input().strip().split()
  if len(inp)==1:
    inv = not inv
  else:
    t, f, c = inp
    if (f=="1" and not inv) or (f=="2" and inv):
      S = c + S
    elif (f=="2" and not inv) or (f=="1" and inv):
      S = S + c
if not inv:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

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

for i in range(q):
    que=input().split()
    if que[0]=="1":
        S=S[::-1]
    else:
        if que[1]=="1":
            S=que[2]+S
        else:
            S=S+que[2]
    
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
actions = []

for i in range(q):
  actions.append(input().split(" "))

for action in actions:
  if action[0] == "1":
    s = s[::-1]
  else:
    if action[1] == "1":
      s = action[2]+s
    else:
      s = s+action[2]
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for _ in range(Q):
  q = input()
  
  if q[0] == "1":
    S.reverse()
  
  if q[0] == "2":
    if q[2] == "1":
      S.appendleft(q[4])
    else:
      S.append(q[4])

print("".join(S))

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

# input
s = list(input())
q = int(input())
query = ['' for _ in range(q)]
for i in range(q):
    query[i] = input().split()

# calc
for i in range(q):
    if query[i][0] == '1':
        s = s[::-1]
    else:
        if query[i][1] == '1':
            s.insert(0, query[i][2])
        if query[i][1] == '2':
            s.append(query[i][2])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
ls = []
for i in range(q):
    qq = input().split()
    ls.append(qq)
for j in range(q):
    if ls[j][0] == '1':
        s = s[::-1]
    elif ls[j][0] == '2':
        if ls[j][1] == '1':
            s = ls[j][2] + s
        else:
            s = s + ls[j][2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 998244353
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline
S = S()
S = [S[i] for i in range(len(S))]
Q = I()
rev = False
for i in range(Q):
    q = LS()
    if q[0] == '1':
        rev = not rev
        continue
    if (q[1] == '1') ^ rev:
        S = [q[2]]+S
    else:
        S = S+[q[2]]
if rev:
    ans = ''.join(S[::-1])
else:
    ans = ''.join(S)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 998244353
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline
S = S()
Q = I()
rev = False
for i in range(Q):
    q = LS()
    if q[0] == '1':
        rev = not rev
        continue
    if (q[1] == '1') ^ rev:
        S = ''.join([q[2],S])
    else:
        S = ''.join([S,q[2]])
if rev:
    ans = S[::-1]
else:
    ans = S

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
r = 0
qn = []*3
left =''
right = ''

for i in range(q):
    qn = input().split()
    if qn[0] == '1':
        r += 1
    elif qn[0] == '2':
        if qn[1] == '1' and r%2 == 0:
            left = qn[2] + left
        elif qn[1] == '1' and r%2 == 1:
            right += qn[2]
        elif qn[1] == '2' and r % 2 == 0:
            right += qn[2]
        elif qn[1] == '2' and r % 2 == 1:
            left = qn[2] + left

ans = left + s + right

if r%2 == 1:
    ans = ans[::-1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
from functools import reduce

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

deque = collections.deque(S)

is_reverse = False
for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if (query[1] == '1' and not is_reverse) or (query[1] =='2' and is_reverse):
            deque.appendleft(query[2])
        else:
            deque.append(query[2])
res = ""
if not is_reverse:
    for s in deque:
        res += s
else:
    for s in deque:
        res = s+res
print(res)

Submission status: Time Limit Exceeded

Solution code:
import collections
from functools import reduce

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

deque = collections.deque(S)

is_reverse = False
for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if (query[1] == '1' and not is_reverse) or (query[1] =='2' and is_reverse):
            deque.appendleft(query[2])
        else:
            deque.append(query[2])
if is_reverse:
    deque.reverse()
print(reduce(lambda x, y: x+y, list(deque)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = list(input())
q = int(input())
cnt = 0
s = deque(s)
for i in range(q):
    l = input().split()
    if l[0] == "1":
        s.reverse()
        continue
    if l[1] == "1":
        s.appendleft(l[2])
    else:
        s.append(l[2])
print(*s, sep="")

Submission status: Time Limit Exceeded

Solution code:
S=(input())
Q=int(input())
cnt_re=0
for i in range(Q):
    q=list(input().split())
    if q[0]=='1':
        cnt_re+=1
    else:
        if (int(q[1])+cnt_re)%2==1:
            S=q[2]+S
        else:
            S=S+q[2]

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

Submission status: Time Limit Exceeded

Solution code:
S=(input())
Q=int(input())
cnt_re=0
for i in range(Q):
    q=list(input().split())
    if q[0]=='1':
        cnt_re+=1
    else:
        tmp=cnt_re%2
        tmpq=q[1]
        if ((tmpq=='1')&(tmp==0)) | ((tmpq=='2')&(tmp==1)):
            S=q[2]+S
        else:
            S=S+q[2]

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

Submission status: Time Limit Exceeded

Solution code:
S=(input())
Q=int(input())
cnt_re=0
for i in range(Q):
    q=input().split()
    if q[0]=='1':
        cnt_re+=1
    else:
        tmp=cnt_re%2
        if ((q[1]=='1')&(tmp==0)) | ((q[1]=='2')&(tmp==1)):
            S=q[2]+S
        elif ((q[1]=='1')&(tmp==1)) | ((q[1]=='2')&(tmp==0)):
            S=S+q[2]
            
if cnt_re%2==1:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
S=(input())
Q=int(input())
cnt_re=0
for i in range(Q):
    q=input().split()
    if q[0]=='1':
        cnt_re+=1
    else:
        tmp=cnt_re%2
        if ((q[1]=='1')&(tmp==0)) | ((q[1]=='2')&(tmp==1)):
            S=q[2]+S
        elif ((q[1]=='1')&(tmp==1)) | ((q[1]=='2')&(tmp==0)):
            S=S+q[2]
S=list(S)
if cnt_re%2==1:
    S.reverse()
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
count = 0
ans = s
for i in range(q):
    tmp = input()
    if tmp[0] == "1":
        count += 1
    else:
        _, f, c = map(str, tmp.split())
        if count % 2 == 0:
            ans = (c + ans) if f == "1" else (ans + c)
        else:
            ans = (ans + c) if f == "1" else (c + ans)
ans = ans[::-1] if count % 2 == 1 else ans
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
ans = s
for i in range(q):
    tmp = input()
    if tmp[0] == "1":
        ans = ans[::-1]
    else:
        _, f, c = map(str, tmp.split())
        ans = (c + ans) if f == "1" else (ans + c)
print(ans)

Submission status: Time Limit Exceeded

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

flag=False

head=''

for _ in range(n):
  l=input().split()
  if l[0]=='1':
    flag= not flag
  elif l[0]=='2':
    if (not flag) and l[1]=='1' or flag and l[1]=='2':
      head=''.join([head,l[2]])
    elif (not flag) and l[1]=='2' or flag and l[1]=='1':
      s=''.join([s,l[2]])

if flag:
  s=s[::-1]+head
else:
  s=head[::-1]+s

print(s)

Submission status: Time Limit Exceeded

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

flag=1

head=''

for _ in range(n):
  l=input().split()
  if l[0]=='1':
    flag*=-1
  elif l[0]=='2':
    if flag==1 and l[1]=='1' or flag==-1 and l[1]=='2':
      head+=l[2]
    elif flag==1 and l[1]=='2' or flag==-1 and l[1]=='1':
      s+=l[2]

s=head[::-1]+s

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

print(s)

Submission status: Time Limit Exceeded

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

flag=1

head=''
tail=''

for _ in range(n):
  l=input().split()
  if l[0]=='1':
    flag*=-1
  elif l[0]=='2':
    if flag==1 and l[1]=='1' or flag==-1 and l[1]=='2':
      head=''.join([head,l[2]])
    elif flag==1 and l[1]=='2' or flag==-1 and l[1]=='1':
      tail=''.join([tail,l[2]])

s=head[::-1]+s+tail

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
reverse=False
for i in range(n):
  ins=input()
  if ins=="1":
    if reverse==False:
       reverse=True
    else:
       reverse=False
  else:
    a,b,c  =ins.split()
    if b=="1":
      #先頭に追加
      if reverse==False:
        s=c+s
      else:
        s=s+c
    else:
      if reverse==False:
        s=s+c
      else:
        s=c+s
if reverse==True:
  s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

flag=1

for _ in range(n):
  l=input().split()
  if l[0]=='1':
    flag*=-1
  elif l[0]=='2':
    if flag==1 and l[1]=='1' or flag==-1 and l[1]=='2':
      s=''.join([l[2],s])
    elif flag==1 and l[1]=='2' or flag==-1 and l[1]=='1':
      s=''.join([s,l[2]])

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

print(s)

Submission status: Time Limit Exceeded

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

flag=1

for l in q:
  if l[0]=='1':
    flag*=-1
  elif l[0]=='2':
    if flag==1 and l[1]=='1' or flag==-1 and l[1]=='2':
      s=''.join([l[2],s])
    elif flag==1 and l[1]=='2' or flag==-1 and l[1]=='1':
      s=''.join([s,l[2]])

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
Qer = [list(map(str,input().split())) for i in range(Q)]
flag = 0
for i in range(Q):
    if Qer[i][0] == "1":
        flag+=1
    else:
        if flag%2 ==0:
            if Qer[i][1] =="1":
                S = Qer[i][2] + S
            else:
                S = S+Qer[i][2]
        else:
            if Qer[i][1] =="2":
                S = Qer[i][2] + S
            else:
                S = S+Qer[i][2]

print(S if flag%2==0 else S[::-1])

Submission status: Time Limit Exceeded

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

flag=1

for l in q:
  if l[0]=='1':
    flag*=-1
  elif l[0]=='2':
    if flag==1 and l[1]=='1' or flag==-1 and l[1]=='2':
      s=l[2]+s
    elif flag==1 and l[1]=='2' or flag==-1 and l[1]=='1':
      s=s+l[2]

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

print(s)

Submission status: Time Limit Exceeded

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
status = 1
for i in range(q):
    query = [i for i in input().split()]
    if query[0] == "1":
        status *= -1
    else:
        if (query[1] == "1" and status == 1) or (query[1] == "2" and status == -1):
            s = query[2] + s
        else:
            s = s + query[2]

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
status = 1
for i in range(q):
    query = [i for i in input().split()]
    if query[0] == "1":
        status *= -1
    else:
        if (query[1] == "1" and status == 1) or (query[1] == "2" and status == -1):
            s = query[2] + s
        else:
            s = s + query[2]

print(s if status == 1 else "".join(list(reversed(s))))

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":
        rev ^= True
    else:
        if (query[1] == "1" and not rev) or (query[1] == "2" and 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:
import sys

def input(): return sys.stdin.readline().strip()

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

for i in range(q):
    query = input().split()
    if query[0] == "1":
        s = s[::-1]
    else:
        if query[1] == "1":
            s = query[2] + s
        else:
            s = s + query[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] == "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 = input()
q = int(input())
for i in range(q):
    query = [i for i in input().split()]
    if query[0] == "1":
        s = "".join(list(reversed(s)))
    else:
        if query[1] == "1":
            s = query[2] + s
        else:
            s = s + query[2]

print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    qry = query[i]
    if qry[0] == '1':
        s.reverse()
    else:
        if qry[1] == '1':
            s.insert(0, qry[2])
        else:
            s.append(qry[2])
            
s = ''.join(s)
print(s)

Submission status: Time Limit Exceeded

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

for _ in range(q):
    qu = list(input().split())
    if len(qu) == 1:
        o = -1 if o == 0 else 0
    if len(qu) == 3:
        if qu[1] == '1':
            s = ''.join([qu[2], s]) if o == 0 else ''.join([s, qu[2]])
        if qu[1] == '2':
            s = ''.join([s, qu[2]]) if o == 0 else ''.join([qu[2], s])

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

Submission status: Time Limit Exceeded

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

for _ in range(q):
    qu = list(input().split())
    if len(qu) == 1:
        o = -1 if o == 0 else 0
    if len(qu) == 3:
        if qu[1] == '1':
            s = qu[2] + s if o == 0 else s + qu[2]
        if qu[1] == '2':
            s = s + qu[2] if o == 0 else qu[2] + s

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
o = 0
for _ in range(q):
    qu = list(input().split())
    if len(qu) == 1:
        o = -1 if o == 0 else 0
    if len(qu) == 3:
        if qu[1] == '1':
            s = qu[2] + s if o == 0 else s + qu[2]
        if qu[1] == '2':
            s = s + qu[2] if o == 0 else qu[2] + s

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = deque(input())
q = int(input())
a = 0
c = ""
for i in range(q):
    b = list(input().split())
    if b[0] == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        if (a, b[1]) == (0, "1") or (a, b[1]) == (1, "2"):
            s.appendleft(b[2])
        else:
            s.append(b[2])
if a == 0:
    for i in range(len(s)):
        c += s[i]
else:
    for i in range(len(s)):
        c += s[-i-1]
print(c)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
for _ in range(Q):
  try:
    T,F,C = map(str,input().split())
    if F == '1':
      S.extendleft([C])
    else:
      S.extend([C])
  except ValueError:
    S.reverse()
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for _ in range(Q):
  try:
    T,F,C = map(str,input().split())
    if F == '1':
      S.insert(0, C)
    else:
      S.insert(len(S),C)
  except ValueError:
    S.reverse()
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for _ in range(Q):
  try:
    T,F,C = map(str,input().split())
    if F == '1':
      S.insert(0, C)
    elif F == '2':
      S.append(C)
  except ValueError:
    S.reverse()
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
a = 0
c = ""
for i in range(q):
    b = list(input().split())
    if b[0] == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        if (a, b[1]) == (0, "1") or (a, b[1]) == (1, "2"):
            s.insert(0, b[2])
        else:
            s.append(b[2])
if a == 0:
    for i in range(len(s)):
        c = c + s[i]
else:
    for i in range(len(s)):
        c = c + s[-i-1]
print(c)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
a = 0
for i in range(q):
    b = list(input().split())
    if b[0] == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        if (a, b[1]) == (0, "1") or (a, b[1]) == (1, "2"):
            s = b[2] + s
        else:
            s = s + b[2]
if a == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
import collections

d = collections.deque()
for i in input():
    d.append(i)
q = int(input())
is_head = True

for _ in range(q):
    tmp = input().split(" ")
    if (len(tmp) == 1):
        is_head = not is_head
    else:
        t, f, c = tmp
        if (f == '1'):
            if (is_head):
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if (is_head):
                d.append(c)
            else:
                d.appendleft(c)

ans = ""
if is_head:
    while len(d) > 0:
        ans += d.popleft()
else:
    while len(d) > 0:
        ans += d.pop()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
is_head = True
 
for _ in range(q):
    tmp = input().split(" ")
    if (len(tmp) == 1):
        is_head = not is_head
    else:
        t, f, c = tmp
        if (f == '1'):
            if (is_head):
                s = c + s
            else:
                s =  s + c
        else:
            if (is_head):
                s = s + c
            else:
                s =  c + s
 
if is_head:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:

s = input()
q = int(input())
is_head = True

for _ in range(q):
    tmp = input().split(" ")
    if (len(tmp) == 1):
        is_head = not is_head
    else:
        t, f, c = tmp
        if (f == '1'):
            if (is_head):
                s = c + s
            else:
                s =  s + c
        else:
            if (is_head):
                s = s + c
            else:
                s =  c + s

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

Submission status: Time Limit Exceeded

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

for _ in range(q):
    tmp = input().split(" ")
    if (len(tmp) == 1):
        s = s[::-1]
    else:
        t, f, c = tmp
        if (f == '1'):
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
s = input()
q = int(input())
li = ["", s]
cur = 1
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        cur ^= 1
    else:
        f, c = query[1:]
        if f == "1":
            li[cur ^ 1] += c
        else:
            li[cur] += c
ans = li[0][::-1] + li[1]
if cur == 0:
    ans = ans[::-1]
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
        arr = list(input().split())
        if arr[0] == '1':
            switch_flg *= -1

        elif arr[0] == '2':
            if int(arr[1])*switch_flg == 1:
                S = arr[2]+S
            elif int(arr[1])*switch_flg == -2:
                S = arr[2]+S
            elif int(arr[1])*switch_flg == -1:
                S = S+arr[2]
            elif int(arr[1])*switch_flg == 2:
                S = S+arr[2]

if switch_flg ==1:
    print(S)
elif switch_flg == -1:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
rev_cnt = 0
for i in range(Q):
    query = [_ for _ in input().split()]
    if len(query) == 1:
        rev_cnt += 1
    else:
        if query[1] == '1':
            if rev_cnt%2 == 0:
                S = query[2] + S
            else:
                S = S + query[2]
        else:
            if rev_cnt%2 == 0:
                S = S + query[2]
            else:
                S = query[2] + S
if rev_cnt % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())
for i in range(Q):
    query = [_ for _ in input().split()]
    if len(query) == 1:
        S.reverse()
    else:
        if query[1] == '1':
            S.appendleft(query[2])
        else:
            S.append(query[2])
print(*list(S), sep='')

Submission status: Time Limit Exceeded

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