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())

while(q > 0):
    try:
        t, f, c=map(str, input().split())
        if int(f) == 1:
            s = c + s
        else:
            s = s + c
    except:
        s = s[::-1]
    q-=1
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
re=False
for i in range(0,q):
    Q=input().split(' ')
    if int(Q[0])==1:
        re=not re
        continue
    if int(Q[0])==2:
        if (int(Q[1])==1 and (not re)) or (int(Q[1])==2 and re):
            s=Q[2]+s
        if (int(Q[1])==1 and re) or (int(Q[1])==2 and (not re)):
            s=s+Q[2]
if re==True:
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    #t
    t=input().split()

    if t[0] == "1":
        s = s[::-1]
    elif t[1] == "1":
        s = t[2] + s
    else:
        s = s + t[2]
"""
    elif t[0] == "2":
        #t[1] = f
        if t[1] == "1":
            s = t[2] + s
        elif t[1] == "2":
            s = s + t[2]
"""
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():
	SS = input()
	SS = SS[:-1]
	Q = inputInt()

	S = ["ooogggiii"]

	flg = True
	for _ in range(Q):
		inp = input()
		inp = inp[:-1]
		inpt = inp.split()

		if len(inpt) == 1:
			if flg == True:
				flg = False
			else:
				flg = True
		else:
			oder = inpt[1]
			if oder == "1":
				if flg == True:
					S = [inpt[2]] + S
				else:
					S = S + [inpt[2]]
			else:
				if flg == True:
					S = S + [inpt[2]]
				else:
					S = [inpt[2]] + S

	#print(S)
	S = "".join(S)
	S = S.replace("ooogggiii",SS)
	if flg == True:
		print(S)
	else:
		print(S[::-1])


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# 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:
def main():
  s=input()
  q=int(input())
  tmp=[]
  for i in s:
      tmp.append(i)

  for _ in [0]*q:
      query = input()
      try:
          t = int(query)
          tmp = tmp[::-1]
      except ValueError:
          t,f,c = map(str,query.split())
          if f == "1":
              tmp = [c] + tmp
          if f == "2":
              tmp.append(c)
  print(''.join(tmp))
main()

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
p = []
for i in range(n):
  i =(input().split())
  if i[0] == '1':
    s = s[::-1]
  elif i[0] =='2':
    if i[1] == '1':
      s.insert(0,i[2])
    else:
      s.append(i[2])
#  count +=1      
      
#print(s)      
      
