Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = int(input())
Q = [input().split() for n in range(N)]
F = False

for n in range(N-1):
  if F:
    F = False
    continue
  if int(Q[n][0])==1 and int(Q[n+1][0])==1:
    F = True
    continue
  else:
    if int(Q[n][0])==1:
      S = S[::-1]
    else:
      if int(Q[n][1])==1:
        S = Q[n][2]+S
      else:
        S = S+Q[n][2]

if int(Q[-1][0])==1:
  if F:
    print(S)
  else:
    print(S[::-1])
else:
  if int(Q[-1][1])==1:
    print(Q[-1][2]+S)
  else:
    print(S+Q[-1][2])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def get_inputs(n):
    data = [input() for _ in range(n)]
    return data

def add_w(n, letter, a):
    if n == 1:
        a = letter + a
    else:
        a = a + letter
    return a

def seq(query, a):
    data = query.split()
    if int(data[0]) == 1:
        a = a[::-1]
    else:
        n = int(data[1])
        letter = data[2]
        a = add_w(n, letter, a)
    return a

a = input()
num = int(input())

data = get_inputs(num)

for d in data:
    a = seq(d, a)
print(a)

Submission status: Time Limit Exceeded

Solution code:
S = input()

Q = int(input())

for i in range(Q):
  instraction = list(map(str, input().split()))
  if instraction[0] == '1':
    S = S[::-1]
  else:
    if instraction[1] == '1':
      S = instraction[2]+S
    else:
      S = S+instraction[2]

print(S)

Submission status: Time Limit Exceeded

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

ans = s
for i in range(q):
    qret = list(input().split())
    if qret[0] == "1":
        ans = ans[::-1]
    else:
        f = qret[1]
        c = qret[2]
        if f == "1":
            ans = c + ans
        else:
            ans += c

print(ans)

Submission status: Time Limit Exceeded

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

for _ in range(q):
    li=input().split()

    if li[0]=="1":
        an.reverse()
    else:
        if li[1]=="1":
            an[0:0]=li[2]
        else:
            an.append(li[2])

print(*an,sep="")

Submission status: Time Limit Exceeded

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

topFlg = True

for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        topFlg = not (topFlg)
    else:
        if query[1] == "1":
            S = query[2] + S if topFlg else S + query[2]
        else:
            S = S + query[2] if topFlg else query[2] + S

if not topFlg:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
# Problem D - String Formation

# input process
S = input()
Q = int(input()) # query number

# initialization
sento = 1

# process
for i in range(Q):
    query_num = input()
    query_1 = int(query_num[0])
    # クエリが１の時
    if query_1==1 and sento==1:
        sento = 10
    elif query_1==1:
        sento = 1
    else: # query num 2
        n, f, c = query_num.split()
        f = int(f)
        if f==1: # 先頭追加処理
            # 先頭の位置によって変わる
            if sento==1:
                S = c + S
            else:
                S = S + c
        else: # 末尾追加処理
            if sento==1:
                S = S + c
            else:
                S = c + S

# output process
if sento==1:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
inital_string = input()
num_of_q = int(input())

for i in range(num_of_q):
  q = input()
  if q[0] == '1':
    inital_string = inital_string[::-1]
  else:
    if q[2] == "1":
      inital_string = q[4] + inital_string
    else:
      inital_string = inital_string + q[4]
      
print(inital_string)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for c in range(0,iteration):
    sousa = input().split()
    if sousa[0] == "1":
        s = s[:: -1]
    elif sousa[1] == "1":
        s = sousa[2] + s
    else:
        s = s + sousa[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from sys import stdin
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)

'''

'''

def solve(l):
	n = len(l)
	

S = input()
a,b = S,S[::-1]
Q = int(stdin.readline())
zz = 0
for _ in range(Q):
	l = list(map(str, stdin.readline().rstrip().split()))
	if(l[0] == '1'):
		a,b = b,a
		zz += 1
	else:
		q,f,c = l
		f = int(f)
		if(f == 1):
			b += c
			a = c + a
		else:
			a += c
			b = c + b
	# print("*",a,b)

