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:
a = input()
q = int(input())

fflag = 0
for i in range(q):
  qi = input()
  if( qi == "1"):
    fflag += 1 
  else:
    if( fflag%2 == 0 ):
      if( qi[2] == "2" ):
        a += qi[4]
      else:
        a = qi[4] + a
    else:
      if( qi[2] == "2" ):
        a = qi[4] + a
      else:
        a += qi[4]      
    
if( fflag%2==0 ):
  print( a )
else:
  print( a[::-1] )

Submission status: Time Limit Exceeded

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

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

    if list[i][0] == '1':
        S = S[::-1]
    elif list[i][0] == '2':
        if list[i][1] == '1':
            S = list[i][2] + S
        elif list[i][1] == '2':
            S = S + list[i][2]
    else:
        continue
print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    X = list(input().split(" "))
    if X[0] == "1" and rev is False:
        rev = True
    elif X[0] == "1" and rev is True:
        rev = False
    elif X[0] == "2" and X[1] == "1" and rev is False:
        S.insert(0, X[2])
    elif X[0] == "2" and X[1] == "1" and rev is True:
        S.append(X[2])
    elif X[0] == "2" and X[1] == "2" and rev is False:
        S.append(X[2])
    elif X[0] == "2" and X[1] == "2" and rev is True:
        S.insert(0, X[2])

if rev is True:
    S.reverse()

print(*S, sep="")

Submission status: Time Limit Exceeded

Solution code:
S = [c for c in input()]
N = int(input())
r = 0

for i in range(N):
	values = input().split()
	if values[0] == '1':
		r ^= 1
	else:
		f = int(values[1]) - 1
		if f == r:
			S.insert(0, values[2])
		else:
			S.append(values[2])

if r == 0:
	print(''.join(S))
else:
	print(''.join(S)[::-1])

Submission status: Time Limit Exceeded

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

fflag = 1
for i in range(q):
  qi = input()
  if( qi == "1"):
    fflag *= -1 
  else:
    if( fflag == 1 ):
      if( qi[2] == "2" ):
        a += qi[4]
      else:
        a = qi[4] + a
    else:
      if( qi[2] == "2" ):
        a = qi[4] + a
      else:
        a += qi[4]      
    
if( fflag == 1 ):
  print( a )
else:
  print( a[::-1] )

Submission status: Time Limit Exceeded

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

fflag = 1
for i in range(q):
  qi = input()
  if( qi == "1"):
    fflag *= -1 
  else:
    if( fflag == 1 ):
      if( qi[2] == "2" ):
        a = a + qi[4]
      else:
        a = qi[4] + a
    else:
      if( qi[2] == "2" ):
        a = qi[4] + a
      else:
        a = a + qi[4]      
    
if( fflag == 1 ):
  print( a )
else:
  print( a[::-1] )

Submission status: Time Limit Exceeded

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

for i in range(q):
  qi = input()
  if( qi == "1"):
    a = a[::-1]
  else:
    if( qi[2] == "2" ):
      a = a + qi[4]
    else:
      a = qi[4] + a
      
print( a )

Submission status: Time Limit Exceeded

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

ct = 0

for i in range(n):
    p = input().split()

    if p[0] == '1':
        ct += 1
    else:
        if ct % 2 != 0:
            s = s[::-1]
        ct = 0
        
        if p[1] == "1":
            s = p[2] + s
        else:
            s = s + p[2] 
    
if ct % 2 != 0:
    s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    X = list(input().split(" "))
    if int(X[0]) == 1:
        S.reverse()
    elif int(X[0]) == 2 and int(X[1]) == 1:
        S.insert(0, X[2])
    elif int(X[0]) == 2 and int(X[1]) == 2:
        S.append(X[2])

print(*S, sep="")

Submission status: Time Limit Exceeded

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

for i in range(Q):
    if list[i][0] == '1':
        S = S[::-1]
    elif list[i][0] == '2':
        if list[i][1] == '1':
            S = list[i][2] + S
        elif list[i][1] == '2':
            S = S + list[i][2]
    else:
        continue
