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()
q = int(input())
for _ in range(q):
    com, *query = input().split()

    if com == "1":
        s = s[::-1]
    else:
        f, c = query
        if f == "1":
            s = c + s
        else:
            s = s + c

print(s)

Submission status: Time Limit Exceeded

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

muki = 1
for i in Query:
    if i[0] == '1':
        muki *= -1
    else:
        if i[1] == '1':
            if muki == 1:
                S = i[2] + S
            else:
                S = S + i[2]
        else:
            if muki == -1:
                S = i[2] + S
            else:
                S = S + i[2]
if muki == -1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

def main():
    s = list(input())
    s = deque(s)
    q = int(input())
    for _ in range(q):
        x = list(input().split())
        if x[0] == "1":
            l = len(s)
            for i in range(l//2):
                s[i],s[-i-1] = s[-i-1],s[i]
        else:
            if x[1] == "1":
                s.appendleft(x[2])
            else:
                s.append(x[2])
    
    print("".join(s))

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def solve(inp):
    S = inp.readline().strip()
    Q = int(inp.readline().strip())
    Queries = [inp.readline().strip() for i in range(Q)]

    r = S
    dir = False
    for Query in Queries:
        if Query == "1":
            dir = not dir
        else:
            (T, F, C) = Query.split(' ')
            if dir == (F == "1"):
                r = r + C
            else:
                r = C + r

    if dir:
        r = "".join(reversed(r))

    return r


def main():
    import sys
    result = solve(sys.stdin)
    if result is not None and result != '':
        print(result)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

mae = deque()
usiro=deque()

s=input()
kai=int(input())

siji=[]*kai

for i in range(kai):
    siji.append(input())


state=1
for i in range(kai):
    si=siji[i]
    if(si[0]=='1'):
        state*=-1
    else:
        _,f,c=si.split()
        f=int(f)
        if(state==-1):
            f=3-f
        if(f==1):
            mae. appendleft(c)
        else:
            usiro.append(c)

ans=deque()
for m in mae:
    if(state==1):
        ans.append(m)
    else:
        ans.appendleft(m)
for smoji in s:
    if(state==1):
        ans.append(smoji)
    else:
        ans.appendleft(smoji)
for u in usiro:
    if(state==1):
        ans.append(u)
    else:
        ans.appendleft(u)

kotae=""
for a in ans:
    kotae+=a
print(kotae)

Submission status: Time Limit Exceeded

Solution code:
S = input()

reverse = False

Q = int(input())

frontstack = ''
backstack = ''
tmp = ''

for _ in range(Q):
    qstr = input()
    qlist = qstr.split()
    T = int(qlist[0])
    if T == 1:
        reverse = False if reverse == True else True
        tmp = frontstack
        frontstack = backstack
        backstack = tmp
    elif T == 2:
        F = int(qlist[1])
        C = qlist[2]
        if F == 1:
            frontstack += C
        elif F == 2:
            backstack += C

if reverse is False:
    ans = frontstack[::-1] + S + backstack
else:
    ans = frontstack[::-1] + S[::-1] + backstack

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
sin = stdin.readline().rstrip()
q = int(stdin.readline().rstrip())
li = [list(stdin.readline().rstrip().split()) for _ in range(q)]
for i in li:
    if i[0] == "1":
        sin = sin[::-1]
    elif i[0] == "2":
        if i[1] == "1":
            sin = i[2] + sin
        else:
            sin += i[2]
print(sin)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=deque(list(input()))
p=0
for i in range(int(input())):
    a=list(map(str,input().split()))
    if len(a)==1:
        p+=1
    else:
        if p%2:
            if a[1]=="2":
                s.appendleft(a[2])
            else:
                s.append(a[2])
        else:
            if a[1]=="1":
                s.appendleft(a[2])
            else:
                s.append(a[2])
ans=""
if p%2==1:
    while s:
        ans+=s.pop()
else:
    while s:
        ans+=s.popleft()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def answer():
    s = input()
    q = int(input())
    rev = False
    for i in range(q):
        query = input().split()

        if query[0] == '2':
            f, c = int(query[1]), query[2]
            if (not rev and f == 1) or (rev and f == 2):
                s = c + s
            else:
                s = s + c
        else:
            rev = not rev
    if rev:
        return s[::-1]
    return s

print(answer())

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	S = input()
	S = S[:-1]
	Q = inputInt()
	for _ in range(Q):
		inp = input()
		inp = inp[:-1]
		inpt = inp.split()
		if len(inpt) == 1:
			S = S[::-1]
			continue
		else:
			oder = inpt[1]
			if oder == "1":
				S = inpt[2] + S
			else:
				S = S + inpt[2]

	print(S)



#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
h=0
for i in range(Q):
    q=input().split()
    if q[0]=="1":
        h=1-h
    else:
        t=int(q[1])
        if t==h+1:
            S.appendleft(q[2])
        else:
            S.append(q[2])
if h==0:
    for i in S:
        print(i,end="")
else:
    for i in range(len(S)):
        print(S[-i-1],end="")

Submission status: Time Limit Exceeded

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


def s_add(r, f):
    if r % 2 == 0:
        if f == 1:
            return True
        else:
            return False
    else:
        if f == 1:
            return False
        else:
            return True


cnt = 0
for _ in range(q):
    query = input()
    if query == "1":
        cnt += 1
    else:
        z, f, c = query.split()
        if s_add(cnt, int(f)):
            s = c + s
        else:
            s += c

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Left = []
Right = []
Reverse = False
for q in range(Q):
    query = input().split()
    if query[0]=="1":
        Reverse = not Reverse
    elif (query[1]=="1") ^ Reverse:#左側にくっつく
        S = query[2] + S
    else:
        S = S + query[2]
if Reverse:
    ans = "".join(reversed(S))
else:
    ans = S
print(ans)

Submission status: Time Limit Exceeded

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

prefix = ''
suffix = ''
mode = 0
for _ in range(Q) :
  Query = list(input().split())
  if Query[0] == '1' :
    mode ^= 1
  else :
    _, F, C = Query
    F = int(F) - 1
    
    if mode != F :
      suffix += C
    else :
      prefix += C
      

if mode == 0 :
  print(prefix[::-1] + S + suffix)
else :
  print(suffix[::-1] + S[::-1] + prefix)

Submission status: Time Limit Exceeded

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

def rev(sts):
    for i in range(len(sts)//2):
        sts[i],sts[-i-1] = sts[-i-1],sts[i]


for i in range(q):
    ins = input().split()
    if(ins[0] == "1"):
        rev(s)
    else:
        if (ins[1] == "1"):
            s.insert(0, ins[2])
        else:
            s.append(ins[2])

for j in s:
    print(j, end="")
print()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
# input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

def main():
    S = input()
    Q = INT()
    reverse_flag = False

    for _ in range(Q):
        query = list(input().split())
        if len(query) == 3:
            F = int(query[1])
            C = query[2]
            if F == 1:
                if reverse_flag:
                    S += C
                else:
                    S = C + S
            elif F == 2:
                if reverse_flag:
                    S = C + S
                else:
                    S += C
        else:
            if reverse_flag:
                reverse_flag = False
            else:
                reverse_flag = True
    
    if reverse_flag:
        S = S[::-1]
    print(S)
    return
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:


s = input()
queNum = int(input())

for _ in range(queNum):
    query = input()

    if query == '1':
        s = s[::-1]
    else:
        num, fi, ci = query.split()
        if fi == '1':
            s = ci + s
        else:
            s = s + ci
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = [i for i in input()]
Q = int(input())
rev_num = 0
for i in range(Q):
  query = [i for i in map(str,input().split())]
  if query[0]=='1':
    rev_num += 1
  else:
    if query[1]=='1':
      if rev_num%2==1:
        S.append(query[2])
      else:
        S.insert(0,query[2])
    else:
      if rev_num%2==1:
        S.insert(0,query[2])
      else:
        S.append(query[2])
        
if rev_num%2:
  S.reverse()
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

s=input()
q=int(input())
query=[input().split() for i in range(q)]
# print(query)
now=0
for i in range(q):
    if query[i][0]=="1":
        now+=1
    else:
        if now%2==0:
            if query[i][1]=="1":
                s=query[i][2]+s
            else:
                s=s+query[i][2]
        else:
            if query[i][1]=="1":
                s=s+query[i][2]
            else:
                s=query[i][2]+s
if now%2==1:
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
res = input()
r = False
for _ in range(int(input())):
    q = input().split()
    if q[0] == "1":
        r = not r
    else:
        if q[1] == "1":
            if r:
                res += q[2]
            else:
                res = q[2] + res
        else:
            if r:
                res = q[2] + res
            else:
                res += q[2]

if r:
    print(res[::-1])
else:
    print(res)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for _ in range(q):
    query = input()
    if query[0] == '1':
        s = s[::-1]
    else:
        _, f, c = query.split()
        if f == '1':
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

reverse = 0
pre = ''
after = ''
S = input()
Q = int(input())
for i in range(Q):
    q = input()
    if q[0] == '1':
        reverse = 1 - reverse
    else:
        T, F, C = q.split()
        if (F == '1') ^ (reverse == 1):
            pre = C + pre
        else:
            after += C
S = pre + S + after
if reverse:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s=deque(list(input()))
q=int(input())

for i in range(q):
  l=list(input().split())
  if len(l)==1:
    s.reverse()
  else:
    t,f,c=l[0],l[1],l[2]
    if f=='1':
      s.appendleft(c)
    else:
      s.append(c)

s=list(s)
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


def solve():
    S = deque(list(input()))
    Q = int(input())
    for _ in range(Q):
        query = list(input().split())
        if int(query[0]) == 1:
            S.reverse()
        else:
            if int(query[1]) == 1:
                S.appendleft(query[2])
            else:
                S.append(query[2])
    print(''.join(S))


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
S = input()
Q = int(input())
flag = True
for i in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag = not flag
    else:
        if flag:
            if q[1] == '1':
                S = q[2] + S
            else:
                S += q[2]
        else:
            if q[1] == '1':
                S = S + q[2]
            else:
                S = q[2] + S
if not flag:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
#[int(i) for i in input().split()]
s = input()
q = int(input())
for i in range(q):
    tmp = [j for j in input().split()]
    if tmp[0] == "2":
        tmp_f = tmp[1]
        tmp_c = tmp[2]
        if tmp_f == "1":
            s = tmp_c + s
        else:
            s += tmp_c
    else:
        s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
cnt = 0
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        cnt += 1
    else:
        if cnt % 2 == 0:
            if int(q[1]) == 1:
                S = q[2] + S
            else:
                S += q[2]
        else:
            if int(q[1]) == 1:
                S += q[2]
            else:
                S = q[2] + S

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = deque(list(input()))
Q = int(input())
for i in range(Q):
    q = input().split()
    if q[0] == "1":
        S.reverse()
    else:
        f, c = q[1:]
        if f == "1":
            S.appendleft(c)
        else:
            S.append(c)

print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]
flag=0
#print(query)
s=S
for i in range(Q):
    if len(query[i])==1:
        flag=(flag+1)%2 #反転フラグをひっくり返す
    else:
        if flag==0:
            if query[i][1]=="1":
                s="".join((query[i][2],s))
            else:
                s = "".join((s,query[i][2]))
        else:
            if query[i][1]=="2":
                s="".join((query[i][2],s))
            else:
                s = "".join((s,query[i][2]))
if flag==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
#D
S=input()
Q=int(input())
t=1 # 1:正順,0:逆順
for _ in range(Q):
    q=input()
    if q[0]=='1':
        t=(t+1)%2
    else:
        __,f,c=q.split()
        if f=='1':
            if t==1:
                S=c+S
            else:
                S=S+c
        else:
            if t==1:
                S=S+c
            else:
                S=c+S
    # print(S)
print(S if t else S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for i in range(q):
  ss=input()
  if ss[0]=='1':
    s=s[::-1]
    #s=t
  else:
    t,f,c=ss.split()
    if f=='1':
      s=c+s
      #s=t
    else:
      s=s+c
      
    







print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
is_reverse = False
for q in query:
    if q[0] == "1":
        is_reverse = not is_reverse
    else:
        if is_reverse:
            if q[1] == "2":
                S = q[2] + S
            else:
                S += q[2]
        else:
            if q[1] == "1":
                S = q[2] + S
            else:
                S += q[2]

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flip = 0
for i in range(Q):
    query = input()
    if query == str(1):
        flip += 1
    else:
        f = int(query[2])
        c = query[-1]
        if f == 1:
            if flip%2 == 1:
                S = S + c
            else:
                S = c + S
        else:
            if flip%2 == 1:
                S = c + S
            else:
                S = S + c
if flip%2 == 1:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
flag = True
for i in range(q):
    inp = input()
    if inp == '1':
        flag = not flag

    else:
        _, f, c = map(str, inp.split())
        if f == '1':
            if flag:
                s = c+s
            else:
                s += c
        else:
            if flag:
                s += c
            else:
                s = c+s
if flag:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
for i in range(Q):
  q = str(input())
  if int(q[0]) == 1:#Ti = 1
    S = S[::-1]
  else:
    if int(q[2]) == 1:#先頭
      t = [q[4], S]
      S = "".join(t)
    else:
      t = [S, q[4]]
      S = "".join(t)
print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
#反転操作はまとめてする
#2の操作が入れ替わるだけ
check=True
for i in range(q):
    x=input().split()
    if len(x)==1:
        check=(not check)
    else:
        t,f,c=x
        f=int(f)
        if check:
            if f==1:
                s=c+s
            else:
                s=s+c
        else:
            if f==2:
                s=c+s
            else:
                s=s+c
if check:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = deque(input())
q = int(input())
for i in range(q):
    x = list(input().split())
    if x [0] == '1':
        s.reverse()
    else:
        if x[1] == '1':
            s.appendleft(x[2])
        else:
            s.append(x[2])
print("".join(s))

Submission status: Time Limit Exceeded

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

inv = 0
for i in range(Q):
  query = input().split()
  if query == ['1']:
    inv = (inv + 1)%2
  else:
    f, c = query[1], query[2]
    if f == '1':
      if inv:
        S += c
      else:
        S = c + S
    else:
      if inv:
        S = c + S
      else:
        S += c

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
count = 0
for i in range(Q):
    TFC = input().split()
    if TFC[0] == '1':
        count += 1
    else:
        T, F, C = TFC
        if F == '1':
            if count % 2 == 0:
                S = C+S
            else:
                S = S+C
        else:
            if count % 2 == 0:
                S = S+C
            else:
                S = C+S
if count % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(N):
  Q = input()
  if Q[0] == "1":
    S = S[::-1]
  elif Q[2] == "1":
    S = Q[4] + S
  else:
    S = S + Q[4]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input().strip()
q = int(input().strip())
queries = [input().split() for _ in range(q)]
for que in queries:
    if que[0] == "1":
        s = s[::-1]
    elif que[0] == "2":
        if que[1] == "1":
            s = que[2] + s
        elif que[1] == "2":
            s += que[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
reverse_now = 0
first = ""
latter = ""
for i in range(q):
    k = list(input().split())
    if k[0] == "1":
        reverse_now = 1 - reverse_now
    else:
        f,c = k[1], k[2]
        if f == "1" and reverse_now == 0:
            first = c + first
        if f == "1" and reverse_now == 1:
            latter = latter + c
        if f == "2" and reverse_now == 0:
            latter = latter + c
        if f == "2" and reverse_now == 1:
            first = c + first
if reverse_now == 1:
    ans = list(first + s + latter)
    ans.reverse()
    ans = "".join(ans)
else:
    ans = first + s + latter
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    query = []
    revFlag = False
    for i in range(Q):
        query.append(list(input().split()))
    for i in range(Q):
        if query[i][0] == "1":
            if revFlag:
                revFlag = False
            else:
                revFlag = True
        else:
            if query[i][1] == "1":
                if revFlag:
                    S += query[i][2]
                else:
                    S = query[i][2] + S
            else:
                if revFlag:
                    S = query[i][2] + S
                else:
                    S += query[i][2]
    if revFlag:
        print(S[::-1])
    else:
        print(S)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for _ in range(q):
    query=list(input().split())
    if query[0]=='1':
        s=s[::-1]
    else:
        if query[1]=='1':
            s=[query[2]]+s
        else:
            s+=[query[2]]
print(*s,sep='')

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
S = input()
Q = int(input())
for i in range(Q):
    q = list(input().split())
    if q[0] == '1':
        S = S[::-1]
    else:
        if q[1] == '1':
            S = q[2] + S
        else:
            S += q[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    S = input().strip()
    Q = int(input())

    rev = 0
    for i in range(Q):
        A = [x for x in input().split()]
        if len(A) == 1:
            rev = (rev + 1) % 2
        else:
            f = int(A[1])
            c = A[2]
            if f == 1:
                if rev == 0:
                    S = c + S
                else:
                    S = S + c
            else:
                if rev == 0:
                    S = S + c
                else:
                    S = c + S

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

S = input()
Q = int(input())
queries = [input().rstrip('\n') for _ in range(Q)]

for query in queries:
    q = query.split(' ')
    if len(q) == 1:
        S = S[::-1]
    else:
        if q[1] == '1':
            S = q[2] + S
        else:
            S = S + q[2]
print(S)

Submission status: Time Limit Exceeded

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

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

    print(s)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
Rev=False
for _ in range(Q):
    q=list(input().split())
    if len(q)==1:
        Rev=not Rev
    else:
        if q[1]=='1':
            if not Rev:
                S=q[2]+S
            else:
                S=S+q[2]
        else:
            if Rev:
                S=q[2]+S
            else:
                S=S+q[2]
if Rev:
    S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    S=input()
    Q=I()
    dir=0#0で先頭
    
    for _ in range(Q):
        q=list(input().split())
        if q[0]=="1":
            dir+=1
            dir%=2
        else:
            dir2=dir
            if q[1]=="2":
                dir2+=1
                dir2%=2
            if dir2==0:
                S=q[2]+S
            else:
                S=S+q[2]
            
    ans=list(S)
    
    if dir==1:
        ans=ans[::-1]
        
    print(''.join(map(str, ans)))
        

main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for _ in range(q):
    a=list(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:
import sys
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

reverse = 0
S = input()
Q = int(input())
for i in range(Q):
    q = input()
    if q[0] == '1':
        reverse = 1 - reverse
    else:
        T, F, C = q.split()
        if (F == '1') ^ (reverse == 1):
            S = C + S
        else:
            S += C
if reverse:
    S = S[::-1]
print(S)