print(a)
# for i in b:
# 	print(i)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
for i in range(n):
    query = [str(j) for j in 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:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            T1 += 1
        else:
            if T1 % 2 == 0:
                # これは元通り
                
                s = FrontS + s + BackS
                FrontS = ""
                BackS = ""

                if T[1] == "1":
                    FrontS = T[2] + FrontS
                else:
                    BackS += T[2]
            else:
                # これは逆向き

                s = FrontS + s + BackS
                FrontS = ""
                BackS = ""
                
                if T[1] == "1":
                    BackS = T[2] + BackS
                else:
                    FrontS = T[2] + FrontS
    
    if BackS != "":
        s += BackS
    if FrontS != "":
        s = FrontS + s

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    q = input()
    if len(q) == 1:
        S = list(S)
        S.reverse()
        S = "".join(S)
    else:
        T, F, C = q.split()
        if int(F) == 1:
            S = C + S
        else:
            S = S + C

print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
    tmp = input()
    if tmp == '1':
        s = s[::-1]
    else:
        t1,t2,t3 = tmp.split()
        if int(t2) == 1:
            s = t3 + s
        if int(t2) == 2:
            s = s + t3

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
g = 0
for i in range(q):
  query = input()
  if (query == "1"):
    g = 1-g
  else:
    queries = query.split()
    f = int(queries[1])-1
    c = queries[2]
    if (g == f):
      s = "".join([c,s])
    else:
      s = "".join([s,c])
if (g == 1):
  s = "".join(list(s)[::-1])
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
def i():return int(sys.stdin.readline().replace("\n",""))
def i2():return map(int,sys.stdin.readline().replace("\n","").split())
def s():return str(sys.stdin.readline().replace("\n",""))
def l():return list(sys.stdin.readline().replace("\n",""))
def intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def strl():return [str(k) for k in sys.stdin.readline().replace("\n","").split()]
def lx():return list(map(lambda x:int(x)*-1,sys.stdin.readline().replace("\n","").split()))
def t():return tuple(map(int,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":pass
s = l()
n = i()
for i in range(n):
    a = strl()
    if len(a) == 1:
        s = s[::-1]
    else:
        if a[1] == "1":
            s.insert(0,a[2])
        else:
            s.append(a[2])
print("".join(s))

Submission status: Time Limit Exceeded

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


for i in range(Q):
    query=input().split()
    if "1"==query[0]:
        S=S[::-1]


    else:
        if query[1]=="1":
            S=S[::-1]
            S+=query[2]
            S=S[::-1]

        else:
            S+=query[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
  qn = input()
  if len(qn) == 1:
    s = s[::-1]
  else:
    if qn[2] == '1':
      s = qn[4] + s
    else:
      s = s + qn[4]
print(s)

Submission status: Time Limit Exceeded

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

qs = []
for i in range(q):
    qs.append(input().split(' '))

rev = False
for i in range(q):

    if qs[i][0] == '1':
        rev = not rev
    else:
        if qs[i][1] == '1' and rev:
            s.append(qs[i][2] )
        elif qs[i][1] == '1' and not rev:
            s.insert(0, qs[i][2])
        elif qs[i][1] == '2' and rev:
            s.insert(0, qs[i][2])
        elif qs[i][1] == '2' and not rev:
            s.append(qs[i][2] )
            
s = ''.join(map(str, s))
if rev:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = input().rstrip('\r\n')
S_list = deque(S)
Q = int(input())
for _ in range(Q):
    query = input().rstrip('\r\n')
    t, f, c = '', '', ''
    if len(query) == 1:
        t = query
    else:
        t, f, c = query.split(' ')
    if t == '1':
        S_list.reverse()
    else:
        if f == '1':
            S_list.appendleft(c)
        else:
            S_list.append(c)
print(''.join(S_list))

Submission status: Time Limit Exceeded

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


def solve(S, Q, qs):
    ans = S
    for n in range(Q):
        if qs[n][0] == 1:
            ans = ans[::-1]
        else:
            if qs[n][1] == 1:
                ans = qs[n][2] + ans
            else:
                ans = ans + qs[n][2]
    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = (next(tokens))  # type: string
    Q = int(next(tokens)) # type: int
    qs = []
    for n in range(Q):
        q = [int(next(tokens))]
        if q[0] == 2:
            q.append(int(next(tokens)))
            q.append((next(tokens)))
        qs.append(q)
    solve(S, Q, qs)

if __name__ == '__main__':
    main()

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    a = input()
    
    if a[0] == '1':
        rev += 1
    else:
        if (int(a[2]) + rev) % 2 == 1:
            s = "{}{}".format(a[4],s)
        else:
            s = "{}{}".format(s,a[4])
    
if rev%2 == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S1 = input()
Q = int(input())
L = []
S = S1
for i in range(Q):
    q = input()
    if q == '1':
        L.append(q)
    else:
        L.append(q)
for i in range(Q):
    if L[i] == '1':
        S = S[::-1]  
    else:
        l = L[i].split()
        if l[-2] == '1':
            S = l[-1] + S
        elif l[-2] == '2':
            S += l[-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
qry = [input().split() for _ in range(Q)]
A = ''
B = ''
oq = 0
cnt = 0
for q in qry:
    if q[0] == '1':
        cnt += 1
        if oq == '1':
            continue
        A, B = B[::-1], A[::-1]
    else:
        if q[1] == '1':
            A = q[2] + A
        else:
            B += q[2]

if cnt % 2 == 1:
    S = S[::-1]

print(A + S + B)

Submission status: Time Limit Exceeded

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

t = 0
for i in range(q):
    query = input()
    if query == '1':
        if t == 1:
            t = 0
        else:
            t = 1
    else:
        if query[2] == '1':
            if t == 0:
                s = query[4]+s
            else:
                s = s+query[4]
        else:
            if t == 0:
                s = s+query[4]
            else:
                s = query[4] + s

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

Submission status: Time Limit Exceeded

Solution code:
# 入力文字列
get_str = input()

# 操作回数
N = int(input())

# リバースフラグ
r_flag = 0

# 操作
for i in range(N):
    # 取得操作
    inp_cmd = input().split()
    # inp_cmd[0] = 1 ならリバースフラグ更新
    if(int(inp_cmd[0]) == 1):
        r_flag = (r_flag+1) % 2
    else:
        # inp_cmd[1]=1、かつリバースフラグなし
        # inp_cmd[1]=2、かつリバースフラグあり
        # 、先頭に文字追加
        if((int(inp_cmd[1]) == 1 and r_flag == 0) or (int(inp_cmd[1]) == 2 and r_flag == 1)):
            get_str = inp_cmd[2] + get_str
        # 上記以外は後ろに追加
        else:
            get_str = get_str + inp_cmd[2]
# リバースフラグあり
if(r_flag == 1):
    get_str = get_str[::-1]

print(get_str)

Submission status: Time Limit Exceeded

Solution code:
#At coder 2020/3/8 C
import math
S=input()
Q=int(input())

jj=0
for ii in range(Q):
    aa=list(map(str,input().split())) 
    if '1'==aa[0]: 
        if jj==1:
            jj=0
        else:
            jj=1
    else:
        if ('1'==aa[1] and jj==0) or ('2'==aa[1] and jj==1):
                S=aa[2]+S
        else:
                S=S+aa[2]
if jj==0:
    ans=S
else:
    ans=S[::-1]
    
print(ans)

Submission status: Time Limit Exceeded

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

lis=[]
d=deque()
for _ in S:
  lis.append(_)

for _ in range(Q):
  s = list(sys.stdin.readline())
  if s[0]=="1":
    lis=lis[::-1]
  else:
    if s[2]=="1":
      d=deque(lis)
      d.appendleft(s[4])
      lis=list(d)
    else:
      lis.append(s[4])
      
print("".join(lis))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def resolve_d():
    s = np.array(list(input()))
    q = int(input())
    l = [input().split() for i in range(q)]
    for ope_l in l:
        if ope_l[0] == '1':
            s = s[::-1]
        else:
            if ope_l[1] == '1':
                s = np.append(ope_l[2], s)
            elif ope_l[1] == '2':
                s = np.append(s, ope_l[2])
    print(''.join(s))
resolve_d()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math as m

def main(**kwargs):
    front = ""
    back = ""
    front_i = ""
    back_i = ""
    now = "f"

    for q in qs:
        if q[0] == "1":
            if now == "b":
                now = "f"
            else:
                now = "b"
        else:
            if (q[1] == "1" and now == "f") or (q[1] == "2" and now == "b"):
                front += q[2]
                front_i = q[2] + front_i
            else:
                back += q[2]
                back_i = q[2] + back_i

    if now == "f":
        res = front_i + s + back
    else:
        res = back_i + s[::-1] + front
        
    return res

if __name__ == "__main__":
    cin = np.array(input().split(" "))
    s, *_ = cin
    cin = np.array(input().split(" ")).astype("int")
    n, *_ = cin

    qs = []

    for i in range(n):
        cin = np.array(input().split(" "))
        qs.append(list(cin))
    
    cout = main(s=s, n=n, qs=qs)
    print(cout)

Submission status: Time Limit Exceeded

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
for i in range (n):
    q=input().split()
    #文字列のリストとして受け取る
    if q[0]=='1':
        l=list(reversed(s))
        s=''.join(l)
    else:
        if q[1]=='1':
            #Fi=1のとき
            s=q[2]+s
        elif q[1]=='2':
            s=s+q[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
queries = [input() for _ in range(Q)]


for q in queries:
    if q[0] == "1":
        S =S[::-1]
    else:
        if q[2] == "1":
            S.insert(0,q[4])
        else:
            S.append(q[4])


print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
syoki_str = input()
syori_cnt = int(input())

for i in range(syori_cnt):
    line = input().split(" ")
    if line[0] == "1":
        new_str = ''.join(list(reversed(syoki_str)))
        syoki_str = new_str
    else:
        if line[1] == "1":
            syoki_str = line[2] + syoki_str
        else:
            syoki_str = syoki_str + line[2]

print(syoki_str)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys 
from collections import deque
s = deque(list(input()))
q = int(input())
cnt = 0
l = []
# for i in range(q):
#     l.append(list(input().split()))
# prev = l[0][0]
# for a in l:
#     if a[0] == '1':
#         cnt += 1
#     else:
#         if cnt >= 1:
#             if cnt%2 != 0:
#                 s.reverse()
#         cnt = 0
#         if a[1] == '1':
#             s.appendleft(a[2])
#         else:
#             s.append(a[2])
#     prev = a[0]
# if cnt >= 1:
#     if cnt%2 != 0:
#         s.reverse()
# print(''.join(list(s)))

for i in range(q):
    a = list(input().split())
    if i == 0:
        prev = a[0]
    if a[0] == '1':
        cnt += 1
    else:
        if cnt >= 1:
            if cnt%2 != 0:
                s.reverse()
        cnt = 0
        if a[1] == '1':
            s.appendleft(a[2])
        else:
            s.append(a[2])
    prev = a[0]
if cnt >= 1:
    if cnt%2 != 0:
        s.reverse()
print(''.join(list(s)))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
qnum = int(input())
for i in range(qnum):
    t = input()
    if t=="1":
        s.reverse()
    else:
        tlist=t.split()
        if tlist[1] == "1":
            s.insert(0,tlist[2])
        else:
            s.append(tlist[2])
ans = ''.join(s)
print(ans)

Submission status: Time Limit Exceeded

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

# 文字列格納リスト
#ans = [S]

# クエリ格納リスト
q_s = []

for i in range(Q):
  _q = list(map(str, input().split()))
  if _q[0] == "2":
    q_s.append(_q)
  elif _q[0] == "1":
    if len(q_s) > 0:
      if q_s[-1][0] == "1":
        del q_s[-1]
      else:
        q_s.append(_q)
    elif len(q_s) == 0:
      q_s.append(_q)
for i in q_s:
  if i[0] == "1":
    ans.reverse()
  elif i[0] == "2":
    if i[1] == "1":
      # 前に追加
      ans.insert(0,i[2])
    elif i[1] == "2":
      # 後ろに追加
      ans.append(i[2])
print("".join(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
Q = int(input())
rev = 0
for i in range(Q):
    query = input()
    ti = list(query)[0]
    if ti == '1':
        rev = (rev + 1) % 2
    else:
        ti, fi, ci = query.split()
        if (int(fi) + rev) == 2:
            s.append(ci)
        else:
            s.insert(0, ci)
if rev == 1:
    s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import copy

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

Que = [0]* Q
for i in range(Q):
    Que[i] = list(map(str, input().split()))

answer = copy.copy(S)
check = 1
for i in range(Q):
    T = Que[i][0]
    if int(T) == 1:
        check *= -1
    else:
        F = Que[i][1]
        if (int(F) == 1 and check == 1) or (int(F) == 2 and check == -1):
            answer = Que[i][2] + answer
        else:
            answer = answer + Que[i][2]
if check == -1:
    answer = ''.join(list(reversed(answer)))
print(answer)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for i in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == "1":
        S.reverse()
    elif Query[1] == "1":
        S.insert(0, Query[2])
    else:
        S.append(Query[2])
print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

s = str(input()).strip()
Q = int(input())

turn=0
for _ in range(Q):
    q = str(input())
    if len(q)!=6:
        turn = (turn+1)%2
    else:
        # print("!!",q,'len',len(q))

        f = q[2]
        c = q[4]
        # print('fc',f,c)
        # _, f,c = q.split()
        if turn == 0:
            if f =='1':
                s=c+s
            elif f =='2':
                s=s+c
        else:
            if f =='1':
                s=s+c
            elif f =='2':
                s=c+s
        # s.append(c)
    # print(s)
if turn == 0:        
    print(s.strip())
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline
s = str(input())
q = int(input())
qmap = [["0"] * 3 for _ in range(q)]
count = 0
for i in range(q):
    a = input().split()
    for j in range(len(a)):
        qmap[i][j] = a[j]

for i in range(q):
    if qmap[i][0] == "1":
        count += 1
    else:
        if count % 2 == 0:
            if qmap[i][1] == "1":
                s = str(qmap[i][2].rstrip()) + s.rstrip()
            else:
                s = s.rstrip() + str(qmap[i][2].rstrip())
        else:
            if qmap[i][1] == "1":
                s = s.rstrip() + str(qmap[i][2].rstrip())
            else:
                s = str(qmap[i][2].rstrip()) + s.rstrip()


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

Submission status: Time Limit Exceeded

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

for i in range(n):
    a = input()
    if a == "1":
        s = s[::-1]
    else:
        b,c,d = a.split()
        if c == "1":
            s = d + s
        else:
            s = s + d

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
is_reversed = False
 
for i in range(Q):
    input_val = input().split()
    if input_val[0] == "1":
        is_reversed = not is_reversed
        continue
 
    if input_val[1] == "1":
        if not is_reversed:
            S = input_val[2] + S
        else:
            S = S + input_val[2]
    else:
        if not is_reversed:
            S = S + input_val[2]
        else:
            S = input_val[2] + S

if is_reversed:
    print("".join(reversed(S)))
else:
    print(S)

Submission status: Time Limit Exceeded

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

reverseCount = 0
for count in range(Q):
    T = list(map(str,input().split()))
    if int(T[0]) == 1:
        reverseCount +=1
    else:
        if reverseCount > 0:
            if reverseCount % 2  == 1:
                reverseCount = 0
                S = S[::-1]
            else:
            	reverseCount = 0
        if int(T[1]) == 1:
            S = T[2] + S
        else:
            S = S + T[2]

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
S = (stdin.readline().rstrip())
Q = int(stdin.readline().rstrip())
cnt = 0
ccnt = 0
s = "@"
for i in range(Q):
    data = [(x) for x in stdin.readline().rstrip().split()]
    if data[0] == "2":
        ccnt = 0
        if data[1] == "1":
            if cnt % 2 == 0:
                s = "".join([data[2],s])
            else:
                s = "".join([s,data[2]])  
        else:
            if cnt % 2 == 0:
                s = "".join([s,data[2]])
            else:
                s = "".join([data[2],s])
    else:
        cnt += 1
if cnt % 2 != 0:
    s = s[::-1]
if cnt % 2 != 0:
    S = S[::-1]
ans = s.replace("@",S)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
pre = []
suf = []
for i in range(Q):
    q = input().split()
    if len(q) == 1:
        pre, suf = suf[::-1], pre[::-1]
        S = S[::-1]
    else:
        if q[1] == '1':
            pre = pre[::-1]
            pre.append(q[2])
            pre = pre[::-1]
        else:
            suf.append(q[2])
print(''.join(pre) + S + ''.join(suf))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(0, q):
    query = input()
    if query[0] == '1':
        s_l = list(s)
        s_l.reverse()
        s = "".join(s_l)
    else:
        if query[2] == '1':
            # 先頭
            s = '{}{}'.format(query[4],s)
        else:
            # 末尾
            s = '{}{}'.format(s, query[4])
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]

ans=deque(S)
for i,q in enumerate(query):
  if len(q)==1:
    tmp=''
    for a in ans:
      tmp+=a
    ans=deque(tmp[::-1])
  else:
    _,f,c=q
    if f=='1':
      ans.appendleft(c)
    else:
      ans.append(c)
print(*ans,sep='')

Submission status: Time Limit Exceeded

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

li = [input() for i in range(q)]

k= 0

for i in range(q):
    if li[i] == "1":
        k += 1
    else:
        li3 = li[i].split()
        if (int(li3[1])+k) % 2 == 0:
            s = s + li3[2]
        else:
            s = li3[2] + s

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=list(str(input()))
s=deque(s)
q=int(input())
cnt=0
for i in range(q):
    que=list(input().split())
    if len(que)==1:
        cnt+=1
    else:
        if cnt%2!=0:
            s.reverse()
        if int(que[1])==1:
            s=deque(s)
            s.appendleft(que[2])
            cnt=0
        elif int(que[1])==2:
            s=deque(s)
            s.append(que[2])
            cnt=0
if cnt%2!=0:
    s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

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

ans = S

for i in range(Q) :
  Qn = list(map(str, input().split()))
  
  if Qn == ["1"] :
    ans = ans[::-1]
  else :
    if Qn[1] == "1" :
      ans = Qn[2] + ans
    if Qn[1] == "2" :
      ans = ans + Qn[2]

print(ans)

Submission status: Time Limit Exceeded

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