s = ''.join(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    reverse = False
    for _ in range(Q):
        query= input()
        if query[0] == '1':
            reverse = not reverse
        elif query[2] == '1':
            if reverse:
                S = ''.join([S,query[4]])
            else:
                S = ''.join([query[4],S])
        elif query[2] == '2':
            if reverse:
                S = ''.join([query[4],S])
            else:
                S = ''.join([S,query[4]])

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

    print(S)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:


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



for i in range(q):
    inp = input()
    if inp[0] == "1":
        s = s[::-1]
    elif inp[2] == "1":
        s = inp[4] + s
    else:
        s = s + inp[4]


print(s)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys
sys.setrecursionlimit(4100000)

ans = str(input())
n = int(input())
r = 2

for i in range(n) :
    x = input().split(" ")
    if x[0] == "1" :
        r = r + 1
    else :
        pp = int(x[1])
        if r % 2 == 1 :
            pp = pp + 1
        if pp % 2  == 1 :
            ans = x[2] + ans
        else :
            ans = ans + x[2]

if r % 2 == 1 :
    ans = ans[::-1]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S =input()
Q = int(input())
T = [0]*Q
for i in range(0,Q):
  T[i] = list(map(str, input().split()))



  
  
nS = ""
for i in range(0,Q):
  if(i != Q-1 and T[i][0] == "1" and T[i+1][0] == "1"):
    T[i][0] = 0
    T[i+1][0] = 0
  elif(T[i][0] == "2" and T[i][1] == "1"):
      S = T[i][2]+S
  elif(T[i][0] == "2" and T[i][1] =="2"):
      S = S+T[i][2]
  elif(T[i][0] == "1"):
    S = S[::-1]
  elif(T[i][0] == 0):
    pass

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
f = 0
for i in range(Q):
  Query = input().split()
  if Query[0] == '1':
    f = 1 - f
  else:
    if int(Query[1]) - f == 1:
      S = Query[2] + S
    else:
      S = S + Query[2]
if f == 0:
  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 t in tmp:
        if len(t) == 1:
            s = s[::-1]
        else:
            f = int(t[1])
            c = t[2]
            if f == 1:
                s = c + s
            else:
                s = s + c
                
    print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def abc158d(s_init, n, query):
    ans = s_init
    reverse = 0
    for i in range(n):
        tgt = query[i]
        if tgt[0] == "1":
            reverse = (reverse + 1) % 2
        elif tgt[0] == "2":
            if tgt[1] == "1":
                if reverse == 0:
                    ans = tgt[2] + ans
                else:
                    ans = ans + tgt[2]
            elif tgt[1] == "2":
                if reverse == 0:
                    ans = ans + tgt[2]
                else:
                    ans = tgt[2] + ans
            else:
                print("invalid query!!:" + str(tgt[0]) + str(tgt[1]))
        else:
            print("invalid query!!:" + str(tgt[0]))
    if reverse == 0:
        return ans
    else:
        return ans[::-1]

###
# main
if(__name__ == '__main__'):
    # input
    s_init = input()
    n = int(input())
    query = []
    for i in range(n):
        query.append(input().split(" "))
    
    accomplishment = abc158d(s_init, n, query)
    
    # output
    print(accomplishment)
# else:
    # do nothing

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
ope1=None
ope2=None
add_s=''
last=True
for i in range(q):
    query = input().split()
    if query[0] == '1' == ope1:
        ope1=None
        continue
    elif query[0] == '1' and ope1 is None:
        last=True
        ope1='1'
        continue
    elif query[0] == '2':
        last=False
        if ope1 and add_s:
            s = ''.join([add_s,s]) if ope2=='1' else ''.join([s,add_s])
            s = s[::-1]
            ope1=None
            ope2=None
        elif ope1:
            s = s[::-1]
            ope1=None

        if ope2 is None:
            ope2=query[1]
            add_s=query[2]
            continue
        elif query[1] == '1' == ope2:
            add_s = ''.join([query[2],add_s])
            continue
        elif query[1] == '2' == ope2:
            add_s = ''.join([add_s,query[2]])
            continue
        else:
            s = ''.join([add_s,s]) if ope2=='1' else ''.join([s,add_s])
            ope2=query[1]
            add_s=query[2]
if last:
    if ope2:
        s = ''.join([add_s,s]) if ope2=='1' else ''.join([s,add_s])
    if ope1:
        s=s[::-1]
else:
    if ope1:
        s=s[::-1]
    if ope2:
        s = ''.join([add_s,s]) if ope2=='1' else ''.join([s,add_s])
print(s)

Submission status: Time Limit Exceeded

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

for i in range(int(Q)):
    TFC = input().split()
    if TFC[0] == "1":
        S = S[::-1]
    else:
        if TFC[1] == "1":
            S = '' . join([TFC[2], S])
        else:
            S = '' . join([S, TFC[2]])

print(S)

Submission status: Time Limit Exceeded

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

S = input()
N = int(input())

for i in range(N):
    Q = list(input().split())
    a = int(Q[0])
    if a == 1:
        S = S[::-1]
    elif a == 2:
        b = int(Q[1])
        if b == 1:
            S = Q[2] + S
        elif b == 2:
            S = S + Q[2]
print(S)

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 = s[::-1]
    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:
syoki_str = input()
syori_cnt = int(input())

for i in range(syori_cnt):
    line = input().split(" ")
    if line[0] == "1":
        syoki_str = ''.join(list(reversed(syoki_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:
s=input()
reverses=s[::-1]
q=int(input())
count=0
for i in range(q):
    query=list(map(str,input().split()))
    query[0]=int(query[0])
    if query[0]==1:
        count+=1
    else:
        query[1]=int(query[1])
        if count%2==0:
            if query[1]==1:
                s=query[2]+s
                reverses=reverses+query[2]
            else:
                s=s+query[2]
                reverses=query[2]+reverses
        else:
            if query[1]==1:
                s=s+query[2]
                reverses=query[2]+reverses
            else:
                s=query[2]+s
                reverses=reverses+query[2]
if count%2==0:
    print(s)
else:
    print(reverses)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(N):
  if int(Q[i][0]) == 1:
    S == S.reverse()
  else:
    if int(Q[i][1]) == 1:
      S.insert(0, Q[i][2])
    else:
      S.append(Q[i][2])

S = "".join(S)
print(S)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
S = input()
N = int(input())
Q =  [list(map(str, stdin.readline().split())) for _ in range(N)]
flag = True
for i in range(N):
    if Q[i][0]=="1":
        flag = not(flag)                
    else: 
        if (Q[i][1] == "2") == (flag == True):
            S += Q[i][2]#先頭
        else :
            S1 = Q[i][2]+ S
            S = S1

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

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]) == 1:
    S = S[::-1]
    continue
  else:
    if q[i][1] == "1":
      S = q[i][2] + S
      continue
    else: 
      S += q[i][2]
      continue

print(S)

Submission status: Time Limit Exceeded

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

for i in range(n):
  q = list(input().split())
  
  if len(q) == 1: #反転処理
    s = ''.join(list(reversed(s))) 
  else:
    if q[1] == "1":
      s = q[2] + s
    else:
      s = s + q[2]
      
print(s)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    s = input()
    n = int(input())
    for _ in range(n):
        q = input().split()
        if q[0] == '1':
            s = s[::-1]
        else:
            if q[1] == '1':
                s = q[2]+s
            elif q[1] == '2':
                s = s+q[2]
    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())
state = 0
for q in range(Q):
    query = input()
    if query[0] == '1':
        if state == 0:
            state = 1
        else:
            state = 0
    else:
        if query[2] == '1':
            if state == 0:
                s = query[-1] + s
            else:
                s += query[-1]
        else:
            if state == 0:
                s += query[-1]
            else:
                s = query[-1] + s

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
R = 1
Top = []
Bot = []
for _ in range(Q):
  inp = input()
  if inp[0]=='1':
    R *= -1
  else:
    o, f, c = inp.split()
    if f=='1' and R==-1 or f=='2' and R==1:
      Bot += [c]
    else:
      Top = [c] + Top
S = ''.join(Top+[S]+Bot)
S = S if R==1 else S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
ad=[[],[]]
count=0
s=input()
q=int(input())
for i in range(q):
    t=input().split()
    if t[0]=='1':
        count+=1
        ad[0].reverse()
        ad[1].reverse()
        ad=[ad[1],ad[0]]
    elif t[1]=='1':
        ad[0].insert(0,t[2])
    else:
        ad[1].append(t[2])

if count%2==0:
    print(''.join(ad[0])+s+''.join(ad[1]))
else:
    print(''.join(ad[0])+s[::-1]+''.join(ad[1]))

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
q=int(input())
for i in range(0,q):
    k=input()
    if k[0]=="1":
        s = s[::-1]
    elif k[2]=="1":
        s.insert(0,k[4])
    else:
        s.append(k[4])
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    T = list(input().split())
    if len(T) > 1:
        if T[1] == '1':
            # 先頭に追加
            S = "{0}{1}".format(T[2], S)
        else:
            # 末尾に追加
            S = "{0}{1}".format(S, T[2])
    else:
        S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

result=''

# 処理
for i in range(Q):
    if Query[i][0]=='1':
        S=S[::-1]
    else:
        if Query[i][1]=='1':
            S=Query[i][2]+S
        else:
            S=S+Query[i][2]


print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
num = int(input())
t = "X"
reverse = False

for _ in range(num):
    row = input().split()
    if len(row) == 1:
        if reverse == False:
            reverse = True
        else:
            reverse = False
    else:
        x = row[1]
        y = row[2]
        if x == "1" and reverse:
            t += y
        elif x == "1" and not reverse:
            t = y + t
        elif x == "2" and reverse:
            t = y + t
        else:
            t += y

if reverse:
    t = t.replace("X", s)
    print(t[::-1])
else:
    t = t.replace("X", s)
    print(t)

Submission status: Time Limit Exceeded

Solution code:
from itertools import chain
s = input()
q = int(input())
for i in range(1, q+1):
    a, b, c, *_ = chain(input().split(), [None] * 3)
    if int(a) == 1:
        s = s[::-1]
    else:
        s = c + s if int(b) == 1 else s + c
print(s)

Submission status: Time Limit Exceeded

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

s = input().rstrip("\n")
q = int(input())

for i in range(q):
    # query = list(map(str, input().split()))
    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("{}回目:{}".format(i+1,s))

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
queries = list()
for ii in range(0,q):
  queries.append(input())
  
#print(queries)

for query in queries:
  if query.startswith("1"):
    s = s[::-1]
  elif query.startswith("2"):
    if query[2] == "1":
      s = query[4] + s
    elif query[2] == "2":
      s = s + query[4]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
lines = [list(input().split()) for i in range(q)]

def q1(s):
    for i in range(len(s) // 2):
        s[i],s[-1-i] = s[-1-i],s[i]
    return s

def q2(s, l):
    if int(l[1]) == 1:
        s.insert(0,l[2])
    else:
        s.extend(l[2])
    return s

for l in lines:
    if int(len(l)) == 1:
        q1(s)
    else:
        q2(s, l)

print(*s, sep='')

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
q = [input().split() for l in range(n)]
for i in range(n):
  if q[i][0] == '1':
    s = ''.join(list(reversed(s)))
  else:
    if q[i][1] == '1':
      l = [q[i][2],s]
      s = ''.join(l)
    else:
      l = [s,q[i][2]]
      s = ''.join(l)
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input().split(" ")
Q = int(input())

bit = 2
for i in range(0, Q):
  inp = input().split(" ")
  
  if inp[0] == "1":
    bit += 1
  if inp[0] == "2":
    if inp[1] == "1" and bit % 2 == 1:
      S.append(inp[2])
    elif inp[1] == "1" and bit % 2 == 0:
      S.insert(0, inp[2])
    elif inp[1] == "2" and bit % 2 == 1:
      S.insert(0, inp[2])
    elif inp[1] == "2" and bit % 2 == 0:
      S.append(inp[2])  

if bit % 2 == 1:
  print("".join(S)[::-1])
else:
  print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
total_t=1
result=[s]
for i in range(q):
  t=input()
  if t=="1":
    total_t=total_t*(-1)
  else:
    f_t=int((t.split())[1])*total_t
    c=(t.split())[2]
    if f_t==1 or f_t==-2:
      result.insert(0,c)
    else:
      result.append(c)
r=''.join(result)
if total_t==-1:
  new_r=r
  r=new_r[::-1]
print(r)

Submission status: Time Limit Exceeded

Solution code:
"""
D - String Formation
"""
s = input()
q = int(input())
string = []
for i in range(q):
    string.append(input())
i = 0
while i < len(string):
    emir = string[i]
    if i+1 == len(string):
        if emir[0] == '1':
            s = s[::-1]
            i += 1
        elif emir[0] == '2':
            if emir[2] == '1':
                s = emir[4] + s
                i += 1
            elif emir[2] == '2':
                s += emir[4]
                i += 1
    else:
        sonraki = string[i+1]
        if emir[0] == sonraki[0] == '1':
            i = i + 2
        elif emir[0] == '1':
            s = s[::-1]
            i += 1
        elif emir[0] == '2':
            if emir[2] == '1':
                s = emir[4] + s
                i += 1
            elif emir[2] == '2':
                s += emir[4]
                i += 1
print(s)

Submission status: Time Limit Exceeded

Solution code:
P=[];T=[];r=0
S=input()
Q=int(input())
for i in range(Q):
  L=list(map(str,input().split()))
  P.append(L)
#print(P)
for i in range(Q):
  if P[i][0]=="1":
    r=r+1
  else:
    if P[i][1]=="1":
      T="".join(S)
      if r%2==0:
        T=P[i][2]+T
      else:
        T=T+P[i][2]
      S=list(T)
    else:
      T="".join(S)
      if r%2==0:
        T=T+P[i][2]
      else:
        T=P[i][2]+T
      S=list(T)

if r%2==0:
  print("".join(S))
else:
  T="".join(S)
  S=list(reversed(T))
  print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
def main():
    S=list(str((input())))
    Q=int(input())
    for _ in range(Q):
        T=list(map(str,input().split()))
        if T[0]=="1":
            S.reverse()
        else:
            if T[1]=="1":
                S.insert(0,T[2])
            else:
                S.append(T[2])

    print("".join(S))

main()

Submission status: Time Limit Exceeded

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

    for _ in range(q):
        tmp = input()
        if len(tmp) == 1:
            s = s[::-1]
        else:
            a, b, c = tmp.split()
            if b == '1':
                s = c + s
            else:
                s = s + c
    print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


S = deque(input())
Q = int(input())
QUERY = [input().split(" ") for i in range(Q)]

for query in QUERY:
    #print(query)
    if query[0] == "1":#反転
        S.reverse()
    else:#=="2"
        if query[1] == "1":
            S.appendleft(query[2])
        else:#=="2"
            S.append(query[2])
print("".join(list(S)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    s = input()
    q = int(input())
    for _ in range(q):
        inp = input()
        if inp == "1":
            s = s[::-1]
        else:
            z = inp.split()
            b = int(z[1])
            c = z[2]
            if b == 1:
                s = "".join([c, s])
            else:
                s = "".join([s, c])
    print(s)
resolve()

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 q[i][0] == "1":
    S = S[::-1]
    continue
  else:
    if q[i][1] == "1":
      S = q[i][2] + S
      continue
    else: 
      S += q[i][2]
      continue

print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = [input()]
q = int(input())

flag = 0
for i in range(q):
    x = list(input().split())
    if int(x[0]) == 1:
        if flag ==0:
            flag = -1
        else:
            flag = 0
        continue
    if int(x[1]) +flag == 1:
        s2 = deque(s)
        # s.insert(0, x[2])
        s2.appendleft(x[2])
        s = list(s2)
    else:
        s.append(x[2])
ans = ''.join(s)
if flag == -1:
    print(ans[::-1])
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
c=0
for i in range(Q):
    q=input()
    if q=='1':
        c+=1
    else:
        if c%2==1:
            S=S[::-1]
            c=0
        _q=q.split()
        S=_q[2]+S if _q[1]=='1' else S+_q[2]
if c!=0:
    if c%2==1:
        S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
import math

a = input()
n = int(input())


rev = 0

for i in range (n):
    li = input().split()
    
    if li[0] == '1':
        rev = rev + 1
    elif li[0] == '2' and li[1] == '1':
        if rev % 2 == 0:
            a = li[2] + a
        else:
            a = ''.join(list(reversed(a)))
            a = li[2] + a
            rev = 0
    else:
        if rev % 2 == 1:
            a = ''.join(list(reversed(a)))
            a = a + li[2]
            rev = 0
        else:
            a = a + li[2]

if rev % 2 == 1:
    a = ''.join(list(reversed(a)))
    print(a)
else:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
#D
#入力
S=input()
Q=int(input())
#Query=[]
#for i in range(Q):
#    Query.append(list(input().split()))
    
    
#処理
t=0
b=""
a=""
for j in range(Q):
    i=list(input().split())
    if i[0]=="1":
        t+=1
        c=b
        b=a[::-1]
        a=c[::-1]
    elif i[0]=="2":
        if i[1]=="1":
            b=i[2]+b
        elif i[1]=="2":
            a=a+i[2]
if t%2==0:
    print(b+S+a)
    
else:
    print(b+S[::-1]+a)

Submission status: Time Limit Exceeded

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

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        s = s[::-1]
    else:
        if t[1] == "1":
            s = "".join([t[2],s])
        else:
            s = "".join([s,t[2]])

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:
    ans.reverse()
  else:
    _,f,c=q
    if f=='1':
      ans.appendleft(c)
    else:
      ans.append(c)
print(*ans,sep='')

Submission status: Time Limit Exceeded

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

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

for i in range(n):
    hoge = input().split()
    if hoge[0] == '1':
        mode = mode * -1
    else:
        if mode == 1:
            if hoge[1] == '1':
                a = hoge[2] + a
            else:
                a = a + hoge[2]
        else:
            if hoge[1] == '2':
                a = hoge[2] + a
            else:
                a = a + hoge[2]

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

print(a)

Submission status: Time Limit Exceeded

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

    is_true = True
    for i in range(q):
        input_list = list(input().split())
        if input_list[0] == '1':
            if is_true:
                is_true = False
            else:
                is_true = True
            continue
        if input_list[1] == '1':
            if is_true:
                s = input_list[2] + s
            else:
                s = s + input_list[2]
        else:
            if is_true:
                s = s + input_list[2]
            else:
                s = input_list[2] + s
            
    if not is_true:
        s = s[::-1]

    print(s)

if __name__ == '__main__':
    main()