print(S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

    hanten = 0 # 偶数なら正常、 奇数なら反転状態

    front = "" # リストの前に追加するのは、O(n)なので避ける
    back = ""

    for _ in range(q):
        tmp = readline().split()
        if len(tmp) == 1:
            hanten ^= 1
        else:
            if hanten: # 反対ver
                if tmp[1] == '1':
                    back += tmp[2]
                else:
                    front += tmp[2]
            else: # 正常
                if tmp[1] == '1':
                    front += tmp[2]
                else:
                    back += tmp[2]

    if hanten == 0:
        print(front[::-1] + s + back)
    else:
        print((front[::-1] + s + back)[::-1])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
ss=s
q=int(input())
c=0
for i in range(q):
    a=[str(x) for x in input().split()]
    if len(a)==1:
        c+=1
    else:
        if a[1]=="1":
            if c%2==0:
                ss=a[2]+ss 
            else:
                ss+=a[2]
        else:
            if c%2!=0:
                ss=a[2]+ss 
            else:
                ss+=a[2] 
if c%2==0:
    print(ss)
else:
    print(ss[::-1])

Submission status: Time Limit Exceeded

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

for i in range(q):
    query = list(input().split())
    if(int(query[0]) == 1):
        s = s[::-1]
    elif(int(query[0]) == 2):
        txt = ''.join(query[2:])
        if(int(query[1]) == 1):
            s = txt + s
        elif (int(query[1]) == 2):
            s += txt

print(s)

Submission status: Time Limit Exceeded

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

    hanten = 0 # 偶数なら正常、 奇数なら反転状態

    front = ""
    back = ""

    for _ in range(q):
        tmp = input()
        if len(tmp) == 1:
            hanten ^= 1
        else:
            if hanten: # 反対ver
                if tmp[2] == '1':
                    back += tmp[4]
                else:
                    front += tmp[4]
            else: # 正常
                if tmp[2] == '1':
                    front += tmp[4]
                else:
                    back += tmp[4]

    if hanten == 0:
        print(front[::-1] + s + back)
    else:
        print((front[::-1] + s + back)[::-1])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

q_list = []
rev_list = []

rev_c = 0
count = 0

for _ in range(q):
  v = input()
  if v == '1':
    rev_c += 1
  else:
    rev_list.append(rev_c)
    q_list.append(v)
    
if rev_c % 2 == 1:
  s = s[::-1]
  
for i,v in enumerate(q_list):
  count = rev_list[i]
  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())
j=0
for l in range(q):
    a=input()
    if a[0]=="1":
        j+=1
    else:
        if a[2]=="1":
            if j%2==0:
                s=a[4]+s
            else:
                s=s+a[4]
        else:
            if j%2==0:
                s=s+a[4]
            else:
                s=a[4]+s
if j%2==0:
    print(s)
else:
    s=s[::-1]
    print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
S = input()
Q = int(input())
for i in range(Q):
    q = input().split()
    if q[0] == "1":
        S = S[::-1]
    else:
        if q[1] == "1":
            S = q[2] + S
        else:
            S += q[2]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    d = input()
    n = int(input())
    for _ in range(n):
        q = input()
        d = d[::-1] if q[0] == ('1') else q[-1]+d if q[:3] == ("2 1") else d+q[-1]
    print(d)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
S=input()
Q=int(input())
onecntr=0
for i in range(Q):
    tmp=input().split()
    if(tmp[0]=="1"):
        onecntr+=1
    else:
        if(onecntr%2==0):
            if(tmp[1]=="1"):
                S=tmp[2]+S
            else:
                S=S+tmp[2]
        else:
            if(tmp[1]=="2"):
                S=tmp[2]+S
            else:
                S=S+tmp[2]
    
    
if(onecntr%2==1):
    S=S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

query =[]
count = 0
for i in range(q):
	a = list(map(str,input().split()))
	a[0] = int(a[0])
	
	if len(a) == 1:
		if a[0] == 1:
			count += 1
	else:
		a[1] = int(a[1])
		if a[0] == 2:
			if count %2 ==1:
				s.reverse()
			if a[1] == 1:
				b = [a[2]]
				b.extend(s)
				s = b
			elif a[1] == 2:
				s.append(a[2])
		count = 0
	if i == q-1:
		if count %2 ==1:
				s.reverse()
