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:
import numpy as np
import math as m

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

    for q in qs:
        if q[0] == "1":
            if now == "b":
                now = "f"
            else:
                now = "b"

    if now == "f":
        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
                else:
                    back += q[2]

        res = front + s + back
    else:
        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]
                else:
                    back = q[2] + back
        res = back + 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 = input()
q = int(input())
cnt = 0
for i in range(q):
    t_f_c = list(input().split())
    #反転する=先末逆転
    if t_f_c[0] == "1":
        cnt  += 1
    #反転しない
    elif t_f_c[0] == "2":
        if cnt % 2 == 0:
            if t_f_c[1] == "1":
                s = t_f_c[2] + s
            elif t_f_c[1] == "2":
                s = s + t_f_c[2]
        elif cnt % 2 == 1:
            if t_f_c[1] == "1":
                s = s + t_f_c[2]
            elif t_f_c[1] == "2":
                s = t_f_c[2] + s
if cnt % 2 == 0:
    print(s)
elif cnt % 2 == 1:
    print(s[::-1])

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=list(str(input()))
q=int(input())
r=[]
l=[]
t=0
for i in range(q):
    ll=list(map(str,input().split()))
    if ll[0]=="1":
        t+=1
    else:
        if t%2==0:
           if ll[1]=="1":
               l=[ll[2]]+l
           else:
               r=r+[ll[2]]
        if t%2==1:
           if ll[1]=="2":
               l=[ll[2]]+l
           else:
               r=r+[ll[2]]
s=l+s+r
if t%2==1:
    s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
bool = True
import sys
def input1():
	return sys.stdin.readline()[:-1]
for i in range(Q-1):
	s = input1()
	if len(s) == 1:
		bool = not bool
	else:
		t = tuple(s.split(" "))
		if t[1] == "1":
			if bool:
				S = t[2] + S
			else:
				S += t[2]
		else:
			if bool:
				S += t[2]
			else:
				S = t[2] + S

s = input()
if len(s) == 1:
	bool = not bool
else:
	if s[2] == "1":
		if bool:
			S = s[-1] + S
		else:
			S += s[-1]
	else:
		if bool:
			S += s[-1]
		else:
			S = s[-1] + S
if bool:
	print(S)
else:
	print(S[::-1])

Submission status: Time Limit Exceeded

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

rev = True
for _ in range(Q):
    s = input()
    if s[0] == "1":
        rev = not(rev)
    else:
        t = int(s[0])
        f = int(s[2])
        c = str(s[4])
        if rev:
            if f == 1:
                S = c+S
            else:
                S = S+c
        else:
            if f == 1:
                S = S+c
            else:
                S = c+S

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
num = int(input())
for _ in range(num):
    inp = list(input().split())
    if len(inp) == 1:
        S = S[::-1]
    else:
        _, f, c = inp
        if f == str(1):
            S = c + S
        else:
            S = S + c 
print(S)

Submission status: Time Limit Exceeded

Solution code:


d = input()



Q = int(input())

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

    if ls[0] == '1':
        
        d = d[::-1]

    else:
        if (ls[1]== '1'):
            d = ls[2] + d
        else:
            d = d + ls[2]



print (d)

Submission status: Time Limit Exceeded

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

T = 0
F = 2
C = 4

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

# query = [input().split() for l in range(q)]





# print(s,q,query)

cnt = 0

for i in range(q):
    query = input()

    # print(query[i])
    if query[T] == "1":
        cnt += 1
        continue

    # print(s)
    if cnt % 2 == 0:
        # print(query[i])
        if query[F] == "1":
            s = query[C] + s
            # print(s)
        else:
            s = s + query[C]

    else:
        # print(query[i])
        if query[F] == "1":
            # print(s)
            s = s + query[C]
        else:
            s = query[C] + s

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
count = 0
for i in range (q):
    t = input().split()
    if(t[0]=='2'):
        if((t[1] == '1' and count % 2 == 0 )or(t[1] == '2' and count % 2 == 1)):
            s = t[2] + s
        else:
            s = s + t[2]
    else:
        count+=1
if(count%2 == 0):
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

query_list = []
for _ in range(q):
    query_list += [input()]


def query1(s):
    s = s[::-1]
    return s

def query2_1(letters,s):
    s = letters[4] + s
    return s

def query2_2(letters,s):
    s = s +  letters[4]
    return s

