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:
x = input()
n = int(input())
y = 0

for i in range(n):
    a = input()
    if a[0] == "1":
      x = x[::-1]
    else:
      a, b, c = a.split()
      a = int(a)
      b = int(b)

      if b == 2:
          x = x + c
      elif b == 1:
          x = x[::-1] + c
          x = x[::-1]

print(x)

Submission status: Time Limit Exceeded

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

if(change_flag == -1):
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

for i in range(n):
    a = input()
    if a[0] == "2":
      a, b, c = a.split()
      a = int(a)
      b = int(b)

      if b == 1:
          x = x[::-1] + c
          x = x[::-1]
      elif b == 2:
          x = x + c
    else:
        x = x[::-1]

print(x)

Submission status: Time Limit Exceeded

Solution code:
s = input()
num = int(input())
for i in range(num):
    a = input().split(" ")
    if(a[0] == "1"):
        s = s[::-1]
    else:
        if(a[1] == "1"):
            s = a[2] + s
        else:
            s = s + a[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
num = int(input())
for i in range(num):
    a = input().split(" ")
    a[0] = int(a[0])
    if(a[0] == 1):
        s = s[::-1]
    elif(a[0] == 2):
        a[1] = int(a[1])
        if(a[1] == 1):
            s = a[2] + s
        elif(a[1] == 2):
            s = s + a[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3 ## -*- python -*-

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

for i in range(q):
    d = input()
    if d[0] == "1":
        s = s[::-1]
    else: # 2
        dd = d.split()
        f = int(dd[1])
        c = dd[2]
        if f == 1:
            s = c + s
        else: # f == 2
            s = s + c

print(s)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3 ## -*- python -*-

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

for i in range(q):
    d = input()
    if d[:1] == "1":
        if not s[:1] * len(s) == s:
            s = s[::-1]
    else: # 2
        dd = d.split()
        f = int(dd[1])
        c = dd[2]
        if f == 1:
            s = c + s
        else: # f == 2
            s = s + c

print(s)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3 ## -*- python -*-

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

for i in range(q):
    d = input()
    if d[:1] == "1":
        s = s[::-1]
    else: # 2
        dd = d.split()
        f = int(dd[1])
        c = dd[2]
        if f == 1:
            s = c + s
        else: # f == 2
            s = s + c

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = int(input())
direct = 0
for i in range(N):
  X = input().split()
  if len(X) == 1:
    direct = 1 - direct
  else:
    if (X[1] == "1" and direct == 0) or (X[1] == "2" and direct == 1):
      S = X[2] + S
    else:
      S += X[2]
print(S) if direct == 0 else print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = int(input())
direct = 0
for i in range(N):
  X = input().split()
  if len(X) == 1:
    direct = 1 - direct
  else:
    if (X[1] == "1" and direct == 0) or (X[1] == "2" and direct == 1):
      S = X[2] + S
    else:
      S = S + X[2]
print(S) if direct == 0 else print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = int(input())
for i in range(N):
  X = input().split()
  if len(X) == 1:
    NS = S[::-1]
  else:
    if X[1] == "1":
      NS = X[2] + S
    else:
      NS = S + X[2]
  S = NS
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
for q in range(int(input())):
    T = input().rstrip().split()
    if T[0] == "1":
        S = S[::-1]
    else:
        F,C = T[1],T[2]
        if F == "1":
            S = [C] + S
        else:
            S += [C]
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
S = input()
for q in range(int(input())):
    T = input().rstrip().split()
    if T[0] == "1":
        S = S[::-1]
    else:
        F,C = T[1],T[2]
        if F == "1":
            S = C + S
        else:
            S += C
print(S)

Submission status: Time Limit Exceeded

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

rev=1

sf=[]
sb=[]

for _ in range(0,q):
  n=input().split()
  if n[0]=="1":
    rev*=-1
  else:
    if (n[1]=="1" and rev==1) or (n[1]=="2" and rev==-1):
      sf.insert(0,n[2])
    else:
      sb.append(n[2])

s=''.join(sf)+s+''.join(sb)

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
# print("Q=")
Q = int(input())

def problem_gksn(S,Q):
  check_reverse = 0 # 偶数のとき正常, 奇数の時反転
  for _ in range(Q):
    # print("queryを入力してください")
    query = input().split()
    # print("query= ",query)
    if len(query) == 1: #1の場合
      # print("文字を反転させます")
      check_reverse += 1
      # S = S[::-1]
    else: #2の場合
      # print("文字をくっつけます")
      if check_reverse % 2 == 0: #反転していない時:
        if int(query[1]) == 2:
          S = S + query[2]
        else:
          S = query[2] + S
      else: #反転している時
        if int(query[1]) == 1:
          S = S + query[2]
        else:
          S = query[2] + S
  if check_reverse % 2==1:
    S = S[::-1]
  return S
print(problem_gksn(S,Q))

Submission status: Time Limit Exceeded

Solution code:
S = input()
# print("Q=")
Q = int(input())

def problem_gksn(S,Q):
  for _ in range(Q):
    # print("queryを入力してください")
    query = input().split()
    # print("query= ",query)
    if len(query) == 1: #1の場合
      # print("文字を反転させます")
      S = S[::-1]
    else: #2の場合
      # print("文字をくっつけます")
      if int(query[1]) == 2:
        S = S + query[2]
      else:
        S = query[2] + S
  
  return S
print(problem_gksn(S,Q))

Submission status: Time Limit Exceeded

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

lr = 'left'

def reverse_list(lr):
    if lr == 'right':
        return 'left'
    else:
    	return 'right'

def moji_tuika(S, f, c, lr):
    if f == 1 and lr == 'left':
        return c + S
    elif f == 1 and lr == 'right':
        return S + c
    elif f == 2 and lr == 'left':
        return S + c
    else:
        return c + S
q = []
for i in range(Q):
    temp = input().split()
    if len(temp) == 1:
        q.append(1)
    else:
        q.append(temp)

for i in q:
    if type(i) == int:
        lr = reverse_list(lr)
    elif int(i[0]) == 2:
        S = moji_tuika(S,int(i[1]),i[2],lr)

if lr == 'right':
    S = list(S)
    S.reverse()
    S = ''.join(S)


print(S)

Submission status: Time Limit Exceeded

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

def reverse_list(S):
    S = list(S)
    S.reverse()
    return ''.join(S)

def moji_tuika(S, f, c):
    if f == 1:
        return c + S
    else:
        return S + c

q = []
for i in range(Q):
    temp = input().split()
    if len(temp) == 1:
        q.append(1)
    else:
        q.append(temp)

for i in q:
    if type(i) == int:
        S = reverse_list(S)
    elif int(i[0]) == 2:
        S = moji_tuika(S,int(i[1]),i[2])

print(S)

Submission status: Time Limit Exceeded

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

def reverse_list(S):
    S = list(S)
    S.reverse()
    return ''.join(S)

def moji_tuika(S, f, c):
    if f == 1:
        return c + S
    else:
        return S + c

q = []
for i in range(Q):
    temp = input().split()
    if len(temp) == 1:
        q.append(1)
    else:
        t = int(temp[0])
        f = int(temp[1])
        c = temp[2]
        q.append((t,f,c))

for i in q:
    if type(i) == int:
        S = reverse_list(S)
    elif i[0] == 2:
        S = moji_tuika(S,i[1],i[2])

print(S)

Submission status: Time Limit Exceeded

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

def reverse_list(S):
    if len(S) == 1:
        return S
    else:
        return S[::-1]

def moji_tuika(S, f, c):
    if f == 1:
        return c + S
    else:
        return S + c

q = []
for i in range(Q):
    temp = input().split()
    if len(temp) == 1:
        q.append(1)
    else:
        t = int(temp[0])
        f = int(temp[1])
        c = temp[2]
        q.append((t,f,c))

for i in q:
    if type(i) == int:
        S = reverse_list(S)
    elif i[0] == 2:
        S = moji_tuika(S,i[1],i[2])

print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
d = deque()
length = 0
for x in input():
    d.append(x)
    length += 1
Q = int(input())
f = 0
for _ in range(Q):
    l = list(input().split())
    if l[0] == '1':
        f ^= 1
    else:
        length += 1
        if f == 0: 
            if l[1] == '2': d.append(l[2])
            else: d.appendleft(l[2])
        else:
            if l[1] == '2': d.appendleft(l[2])
            else: d.append(l[2])
ans = ''
for x in range(length):
    ans += d.pop()
if f == 0:
    print(ans[::-1])    
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
q = int(input())
sw = 1
for i in range(q):
  e = list(input().split())
  t = int(e[0])

  if t == 1:
    sw = sw * -1
  elif t == 2:
    f = int(e[1])
    if (sw == 1 and f == 1) or (sw == -1 and f == 2):
      S = e[2] + S
    elif (sw == -1 and f == 1) or (sw == 1 and f == 2):
      S = S + e[2]

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
flag = 0
while q>0:
  lt = [x for x in input().split()]
  if int(lt[0]) == 1:
    flag = (flag + 1)%2
  else:
    if int(lt[1]) == 1 and flag==0:
      s = lt[2] + s
    elif int(lt[1]) == 1 and flag==1:
      s = s + lt[2]
    elif int(lt[1]) == 2 and flag==1:
      s = lt[2] + s 
    else:
      s = s + lt[2]
  q = q-1
  
if flag == 0:
    print(s)
else:
    print("".join(reversed(s)))

Submission status: Time Limit Exceeded

Solution code:
S = input()
q = int(input())
sw = 1
for i in range(q):
  e = list(map(str, input().split()))
  t = int(e[0])

  if t == 1:
    sw = sw * -1
  elif t == 2:
    f = int(e[1])
    if (sw == 1 and f == 1) or (sw == -1 and f == 2):
      S = e[2] + S
    elif (sw == -1 and f == 1) or (sw == 1 and f == 2):
      S = S + e[2]

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
q = int(input())
Q = []
sw = 1
for i in range(q):
  e = list(map(str, input().split()))
  Q.append(e)
  t = int(e[0])

  if t == 1:
    sw = sw * -1
  elif t == 2:
    f = int(e[1])
    if (sw == 1 and f == 1) or (sw == -1 and f == 2):
      S = e[2] + S
    elif (sw == -1 and f == 1) or (sw == 1 and f == 2):
      S = S + e[2]

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

print(S)

Submission status: Time Limit Exceeded

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

while q>0:
  lt = [x for x in input().split()]
  if int(lt[0]) == 1:
    s = "".join(reversed(s))
  else:
    if int(lt[1]) == 1:
      s = lt[2] + s
    else:
      s = s + lt[2]
  
  q = q-1
  
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flg = 0
for i in range(Q):
    A = list(input().split())
    if int(A[0]) == 1 and flg == 0:
        flg = 1
        continue
    elif int(A[0]) == 1 and flg == 1:
        flg = 0
        continue
    if int(A[1]) == 1:
        if flg == 0:
            S = A[2] + S
        else:
            S = S + A[2]
    else:
        if flg == 0:
            S = S + A[2]
        else:
            S = A[2] + S
if flg == 0:
    print(S)
if flg == 1:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np

def main():
	s = input()
	n = int(input())
	f = 0
	l = ""
	r = ""
	for i in range(n):
		q=input().split()
		if q[0] == "1":
			f ^= 1
		else:
			if (f == 0 and q[1] == "1") or (f == 1 and q[1] == "2"):
				l = l + q[2]
			else:
				r = r + q[2]
	if f == 0:
		ans = l[::-1] + s + r
	else:
		ans = r[::-1] + s[::-1] + l
	print(ans)


if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
f=0
l=""
r=""
for i in range(n):
	q=input().split()
	if q[0]=="1":
		f^=1
	else:
		if (f==0 and q[1]=="1") or (f==1 and q[1]=="2"):
			l=l+q[2]
		else:
			r=r+q[2]

if f==0:
	ans=l[::-1]+s+r
else:
	ans=r[::-1]+s[::-1]+l

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
  w = input()
  s = collections.deque(w)
  n = int(input())
  
  order = True
  for _ in range(n):
    q = list(input_list_str())
    if len(q) == 1:
      order = False if order else True
      continue
    t, f, c = q
    if int(f) == 1:
      if order:
        w = c + w
      else:
        w = w + c
    else:
      if order:
        w = w + c
      else:
        w = c + w
  if order is False:
    w = w[::-1]
  print(w)

    
def input_list():
  return map(int, input().split())

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

import math
import fractions
import collections
from functools import reduce
main()

Submission status: Time Limit Exceeded

Solution code:
word = input()
count = int(input())
reversed_flg = False

for i in range(count):
    line = input()
    if len(line) == 1:
        reversed_flg = not reversed_flg
    else:
        str_t, str_f, char = line.split()
        f = int(str_f)
        
        if (f == 1 and not reversed_flg) or (f == 2 and reversed_flg):
            word = char + word
        else:
            word = word + char

    if i == count - 1 and reversed_flg:
        word = word[::-1]

print(word)

Submission status: Time Limit Exceeded

Solution code:
a = str(input())
itr_num = int(input())
test =[]
status =1 

##type of valiable##
for i in range(itr_num):
  in_val =input().split()  
  test.append(in_val)

for i in test:
  if len(i)==1:
    status = status *(-1)
  else:
    if i[1] =="1":
      if status ==1:
        a = str(i[2])+a
      else:
        a = a+str(i[2])
    if i[1] =="2":
      if status ==1:
        a = a+str(i[2])
      else:
        a = str(i[2])+a

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

Submission status: Time Limit Exceeded

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

s = input().rstrip()
q = int(input())

isRev = False

for i in range(q):
    query = list(input().split())
    if len(query) == 1:
        isRev = not isRev
    else:
        if isRev and query[1] == '1':
            s = s + query[2]
        elif query[1] == '1':
            s = query[2] + s
        elif isRev and query[1] == '2':
            s = query[2] + s
        elif query[1] == '2':
            s = s + query[2]
            
if isRev:
    s = s[::-1]
        
print(s)

Submission status: Time Limit Exceeded

Solution code:
a = str(input())
itr_num = int(input())
test =[]
status =1 

##type of valiable##
for i in range(itr_num):
  in_val =input().split()  
  test.append(in_val)

for i in test:
  if len(i)==1:
    status = status *(-1)
  elif len(i)==3 and i[1] =="1" and status ==1:
    a = str(i[2])+a
  elif len(i)==3 and i[1] =="1" and status !=1:
    a = a+str(i[2])
  elif len(i)==3 and i[1] =="2" and status ==1:
    a = a+str(i[2])
  elif len(i)==3 and i[1] =="2" and status !=1:
    a = str(i[2])+a

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

Submission status: Time Limit Exceeded

Solution code:
a = str(input())
itr_num = int(input())
test =[]
status =1 
##type of valiable##
for i in range(itr_num):
  in_val =input().split()  
  test.append(in_val)

for i in test:
  if str(len(i))=="1":
    status = status *(-1)
  elif str(len(i))=="3" and str(i[1]) =="1" and status ==1:
    a = str(i[2])+a
  elif str(len(i))=="3" and str(i[1]) =="1" and status !=1:
    a = a+str(i[2])
  elif str(len(i))=="3" and str(i[1]) =="2" and status ==1:
    a = a+str(i[2])
  elif str(len(i))=="3" and str(i[1]) =="2" and status !=1:
    a = str(i[2])+a

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

Submission status: Time Limit Exceeded

Solution code:
a = str(input())
itr_num = int(input())
test =[]
status =0 
##type of valiable##
for i in range(itr_num):
  in_val =input().split()  
  test.append(in_val)

for i in test:
  if str(len(i))=="1":
    status = status +1
  elif str(len(i))=="3" and str(i[1]) =="1" and status%2 ==0:
    a = str(i[2])+a
  elif str(len(i))=="3" and str(i[1]) =="1" and status%2 ==1:
    a = a+str(i[2])
  elif str(len(i))=="3" and str(i[1]) =="2" and status%2 ==0:
    a = a+str(i[2])
  elif str(len(i))=="3" and str(i[1]) =="2" and status%2 ==1:
    a = str(i[2])+a

if status%2 ==1:
  print(a[::-1])
else:
  print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys
s = input()
Q = int(input())
rev = 1

for i in range(Q):
    query = input().split()
    if query[0] == '1':
        rev *= -1
    elif query[0] == '2':
        if(rev == 1):
            if query[1] == '1':
                s = query[2] + s
            elif query[1] == '2':
                s = s + query[2]
        if(rev == -1):
            if query[1] == '1':
                s = s + query[2]
            elif query[1] == '2':
                s = query[2] + s


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

Submission status: Time Limit Exceeded

Solution code:
import sys
s = input()
Q = int(input())
rev = 1

for i in range(Q):
    query = input().split()
    if query[0] == '1':
        rev *= -1
    elif query[0] == '2':
        if(rev == 1):
            if query[1] == '1':
                s = query[2] + s
            elif query[1] == '2':
                s = s + query[2]
        if(rev == -1):
            if query[1] == '1':
                s = s + query[2][::-1]
            elif query[1] == '2':
                s = query[2][::-1] + s


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

Submission status: Time Limit Exceeded

Solution code:
word = input()
count = int(input())

for i in range(count):
    line = input()
    if len(line) == 1:
        word = word[::-1]
    else:
        t, f, char = map(str, line.split())
        if int(f) == 1:
            word = char + word
        else:
            word = word + char

print(word)

Submission status: Time Limit Exceeded

Solution code:
a = str(input())
itr_num = int(input())
test =[]

##type of valiable##
for i in range(itr_num):
  in_val =input().split()  
  test.append(in_val)
  #print(in_val)
##print(test)

for i in test:
  #print(i)
  if str(len(i))=="1":
    a = a[::-1]
    ##print("done1")
  elif str(len(i))=="3" and str(i[1]) =="1":
    a = str(i[2])+a
    ##print("done2")
  elif str(len(i))=="3" and str(i[1]) =="2":
    a = a+str(i[2])
    ##print("done3")
print(a)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = list(input())
S = deque(S)
Q = int(input())
query = [input().split() for i in range(Q)]
for i in query:
    # if i[0] == '1':
    if len(i) == 1:
        S.reverse()
    elif i[1]=='1':
        S.appendleft(i[2])
    # elif i[0]=='2' and i[1]=='2':
    else:
        S.append(i[2])
print(''.join(list(S)))

Submission status: Time Limit Exceeded

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


for i in range(Q):
    query = input().split()
    if query[0] == '1':
        s = s[::-1]
    elif query[0] == '2':
        if query[1] == '1':
            s = query[2] + s
        elif query[1] == '2':
            s = s + query[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
d = deque()
num = 0
for i in s:
    d.append(i)
q = int(input())
for i in range(q):
        data = input().split()
        if len(data) == 1 :
            num += 1
        else:
            if num % 2 == 1:
                d.reverse()
            num = 0
            f = int(data[1])
            c = data[2]
            if f == 1:
                d.appendleft(c)
            else:
                d.append(c)
                
if num% 2 ==1:
    d.reverse()


s = list(d)
s = "".join(s)



print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
d = deque()
num = 0
for i in s:
    d.append(i)
q = int(input())
for i in range(q):
        data = input().split()
        if len(data) == 1:
            d.reverse()
        else:
            f = int(data[1])
            c = data[2]
            if f == 1:
                d.appendleft(c)
            else:
                d.append(c)

s = list(d)
s = "".join(s)



print(s)

Submission status: Time Limit Exceeded

Solution code:
s,_,*q=open(0)
a=s[:-1]
p=1
for q in q:p^=q<'2';c=q[-2]*(q>'2');f=(q<'2 2')^p;a=(f^1)*c+a+c*f
print(a[::p or-1])

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
f = True
for i in range(q):
  t = input().split()
  if int(t[0]) == 1:
    f = not f
  else:
    if int(t[1]) == 1:
      if f:
        s = [t[2]]+s
      else:
        s = s+[t[2]]
    else:
      if f:
        s = s+[t[2]]
      else:
        s = [t[2]]+s
if not f:
  s = s[::-1]
print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
S = sys.stdin.readline().rstrip()
Q = int(sys.stdin.readline().rstrip())
flip = False

for i in range(Q):
    Que = sys.stdin.readline().rstrip()
    if Que == "1":
        flip = not(flip)
    else:
        Query = Que.split()
        F = Query[1]
        if (F == "1") ^ flip:
            S = "".join([Query[2], S])
        else:
            S = "".join([S, Query[2]])
print(S if not flip else S[::-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
d = deque()
for i in s:
    d.append(i)
q = int(input())
for i in range(q):
        data = input().split()
        if len(data) == 1:
            if len(d) != 1:
                d.reverse()
        else:
            f = int(data[1])
            c = data[2]
            if f == 1:
                d.appendleft(c)
            else:
                d.append(c)
s = list(d)
s = "".join(s)
print(s)

Submission status: Time Limit Exceeded

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

iturn=0
for iq in range(1,q+1):
	t=input().split()
	if t[0]=='1':
		iturn=iturn+1   	
	else:
		iturn2=iturn%2
		if (t[1]=='1' and iturn2==0) or (t[1]=='2' and iturn2==1):
#			s=t[2]+s
			s='{}{}'.format(t[2],s)
		else:
#			s=s+t[2]
			s='{}{}'.format(s,t[2])

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

Submission status: Time Limit Exceeded

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

iturn=0
for iq in range(1,q+1):
	t=input().split()
	if t[0]=='1':
		iturn=iturn+1   	
	else:
		iturn2=iturn%2
		if (t[1]=='1' and iturn2==0) or (t[1]=='2' and iturn2==1):
			s=t[2]+s
		else:
			s=s+t[2]

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

Submission status: Time Limit Exceeded

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

iturn=0
for iq in range(1,q+1):
	t=input().split()
	if t[0]=='1':
		iturn=iturn+1   	
	else:
		if t[1]=='1':
#			s=t[2]+s
			if iturn%2==0:
				s=t[2]+s
			else:
				s=s+t[2]
		else:
#			s=s+t[2]
			if iturn%2==1:
				s=t[2]+s
			else:
				s=s+t[2]

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

Submission status: Time Limit Exceeded

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

rev=1

for _ in range(0,q):
  n=input().split()
  x=[]
  if n[0]=="1":
    rev*=-1
  else:
    if (n[1]=="1" and rev==1) or (n[1]=="2" and rev==-1):
      x.append(n[2])
      x.append(s)
      s=''.join(x)
#      s=n[2]+s
    else:
      x.append(s)
      x.append(n[2])
      s=''.join(x)
#      s=s+n[2]
if rev==1:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

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

rev=1

for _ in range(0,q):
  n=input().split()
  if n[0]=="1":
    rev*=-1
  else:
    if (n[1]=="1" and rev==1) or (n[1]=="2" and rev==-1):
      s=n[2]+s
    else:
      s=s+n[2]
if rev==1:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
q= int(input())
 
for iq in range(1,q+1):
	t=input().split()
	if t[0]=='1':
		s=s[::-1]
	else:
		if t[1]=='1':
			s=t[2]+s
#			s=f"{t[2]}{s}"
#			list1=[t[2],s]
#			s="".join(list1)
		else:
			s=s+t[2]
#			s=f"{s}{t[2]}"   
#			list2=[s,t[2]]
#			s="".join(list2)
 
print (s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q= int(input())
 
for iq in range(1,q+1):
	t=input().split()
	if t[0]=='1':
		s=s[::-1]
	else:
		if t[1]=='1':
#			s=t[2]+s
#			s=f"{t[2]}{s}"
			list1=[t[2],s]
			s="".join(list1)
		else:
#			s=s+t[2]
#			s=f"{s}{t[2]}"   
			list2=[s,t[2]]
			s="".join(list2)
 
print (s)

Submission status: Time Limit Exceeded

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

for iq in range(1,q+1):
	t=input().split()
	if t[0]=='1':
		s=s[::-1]
	else:
		if t[1]=='1':
#			s=t[2]+s
#			s=f"{t[2]}{s}"
			list1=[t[2],s]
			s="".join(list1)
		else:
#			s=s+t[2]
#			s=f"{s}{t[2]}"   
			list2=[s,t[2]]
			s="".join(list2)

print (s)

Submission status: Time Limit Exceeded

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

for iq in range(1,q+1):
	t=input().split()
	if t[0]=='1':
		s=s[::-1]
	else:
		if t[1]=='1':
			s=t[2]+s
		else:
			s=s+t[2]   
print (s)

Submission status: Time Limit Exceeded

Solution code:
string=list(input())
Q=int(input())
reverse=0
def func1(s):
    s.reverse()
    
def func2(s,F,C):
    if F=="1":
        s.insert(0,C)
        #return s
    if F=="2":
        s.append(C)
        #return s

            
for i in range(Q):
    Input=list(map(str,input().split()))
    if Input[0]=='1':
        func1(string)
        
    elif Input[0]=='2':
        func2(string,Input[1],Input[2])
for i in string:
    print(i,end="")

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
string=list(input())
Q=int(input())
def func1(s):
    s.reverse()
    
def func2(s,F,C):
    if F=="1":
        s.insert(0,C)
        #return s
    if F=="2":
        s.append(C)
        #return s

            
for i in range(Q):
    Input=list(map(str,input().split()))
    if Input[0]=='1':
        func1(string)
        
    elif Input[0]=='2':
        func2(string,Input[1],Input[2])
for i in string:
    print(i,end="")

Submission status: Time Limit Exceeded

Solution code:
string=list(input())
Q=int(input())
def func1(s):
    s.reverse()
    return s
def func2(s,F,C):
    if F=="1":
        s.insert(0,C)
        #return s
    if F=="2":
        s.append(C)
        #return s

            
for i in range(Q):
    Input=list(map(str,input().split()))
    if Input[0]=='1':
        func1(string)
        
    elif Input[0]=='2':
        func2(string,Input[1],Input[2])
for i in string:
    print(i,end="")

Submission status: Time Limit Exceeded

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

for i in range(Q):
    Que = input()
    if Que == "1":
        flip = not(flip)
    else:
        Query = Que.split()
        F = Query[1]
        C = Query[2]
        if (F == "1" and not flip) or (F == "2" and flip):
            S = "".join([C, S])
        else:
            S = "".join([S, C])
print(S if not flip else S[::-1])