ans = ""
for i in range(len(s)):
	ans += s[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    d = input()
    n = int(input())
    for _ in range(n):
        q = input()
        if q[0] == ('1'):
            d = d[::-1]
        elif q[:3] == ("2 1"):
            d = q[-1]+d
        elif q[:3] == ("2 2"):
            d=d+q[-1]
    print(d)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
S=input()
Q=int(input())
q=[[0 for i in range(3)] for j in range(Q)]
onecntr=0
for i in range(Q):
    tmp=input().split()
    if(tmp[0]=="1"):
        q[i][0]=1
    else:
        q[i][0]=2
        q[i][1]=int(tmp[1])
        q[i][2]=tmp[2]
    

for i in range(Q):
    if(q[i][0]==1):
        onecntr+=1
    else:
        
        if(onecntr%2==0):
            if(q[i][1]==1):
                S=q[i][2]+S
            else:
                S=S+q[i][2]
        else:
            if(q[i][1]==2):
                S=q[i][2]+S
            else:
                S=S+q[i][2]
    
if(onecntr%2==1):
    S=S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

    hanten = 0 # 偶数なら正常、 奇数なら反転状態

    for _ in range(q):
        tmp = input()
        if len(tmp) == 1:
            hanten ^= 1
        else:
            if hanten == 0:
                if tmp[2] == '1':
                    s = tmp[4] + s
                else:
                    s = s + tmp[4]
            else:
                if tmp[2] == '1':
                    s = s + tmp[4]
                else:
                    s = tmp[4] + s

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

if __name__ == '__main__':
    d = input()
    n = int(input())
    for _ in range(n):
        q = input()
        if q == '': break
        elif q.startswith('1'):
            d = d[::-1]
        elif q.startswith("2 1"):
            d = q[-1]+d
        elif q.startswith("2 2"):
            d=d+q[-1]
    print(d)

Submission status: Time Limit Exceeded

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    ret = list(map(str, sys.stdin.readline().rstrip()))
    Q = int(sys.stdin.readline().rstrip())

    for q in range(Q):
        line = sys.stdin.readline().split()
        if line[0] == '1':
            ret.reverse()
        elif line[0] == '2' and line[1] == '1':
            ret.insert(0, line[2])
        else:
            ret.append(line[2])

    print("".join(ret))



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

query =[]

for i in range(q):
	a = list(map(str,input().split()))
	a[0] = int(a[0])
	
	if len(a) == 1:
		if a[0] == 1:
			s.reverse()
	else:
		a[1] = int(a[1])
		if a[0] == 2:
			if a[1] == 1:
				b = [a[2]]
				b.extend(s)
				s = b
			elif a[1] == 2:
				s.append(a[2])
ans = ""
for i in range(len(s)):
	ans += s[i]
print(ans)

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
back = []
head = []

for idx, value in enumerate(Query):
    if int(value[0]) == 1:
        cnt +=1
    else:
        if int(value[1]) == 1:
            if cnt % 2 != 0:
                back = back + [value[2]]
            else:
                head = [value[2]] + head
        else:
            if cnt % 2 != 0:
                head = [value[2]] + head
            else:
                back = back + [value[2]]
if cnt % 2 == 0:
    print("".join(head + Slist + back))
else:
    Slist.reverse()
    head.reverse()
    back.reverse()
    print("".join(back + Slist + head))
    
# head Slist last

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=input()
q=int(input())
sd=deque()
for i in range(len(s)):
    sd.append(s[i])

for i in range(q):
    que=list(map(str,input().split()))
    if int(que[0])==1:
        sd=deque(reversed(sd))
    else:
        if int(que[1])==1:
            sd.appendleft(str(que[2]))
        else:
            sd.append(str(que[2]))

ans=''.join(sd)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=input()
q=int(input())
sd=deque()
for i in range(len(s)):
    sd.append(s[i])
    
for i in range(q):
    que=list(map(str,input().split()))
    if int(que[0])==1:
        sd.reverse()
    else:
        if int(que[1])==1:
            sd.appendleft(str(que[2]))
        else:
            sd.append(str(que[2]))

ans=''.join(sd)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
S = [i for i in input()]
Q = int(input())
head = 0
# head % 2 == 1 のとき反転している

for q in sys.stdin:
  q = q.strip("\n")
  if q == "1":
    head += 1
  elif head % 2 ==0:
    if q[2] == "1":
      S.insert(0,q[4])
    else:
      S.append(q[4])
  elif head % 2 ==1:
    if q[2] == "1":
      S.append(q[4])
    else:
      S.insert(0,q[4])
if head % 2 == 1: S.reverse()
S = "".join(S)
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

first = []
last = []
reverse = 0
for _ in range(Q):
    QQ = input().split()
    if QQ[0] == '1':
        reverse = (reverse + 1) % 2
    elif QQ[1] == '1':
        if reverse == 0:
            first.insert(0, QQ[2])
        else:
            last.append(QQ[2])
    else:
        if reverse == 0:
            last.append(QQ[2])
        else:
            first.insert(0, QQ[2])

R = first + S + last

if reverse == 1:
    R.reverse()

print(''.join(R))

Submission status: Time Limit Exceeded

Solution code:
def d():
    S = str(input())
    Q = int(input())
    rvs = False

    for i in range(Q):
        q = list(input().split())

        if q[0] == '1':
            rvs = not rvs
        elif q[0] == '2':
            if q[1] == '1':
                if rvs:
                    S = S + q[2]
                else:
                    S = q[2] + S
            elif q[1] == '2':
                if rvs:
                    S = q[2] + S
                else:
                    S = S + q[2]
            else:
                raise ValueError(
                    'F({}) is {}, which is invalid!'.format(i, q[1]))
        else:
            raise ValueError('T({}) is {}, which is invalid!'.format(i, q[0]))

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


def dd(S):
    print()


##########
d()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
def main():
    S=input()
    Q=int(input())
    query=[]
    for i in range(Q):
        li = input().split(' ')
        query.append(li)
    f=True
    for i in range(Q):
        if(query[i][0]=='1'):
            f=not f
            continue
        else:
            F=query[i][1]
            C=query[i][2]
            
            if(f):
                if(F=='1'):
                    S=C+S
                else:
                    S=S+C
            else:
                C=C[::-1]
                if(F=='1'):
                    S=S+C
                else:
                    S=C+S
        
    if(not f):
        S=S[::-1]
    print(S) 



    

if __name__ == '__main__':
    main()

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:
        S=tmp[2]+S
        
      else:
        S=S+tmp[2]
        
  if cnt%2!=0:
    S=''.join(list(reversed(S)))

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

Submission status: Time Limit Exceeded

Solution code:
S = [i for i in input()]
Q = int(input())
head = 0
# head % 2 == 1 のとき反転している

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
reverse = -1
S = list(input())
Q = int(input())
for _ in range(Q):
    Query = input().split()
    if Query[0] == "1":
        reverse *= -1
    else:
        if Query[1] == "1":
            if reverse == 1:
                S.append(Query[2])
            else:
                S.insert(0, Query[2])
        else:
            if reverse == 1:
                S.insert(0, Query[2])
            else:
                S.append(Query[2])

ans = "".join(S)
if reverse == 1:
    ans = ans[::-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = input()
n = int(input())
d = deque(s)

now_reverse = False

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:
    d.append(c)
  elif f == '1' and not now_reverse:
    d.appendleft(c)
    s = "".join([c,s])
  elif f == '2' and now_reverse:
    d.appendleft(c)
  else:
    d.append(c)
  
if now_reverse:
  d = reversed(list(d))
else:
  d =list(d)

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

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:
def d():
    S = str(input())
    Q = int(input())
    rvs = False

    for i in range(Q):
        q = list(input().split())

        if q[0] == '1':
            rvs = not rvs
        elif q[0] == '2':
            if q[1] == '1':
                if rvs:
                    S = S + q[2]
                else:
                    S = q[2] + S
            elif q[1] == '2':
                if rvs:
                    S = q[2] + S
                else:
                    S = S + q[2]
            else:
                raise ValueError(
                    'F({}) is {}, which is invalid!'.format(i, q[1]))
        else:
            raise ValueError('T({}) is {}, which is invalid!'.format(i, q[0]))

    if rvs == True:
        S = S[::-1]
    print(''.join(S))


##########
d()

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

def rev2(flg):
  if flg == 0:
    flg = 1
  else:
    flg = 0
  return flg

def add(olist,ele,j,flg,head,tail):
  if j == '1' and flg == 0:
    head.insert(0,ele)
  elif j == '1' and flg == 1:
    tail.append(ele)
  elif j == '2' and flg == 0:
    tail.append(ele)
  elif j == '2' and flg == 1:
    head.insert(0,ele)

flg = 0
head = []
tail = []

for n in range(Q):
  
  if q[n][0] == '1':
    flg = rev2(flg)
  
  else:
    add(S,q[n][2],q[n][1],flg,head,tail)

ans = head + S + tail

if flg == 1:
  ans = rev(ans)
  
print(''.join(ans))

Submission status: Time Limit Exceeded

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

inv = False
mae = ''
usiro = ''

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

    else:
        _,f,c = tmp.split()

        if f == '1' and not inv:
            mae += c
        elif f == '2' and not inv:
            usiro += c
        elif f == '1' and inv:
            usiro += c
        elif f == '2' and inv:
            mae += c

    # print(mae,s,usiro)

ans = mae[::-1] + s + usiro

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
l_list = []
r_list = []
flag = True
for i in range(Q):
    A = list(map(str, input().split()))
    if len(A)==1:
        flag = not flag
    else:
        if flag:
            if A[1]=="1":
                l_list.insert(0, A[2])
            else:
                r_list.append(A[2])
        else:
            if A[1]=="1":
                r_list.append(A[2])
            else:
                l_list.insert(0, A[2])
if flag:
    print("".join(l_list)+"".join(S)+"".join(r_list))
else:
    print("".join(r_list[::-1])+"".join(S[::-1])+"".join(l_list[::-1]))

Submission status: Time Limit Exceeded

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

r = 0
for i in range(q):
  query=input().split()
  if query[0] == '1':  
    r = r ^ True
  else  :
    if query[1] == '1' and r == 0 :
      s = query[2] + s 
    elif query[1] == '1' and r == 1 :
      s = s + query[2] 
    elif query[1] == '2' and r == 0 :
      s = s + query[2] 
    elif query[1] == '2' and r == 1 :
      s = query[2] + s 

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:
in_str = input()
N = int(input())

for i in range(N):
  inst = input().split(' ')
  if len(inst) == 1:
    in_str = in_str[::-1]
  else:
    if inst[1] == '1':
      in_str = inst[2] + in_str
    else:
      in_str = in_str + inst[2]
print(in_str)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
s=deque(S)
Q=int(input())
Que=[list(map(str, input().split())) for _ in range(Q)]
#print(Que)
for i in range(Q):
    if Que[i][0]=="1":
        s.reverse()
#        s=s[::-1]
    elif Que[i][0]=="2":
        if Que[i][1]=="2":
            s.append(Que[i][2])
        elif Que[i][1]=="1":
            s.appendleft(Que[i][2])
ans="".join(s)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
cnt = 2
for _ in range(Q):
    L = input().split()
    if L[0] == "1":
        cnt += 1
    elif L[0] == "2":
        if L[1] == "1" and (cnt)%2 == 0:
            S = str(L[2]) + S
        elif L[1] == "1" and (cnt)%2 != 0:
            S =  S + str(L[2])
        elif L[1] == "2" and (cnt)%2 == 0:
            S =  S + str(L[2])
        elif L[1] == "2" and (cnt)%2 != 0:
            S = str(L[2]) + S
if cnt%2 != 0:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = list(input())
q = I()
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        s = s[::-1]
    else:
        if query[1] == "1":
            s = [query[2]] + s
        else:
            s.append(query[2])
ans = ''
for i in s:
    ans += i
print(ans)

Submission status: Time Limit Exceeded

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
nq = int(input())
flg = 0
front = []
back = []
for i in range(nq):
    q = input()
    if len(q) == 1:
        #flg += 1
        flg = (flg+1)%2
    else:
        if flg == 0:
            if q[2] == '1':
                front = [q[-1]] + front
            else:
                back = back + [q[-1]]
        else:
            if q[2] == '1':
                back = back + [q[-1]]
            else:
                front = [q[-1]] + front
                
s = ''.join(front) + s + ''.join(back)
if flg % 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]
    elif tmp[1] == '2' and not inv:
        usiro += tmp[2]
    elif tmp[1] == '1' and inv:
        usiro += tmp[2]
    elif tmp[1] == '2' and inv:
        mae += tmp[2]

    # print(mae,s,usiro)

ans = mae[::-1] + s + usiro

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

print(ans)

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 = ''.join([t[2], s])
        else:
            s = ''.join([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())
r = []
#先頭の数
t = []
#2つめ
u = []
#3つめ
v = []
for i in range(q):
    a = input()
    r.append(a)
    t.append(int(r[i][0]))
    if r[i][0] == '1':
        u.append('0')
        v.append('0')
    else:
        u.append(r[i][2])
        v.append(r[i][4])
#
if t.count(1) %2 == 1:
    s = ''.join(list(reversed(s)))


for i,j in enumerate(t):
    if j == 2:
        w = (t[i:]).count(1)
        if w % 2 == 1:
            if u[i] == '1':
                u[i] = '2'
            else:
                u[i] = '1'
        if u[i] == '1':
            s = v[i] + s
        else:
            s = s + v[i]
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 = mae + tmp[2]
    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 = mae + tmp[2]

    # print(mae,s,usiro)

ans = mae[::-1] + s + usiro

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=input()
q=int(input())
ans=deque(s)
for _ in range(q):
  que=input()
  if que[0]=='1':
    ans.reverse()
  else:
    t,f,c=map(str,que.split())
    if f=='1':
      ans.appendleft(c)
    else:
      ans.append(c)
      
print(''.join(ans))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
rev = False
for i in range(q):
  inp = input()
  if int(inp[0]) == 1:
    rev = not rev
  else:
    t, f, c = inp.split()
    if (int(f) == 1 and rev) or (int(f) == 2 and not rev):
      s += c
    else:
      s = c + s
print(s[::-1] if rev else s)