for i in range(q):
    if len(query_list[i])==1:
        s = query1(s)
        # print(s)
    else:
        if query_list[i][2]=="1":
            s = query2_1(query_list[i],s)
            # print(s)
        elif query_list[i][2]=="2":
            s = query2_2(query_list[i],s)
            # print(s)

print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

d = deque(['S'])

#d = ['S']

rev = 0

for i in range(q):
    if query[i][0] == '2':
        if query[i][1] == '1':
            d.appendleft(query[i][2])
        else:
            d.append(query[i][2])
    else:
        d.reverse()
        rev += 1

ans = deque()

if rev % 2 == 1:
    #s = ''.join(list(reversed(s)))
    s = s[::-1]

while len(d):
    tmp = d.popleft()
    if tmp == 'S':
        ans.append(s)
    else:
        ans.append(tmp)

print(*ans,sep='')

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = int(input())
X = []
for i in range(N):
    X.append(input())

for i in range(N):
    if len(X[i]) == 1:
        S.reverse()
    else:
        Y = list(X[i].split())
        if int(Y[1]) == 1:
            S.insert(0, Y[2])
        else:
            S.append(Y[2])

print(''.join(S))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
S = input().rstrip()
Q = int(input().rstrip())
reversed = False
for _ in range(Q):
    l = input().rstrip()
    if l.startswith('1'):
        reversed = False if reversed else True
    else:
        _, F, C = l.split()
        if F == '1':
            if reversed:
                S = S + C
            else:
                S = C + S
        else:
            if reversed:
                S = C + S
            else:
                S = S + C
if reversed:
    ret = S[::-1]
else:
    ret = S
print(ret)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
list=[]
mojiretu=[z for z in S]
for a in range(Q):
    b=input().split()
    list.append(b)
for z in range(len(list)-1):
    if list[z]==list[z+1]=='1':
        list.replace(list[z],' ')
        list.replace(list[z+1],' ')
        z+=1
nlist=[x for x in list if x]
for c in nlist:
    if len(c)==1:
        mojiretu.reverse()
    else:
        if c[1]=='1':
            mojiretu.insert(0,c[2])
        else:
            mojiretu.append(c[2])
print(''.join(mojiretu))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = input()
q = int(input())
ls = [input().split() for _ in range(q)]

ans = deque(s)

for t in ls:
    if t[0] == '1':
        ans.reverse()
    elif t[0] == '2':
        if t[1] == '1':
            ans.appendleft(t[2])
        elif t[1] == '2':
            ans.append(t[2])

print("".join(ans))

Submission status: Time Limit Exceeded

Solution code:
S = input()
q = int(input())
Q = list()
for i in range(q):
    Q.append(list(map(str, input().split())))

flag = False
for i in range(q):
    if Q[i][0] == '1':
        flag = not flag
    elif (Q[i][1] == '1' and flag) or (Q[i][1] == '2' and not flag):
        S += Q[i][2]
    else:
        S = Q[i][2] + S

if flag:
    S = reversed(S)
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    ir = False
    for _ in range(Q):
        Query = input()
        if Query == '1':
            ir = not ir
        else:
            Query = Query.split()
            if (Query[1] == '1' and (not ir)) or (Query[1] == '2' and ir):
                S = ''.join([Query[2], S])
            else:
                S = ''.join([S, Query[2]])

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

main()

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = int(input())
t = [0] * N
f = [0] * N
c = [0] * N
for i in range(N):
    li = list(input().split())
    
    if li[0] == '1':
        S = S[::-1]
    else:
        if li[1] == '1':
            S[0:0] = li[2]
        elif li[1] == '2':
            S.extend(li[2])
        
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
count = 0
lr = ['', '']
for i in range(Q):
    TFC = input()
    if TFC[0] == '1':
        count += 1
    elif TFC[2] == '1':
        lr[count % 2] = ''.join([lr[count%2], TFC[4]])
    else:
        lr[(count+1) % 2] = ''.join([lr[(count+1)%2], TFC[4]])
if count % 2 == 0:
    # print(lr[0][::-1] + S + lr[1])
    print(''.join([lr[0][::-1], S, lr[1]]))
else:
    print(''.join([lr[1][::-1] , S[::-1], lr[0]]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
T=5*100001
q=[""]*T
s=200000
e=200001
for i in S:
    q[e] = i
    e+=1


Q=int(input())

for i in range(Q):
    QE=input()
    if QE=="1":
        ts=s
        te=e
        if (e-s)%2==0:
            while ts!=te:
                q[ts],q[te]=q[te],q[ts]
                ts+=1
                te-=1
        else:
            while ts<te:
                q[ts],q[te]=q[te],q[ts]
                ts+=1
                te-=1
            
    else:
        _,F,C=QE.split()
        
        if F=="1":
            q[s]=C
            s-=1
        else:
            q[e]=C
            e+=1
print("".join(q[s+1:e]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
qn = int(input())
q = [input() for i in range(qn)]

#方向判定 順方向1、逆方向-1
d = 1 

for i in q:
  if i[0] == '1':
    d = -1*d
  elif d == 1:
    if i[2] == '1':
      s = i[4]+s
    elif i[2] == '2':
      s = s+i[4]
  elif d == -1:
    if i[2] == '1':
      s = s+i[4]
    elif i[2] == '2':
      s = i[4]+s

#最終順序判定
if d == -1:
  s = s[::-1]
#
print(s)

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':
        S.reverse()
    elif i[0]=='2' and i[1]=='1':
        # S.insert(0, i[2])
        S.appendleft(i[2])
    elif i[0]=='2' and i[1]=='2':
        S.append(i[2])
print(''.join(list(S)))

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
usd = False

for _ in range(q):
    t, *fc = input().split()
    t = int(t)
    if fc:
        fc[0] = int(fc[0])

    if t == 1:
        usd = not usd
        continue
    if t == 2:
        if (fc[0] == 1 and not usd) or (fc[0] == 2 and usd):
            s = fc[1] + s
        else:
            s = s + fc[1]

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline()
S = input().strip()
Q = int(input().strip())

mode = 1

Query = [list(map(str,list(input().split()))) for i in range(Q)]

for q in Query:
  if "1" in q[0]:
    mode *= -1
    continue
  if mode == 1:
    if "1" in q[1]:
      S = ''.join([q[2].strip(),S])
    else:
      S = ''.join([S,q[2].strip()])
  else:
    if "1" in q[1]:
      S = ''.join([S,q[2].strip()])
    else:
      S = ''.join([q[2].strip(),S])

if mode == 1:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
 
def main():
    s = input()[:-1]
    q = int(input())
    tmp = [input().split() for _ in range(q)]
 
    for i in range(len(tmp)):
        if len(tmp[i]) == 1:
            s = s[::-1]
        else:
            if int(tmp[i][1]) == 1:
                s = ''.join([tmp[i][2], s])
            else:
                s = ''.join([s, tmp[i][2]])
                
    print(s)
 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
def rev_string(original_string):
  reverse_string = ""
  for a in original_string:
    reverse_string = a + reverse_string
  return reverse_string
ans = ""
l = ""
r = ""
c = 0
for _ in range(q):
  t = input().split()
  if t[0] == '1':
    c += 1
    a = l
    l = r
    r = a
  else:
    if t[1] == '1':
      l += t[2]
    else:
      r += t[2]
if c%2 ==0:
  print(rev_string(l) + s + r)
else:
  print(rev_string(l) + rev_string(s) + r)

Submission status: Time Limit Exceeded

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

rev = 0
for _ in range(q):
    query = list(input().split())
    if query[0] == "1":
        rev = 1 if rev == 0 else 0
    else:
        if query[1] == "1":
            if rev == 0:
                s = query[2] + s
            else:
                s = s + query[2]
        else:
            if rev == 0:
                s = s + query[2]
            else:
                s = query[2] + s
if rev == 1:
    new_s = ''.join(list(reversed(s)))
    print(new_s)
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():
  s=input()
  q=int(input())
  count=0
  for _ in [0]*q:
    a=list(input().split())
    if len(a)==1:
      count+=1
    else:
      if count%2==0:
        if a[1]=='1':
          l=(a[2],s)
          s=''.join(l)
        else:
          l=(s,a[2])
          s=''.join(l)
      else:
        if a[1]=='1':
          l=(s,a[2])
          s=''.join(l)
        else:
          l=(a[2],s)
          s=''.join(l)
  if count%2==1:
    s=s[::-1]
  print(s)
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for _ in [0]*q:
	k=input()
	if k[0] == "1":
		s = s[::-1]
	else:
		if k[2] == "1":
			s = "".join([k[4:],s])
		else:
			s = "".join([s,k[4:]])
print(s)

Submission status: Time Limit Exceeded

Solution code:
# 2020/03/07
# AtCoder Beginner Contest 158 - D

# Input
s = input()
q = int(input())
tcnt = 0
for i in range(q):
    l = list(input().split())
        
    if l[0] == "1":
        tcnt = tcnt + 1
    else:
        if tcnt % 2 == 0:
            if l[1] == "1":
                s = l[2] + s
            else:
                s = s + l[2]
        else:
            if l[1] == "1":
                s = s + l[2]
            else:
                s = l[2] + s

# Case odd: Reverse
if tcnt % 2 == 1:
#   ans = ''.join(list(reversed(s)))
    ans = ""
    for i in range(len(s)-1, -1, -1):
        ans = ans + s[i]
else:
    ans = s

# Output
print(ans)

Submission status: Time Limit Exceeded

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


def main():
    S = input()
    Q = int(input())
    Queries = []

    Queries = [input().split() for i in range(Q)]
    Queries = [int(q[0]) if len(q) == 1 else [int(q[0]), int(q[1]), q[2]] for q in Queries]

    cumsum_Queries_preprocessing = [1 if q == 1 else 0 for q in Queries]
    current = sum(cumsum_Queries_preprocessing)
    cumsum_Queries = list(itertools.accumulate(cumsum_Queries_preprocessing))
    cumsum_Queries = [current - cumsum_Queries[i] for i in range(Q)]

    if current % 2 == 0:
        S = S
    else:
        S = S[::-1]

    for s, q in zip(cumsum_Queries, Queries):
        if q != 1:
            if q[1] == 1:
                if s % 2 == 0:
                    S = q[2] + S
                else:
                    S += q[2][::-1]
            else:
                if s % 2 == 0:
                    S += q[2]
                else:
                    S = q[2][::-1] + S
    print(S)
    pass

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

c=0
S0=list(S)

for i in range(Q):
    if Query[i][0]=='1':
        c=c+1
    else:
        if ((Query[i][1]=='1')and(c%2==0)) or ((Query[i][1]=='2')and(c%2==1)):
            S0.insert(0, Query[i][2])
        else:
            S0.append(Query[i][2])

if c%2==0:
    print(''.join(S0))
else:
    print(''.join(reversed(S0)))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
cnt=0
for i in range(q):
    j=tuple(input().split())
    if  len(j)==3 :
        if (cnt+int(j[1]))%2:
            s=j[2]+s
        else:
            s+=j[2]
    else:
        cnt+=1
if cnt%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
#A,B = map(int,input().split())

a = input()
n = int(input())
mode = 1

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

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

print(a)

Submission status: Time Limit Exceeded

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

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

for i in range(Q):
    query = input()

    if query == '1':
        S.reverse()
    else:
        T, F, C = query.split()
        
        if F == '1':
            S.insert(0, C)
        else:
            S.append(C)

print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
rev = 0
for i in range(Q):
  query = input()
  if query == "1": rev = 1 - rev 
  elif int(query[2]) + rev == 2: S.append(query[4])
  else:
    S.reverse()
    S.append(query[4])
    rev = 1 - rev
if rev == 1: S.reverse()  
print("".join(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:
def main():
    S=input()
    for i in range(int(input())):
        T = input()
        if T == '1':
            S = S[::-1]
        else:
            ta = T.split()
            F = int(ta[1])
            C = ta[2]
            if F == 1:
                S = ''.join([C, S])
            else:
                S = ''.join([S, C])

    print(S)

main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q =int(input())
q = [input().split() for i in range(Q)]


for i in range(Q):
  if len(q[i]) == 3:
    if q[i][1] == "1":
      S = q[i][2] + S
    else: 
      S += q[i][2]
      
  else:
    S = S[::-1]
     

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
T = [input() for i in range(Q)]

count = True

for i in range(Q):
  t = T[i]
  if(int(t[0]) == 1):
    count = not count
  else:
    if(int(t[2]) == 1):
      if(count):
        S = t[4] + S
      else:
        S = S + t[4]
    else:
      if(count):
        S = S + t[4]
      else:
        S = t[4] + S

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

Submission status: Time Limit Exceeded

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

is_rev = False

for i in range(q):
    query = input().split()
    # print(query)
    if query[0] == '1':
        is_rev = not is_rev
    else:
        f1 = query[1]
        c = query[2]
        if not is_rev:
            if f1 == '1':
                s = c + s
            else:
                s += c
        else:
            if f1 == '1':
                s += c
            else:
                s = c+s


if not is_rev:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
##D
S = str(input())
Q = int(input())
Query = [str(input()) for i in range(Q)]
##print(Query)
ans = S
for j in range(len(Query)):
    if Query[j][0] == "1":
        ans = ans[::-1]
    else:
        if Query[j][2] == "1":
            ans = str(Query[j][4])+ans
        else:
            ans = ans+ str(Query[j][4])
print(ans)

Submission status: Time Limit Exceeded

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

# a = [list(map(int, list(input()))) for i in range(n)]
s = input()
q = int(input())

for i in range(0, q):
    query = input()

    if query == '1':
        s = s[::-1]
    else:
        t, f, c = query.split()

        if f == '1':
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math

s = stdin.readline().rstrip()
n = int(stdin.readline().rstrip())

flag = True
for i in range(n):
    q = stdin.readline().rstrip().split()
    if q[0] == '1':
        if flag:
            flag = False
        else:
            flag = True
    if q[0] == '2':
        if q[1] == '1':
            if flag:
                s = q[2] + s
            else:
                s = s + q[2]
        if q[1] == '2':
            if flag:
                s = s + q[2]
            else:
                s = q[2] + s

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
uerys = [list(input().split()) for i in range(Q)]
count = 0

for uery in uerys:
    if uery[0] == '1':
        count += 1
    
    if uery[0] == '2' and count % 2 == 0:
        if uery[1] == '1':
            S = uery[2] + S
        else:
            S = S + uery[2]
    elif uery[0] == '2' and count % 2 == 1:
        if uery[1] == '1':
            S = S + uery[2]
        else:
            S = uery[2] + S

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
l=[]

for i in range(n):
    l.append(list(input().split()))


def re(s):
    return s[::-1]
    
def fa(s,a):
    s1=[a,s]
    p=''.join(s1)
    return p

def la(s,a):
    s2=[s,a]
    q=''.join(s2)
    return q

for i in range(n):
    if len(l[i])==1:
        s=re(s)
    elif l[i][1]=='1':
        s=fa(s,l[i][2])
    else:
        s=la(s,l[i][2])
   
print(s)

Submission status: Time Limit Exceeded

Solution code:
import copy
s=list(input())
Q=int(input())
q=[]
for _ in range(Q):
  qq=input().split()
  qq=list(qq)
  q.append(qq)
for i in range(Q):
  if q[i][0]=='1':
    S=copy.copy(s)
    for j in range(len(S)//2):
      s[j]=S[len(s)-j-1]
      s[len(s)-j-1]=S[j]
  if q[i][0]=='2':
    if q[i][1]=='1':
      s.insert(0,q[i][2])
    else:
      s.append(q[i][2])
s=map(str,s)
s=''.join(s)
print(s)

Submission status: Time Limit Exceeded

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

for _ in range(q):
    query = input()
    if query == "1":
        queries.append({"t": 1})
    else:
        _, f, c = query.split(" ")
        queries.append({"t": 2, "f": int(f), "c": c})

i = 0
while(True):
    if queries[i]["t"] == 1:
        rev = 1
        while(True):
            if i < q-1 and queries[i+1]["t"] == 1:
                rev += 1
                i += 1
            else:
                break
        if rev % 2 == 1:
            s = s[::-1]
        i += 1
    else:
        s = queries[i]["c"] + s if queries[i]["f"] == 1 else s + queries[i]["c"]
        i += 1
    if i > q-1:
        break

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

if r is True:
    s = s[::-1]
print s

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

right=[]
left=[]
flag=0

for i in range(q):
    a = input()
    if int(a[0]) == 1:
        if flag == 0:
            flag=1
        else:
            flag=0
    else:
        if int(a[2]) == 1:
            if flag == 0:
                left.insert(0,a[4])
            else:
                right.append(a[4])
        elif int(a[2]) ==2:
            if flag ==0:
                right.append(a[4])
            else:
                left.insert(0,a[4])

right_result="".join(right)
left_result="".join(left)

if flag==0:
    print(str(left_result+s+right_result))
else:
    print(str(right_result[::-1]+s[::-1]+left_result[::-1]))