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 sys
def input(): return sys.stdin.readline().rstrip()
S = input()
Q = int(input())
qs = list(input() for _ in range(Q))
rev = 0
for q in qs:
    q = q.split()
    if q[0] == '1':
        rev += 1
        rev %= 2
    else:
        if (q[1]=='1' and rev==0) or (q[1]=='2' and rev==1):
            S = q[2] + S
        else:
            S += q[2]
print(S if rev == 0 else S[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
S = input()
Q = int(input())
qs = list(input() for _ in range(Q))
rev = 0
for q in qs:
    q = q.split()
    if q[0] == '1':
        rev = (rev+1) % 2
    else:
        if (q[1]=='1' and rev==0) or (q[1]=='2' and rev==1):
            S = q[2] + S
        else:
            S += q[2]
print(S if rev == 0 else S[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
S = input()
Q = int(input())
qs = list(input() for _ in range(Q))
rev = 0
for q in qs:
    q_sep = q.split()
    if q_sep[0] == '1':
        rev = (rev+1) % 2
    else:
        if (q_sep[1]=='1' and rev==0) or (q_sep[1]=='2' and rev==1):
            S = q_sep[2] + S
        else:
            S += q_sep[2]
print(S if rev == 0 else S[::-1])

Submission status: Time Limit Exceeded

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

for i in range(q):
    x = list(input().split())
    if len(x) == 3:
        if x[1] == '1':
            s.insert(0, x[2])
        else:
            s.append(x[2])
    else:
        s = s[::-1]
print(*s, sep='')

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
inversion_flg = False
# L = [input().split() for _ in range(Q)]

for i in range(Q):
    L = input().split()
    if L[0] == '1':
        if inversion_flg:
            inversion_flg = False
        else:
            inversion_flg = True
    # if L[0] == '2':
    else:
        if not inversion_flg:
            if L[1] == '1':
                S = L[2] + S
            else:
                S = S + L[2]
        else:
            if L[1] == '2':
                S = L[2] + S
            else:
                S = S + L[2]



# print(L)
if not inversion_flg: 
    print(S)
else:
    # S.reverse()
    S = S[::-1]
    print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
inversion_flg = False
# L = [input().split() for _ in range(Q)]

for i in range(Q):
    L = input().split()
    if L[0] == '1':
        if inversion_flg:
            inversion_flg = False
        else:
            inversion_flg = True
    if L[0] == '2':
        if not inversion_flg:
            if L[1] == '1':
                S = L[2] + S
            else:
                S = S + L[2]
        else:
            if L[1] == '2':
                S = L[2] + S
            else:
                S = S + L[2]



# print(L)
if not inversion_flg: 
    print(S)
else:
    # S.reverse()
    S = S[::-1]
    print(S)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
inversion_flg = False
# L = [input().split() for _ in range(Q)]

for i in range(Q):
    L = input().split()
    if L[0] == '1':
        if inversion_flg:
            inversion_flg = False
        else:
            inversion_flg = True
    if L[0] == '2':
        if not inversion_flg:
            if L[1] == '1':
                S = [L[2]] + S
            else:
                S = S + [L[2]]
        else:
            if L[1] == '2':
                S = [L[2]] + S
            else:
                S = S + [L[2]]



# print(L)
if not inversion_flg: 
    print(''.join(S))
else:
    # S.reverse()
    S = S[::-1]
    print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
inversion_flg = False
# L = [input().split() for _ in range(Q)]

for i in range(Q):
    L = input().split()
    if L[0] == '1':
        if inversion_flg:
            inversion_flg = False
        else:
            inversion_flg = True
    if L[0] == '2':
        if not inversion_flg:
            if L[1] == '1':
                S = [L[2]] + S
            else:
                S = S + [L[2]]
        else:
            if L[1] == '2':
                S = [L[2]] + S
            else:
                S = S + [L[2]]



# print(L)
if not inversion_flg: 
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
count = 0
from collections import deque
q1 = deque()
q2 = deque()
for i in range(Q):
    li = input().split()
    if len(li) == 1:
        count +=1
        q11 = q1
        q22 = q2
        q1 = q22
        q2 = q11
        q1.reverse()
        q2.reverse()
    if len(li) == 3:
        if li[1] == '1':
            q1.appendleft(li[2])
        if li[1] == '2':
            q2.append(li[2])
s1 = ''.join(list(q1))
s2 = ''.join(list(q2))
if count % 2 == 1:
    K = reversed(list(S))
    S = ''.join(K)
print(s1+S+s2)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
import time
# L = [input().split() for _ in range(Q)]

count = 0 # ['1']の数
for i in range(Q):
    L = input().split()
    if L[0] == '1':
        count += 1
    if L[0] == '2':
        if (count % 2) == 0:
            count = 0
            if L[1] == '1':
                S = [L[2]] + S
            else:
                S = S + [L[2]]
        else:
            if L[1] == '2':
                S = [L[2]] + S
            else:
                S = S + [L[2]]



# print(L)
if (count % 2) == 0:
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Submission status: Time Limit Exceeded

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

for _ in range(q):
    now_queue = input()
    if now_queue == '1':
        s = s[::-1]
    else:
        temp = now_queue.split()
        num = int(temp[1])
        if num == 1:
            s.insert(0, temp[2])
        else:
            s.append(temp[2])

print(''.join(s))

Submission status: Time Limit Exceeded

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

count = 0 # ['1']の数
for i in range(Q):
    L = input().split()
    if L[0] == '1':
        count += 1
    if L[0] == '2':
        if (count % 2) == 0:
            if L[1] == '1':
                S = [L[2]] + S
            else:
                S = S + [L[2]]
        else:
            if L[1] == '2':
                S = [L[2]] + S
            else:
                S = S + [L[2]]



# print(L)
if (count % 2) == 0:
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Submission status: Time Limit Exceeded

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

head = ""
tail = ""
main = S

status = 0

while Q:
  Q -= 1
  l = list(map(str,input().split()))
  if l[0] == "1":
    status = (status+1)%2
  else:
    letter = l[2]
    if l[1] == "1":
      if status == 0:
        head += letter
      else:
        tail += letter
    else:
      if status == 0:
        tail += letter
      else:
        head += letter

if status == 0:
  print(head[::-1]+main+tail)
else:
  print(tail[::-1]+main[::-1]+head)

Submission status: Time Limit Exceeded

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


flip = 0
head = ""
for _ in range(Q):
    q = input()
    if q == '1':
        flip += 1
    else:
        _, f, c = q.split(' ')
        if f == '1' and (flip % 2) == 0:
            # S = c + S
            head = head + c
        elif f == '1' and (flip % 2) != 0:
            S = S + c
        elif f != '1' and (flip % 2) == 0:
            S = S + c
        else:
            # S = c + S
            head = head + c

if (flip % 2) == 0:
    head = head[::-1]
    S = head + S
else:
    S = S[::-1]
    S = S + head

print(S)

Submission status: Time Limit Exceeded

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

#############
# Libraries #
#############

import math
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9 +7
INF = float('inf')

#############
# Functions #
#############

kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#############
# Main Code #
#############

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

head = ""
tail = ""
main = ""

main = S

status = 0

def query():
  global head
  global tail
  global main
  global status
  l = list(map(str,input().split()))
  if l[0] == "1":
    status = (status+1)%2
  else:
    letter = l[2]
    if l[1] == "1":
      if status == 0:
        head += letter
      else:
        tail += letter
    else:
      if status == 0:
        tail += letter
      else:
        head += letter

      


while Q:
  Q -= 1
  query()

if status == 0:
  print(head[::-1]+main+tail)
else:
  print(tail[::-1]+main[::-1]+head)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
s = list(stdin.readline().rstrip())
q = int(stdin.readline().rstrip())
t = []
for i in range(q):
    t.append([x for x in stdin.readline().rstrip().split()])
r = 0
for i in range(q):
    if int(t[i][0]) == 1:
        if r == 1:
            r = 0
        else:
            r = 1
    else:
        if (int(t[i][1]) + r) % 2 == 0:
            s.append(t[i][2])
        else:
            s.insert(0,t[i][2])
if r == 1:
    s = list(reversed(s))
s = ''.join(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    s = input()
    q = int(input())
    ans = deque()
    swap = False
    for i in range(len(s)):
        ans.append(s[i])

    for i in range(q):
        #print(ans, swap)
        inp = input()
        if len(inp) == 1:
            hoge = int(inp)
            if swap:
                swap = False
            else:
                swap = True
        else:
            # print(tuple(inp))
            fi, ci = tuple(inp)[2], tuple(inp)[4]
                       
            if int(fi) == 1:
                if not swap:
                    ans.appendleft(ci)
                else:
                    ans.append(ci)
            if int(fi) == 2:
                if not swap:
                    ans.append(ci)
                else:
                    ans.appendleft(ci)
    s_ans = str()
    ans = list(ans)
    if swap:
        for i in range(len(ans)-1,-1, -1):
            s_ans = s_ans +  ans[i]
    else:
        for i in range(len(ans)):
            s_ans = s_ans + ans[i]
    
    print(s_ans)
                
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = deque(input(""))
Q = int(input())
c = 0

for i in range(Q):
    t = input("")
    if len(t) == 1:
        c += 1
    elif t[2] == "1":
        if c % 2 == 0:
            s.appendleft(t[4])
        else:
            s.append((t[4]))
    else:
        if c % 2 == 0:
            s.append(t[4])
        else:
            s.appendleft(t[4])

if c % 2 == 0:
    for i in s:
        print(i,end="")
else:
    for i in range(len(s)-1,-1,-1):
        print(s[i],end="")

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
r=0
for i in range(n):
    strlist=input()
    if strlist[0]=='1':
        r+=1
    elif strlist[0]=='2' and strlist[2] == '1':
        if r%2==0:
            s = '{}{}'.format(strlist[4],s)
        else:
            s = '{}{}'.format(s,strlist[4])
    else:
        if r%2==0:
            s = '{}{}'.format(s,strlist[4])
        else:
            s = '{}{}'.format(strlist[4],s)
if r%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
# Problem D - String Formation

# input process
S = input().replace("\n", "")
S = list(S)
Q = int(input()) # query number

# initialization
sento = 1

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

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
for i in range(n):
  strlist=input()
  if strlist[0]=='1':
    s=s[::-1]
  elif strlist[0]=='2' and strlist[2] == '1':
    s = '{}{}'.format(strlist[4],s)
  else:
    s = '{}{}'.format(s,strlist[4])
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
for i in range(n):
  strlist=input()
  if strlist[0]=='1':
    s=s[::-1]
  elif strlist[0]=='2' and strlist[2] == '1':
    s = '{}{}'.format(strlist[4],s)
  elif strlist[0]=='2' and strlist[2] == '2':
    s = '{}{}'.format(s,strlist[4])
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
strlist=[input() for i in range(n)]
for i in range(n):
  if strlist[i][0]=='1':
    s=s[::-1]
  elif strlist[i][0]=='2' and strlist[i][2] == '1':
    s = '{}{}'.format(strlist[i][4],s)
  elif strlist[i][0]=='2' and strlist[i][2] == '2':
    s = '{}{}'.format(s,strlist[i][4])
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
S = input()
Q = int(input())
qs = list(input() for _ in range(Q))
rev = 0
for q in qs:
    q = q.split()
    if q[0] == '1':
        rev = (rev+1) % 2
    else:
        if (q[1]=='1' and rev==0) or (q[1]=='2' and rev==1):
            S = q[2] + S
        else:
            S = S + q[2]
print(S if rev == 0 else S[::-1])

Submission status: Time Limit Exceeded

Solution code:
# 文字列の操作だけでできるか

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

T = 0
for i in range(Q):
    Query = list(input().split())
    if len(Query) == 1:
        # T = T ^ 1 (排他的論理和; 0 XOR 1 = 1, 1 XOR 1 = 0)
        T ^= 1
    elif T == 0:
        s = Query[2]
        if Query[1] == '1':
            S = s + S
        else:
            S = S + s
    else:
        s = Query[2]
        if Query[1] == '1':
            S = S + s
        else:
            S = s + S

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
N=int(input())
Q=[input() for i in range(N)]
T=deque(S)
cnt=0
for i in range(N):
    if Q[i][0]=="1":
        cnt+=1
    if cnt%2==0:
        if Q[i][0]=="2":
            if Q[i][2]=="1":
                T.appendleft(Q[i][4])
                S=list(T)
            if Q[i][2]=="2":
                T.append(Q[i][4])
                S=list(T)
    if cnt%2==1:
        if Q[i][0]=="2":
            if Q[i][2]=="1":
                T.append(Q[i][4])
                S=list(T)
            if Q[i][2]=="2":
                T.appendleft(Q[i][4])
                S=list(T)
if cnt%2==1:
    S=S[::-1]
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
data = input()
num = int(input())
for _ in range(num):
    temp = input()
    if temp == "1":
        data = data[::-1]
    else:
        if temp[2] == "1":
            data = temp[4] + data
        else:
            data += temp[4]
print(data)

Submission status: Time Limit Exceeded

Solution code:
import sys
# input = sys.stdin.readline
# print(s)
s = input()
q = int(input()) #int(input())
inv = 1
query = [input() for _ in range(q)]

for t in query:
  if t == '1':
    inv *= -1 #s = s[::-1]
  else:
    _, f, c = map(str, t.split())
    if (f =='1' and inv == 1) or (f =='2' and inv == -1):
      s = c+s
    else:
      s = s+c

print(s[::inv])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
lead = ''
end = ''
inversion_flg = -1

for j in range(q):
    query = input().split()
    if query[0] == "1":
        inversion_flg *= -1
    else:
        if inversion_flg == 1:
            if query[1] == "1":
                query[1] = "2"
            else:
                query[1] = "1"
        if query[1] == "1":
            lead += query[2]
        else:
            end += query[2]

lead = lead[::-1]
s = lead + s + end
if inversion_flg == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

sei_or_gyaku = 1
mae = ''
ato = ''

for i in range(q):
    sirei = input()
    if len(sirei) == 1:
        ti = int(sirei)
        sei_or_gyaku *= -1
    else:
        ti, fi, ci = map(str, sirei.split())
        ti, fi = int(ti), int(fi)
        if fi == 1:
            if sei_or_gyaku == 1:
                mae += ci
            else:
                ato += ci
        else:
            if sei_or_gyaku == 1:
                ato += ci
            else:
                mae += ci

mae = mae[::-1]
ans = mae + s + ato
if sei_or_gyaku == 1:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = []
 
def func(string, t, f, c):
    if t == "1":
        new_str = ''.join(list(reversed(string)))
        return new_str
    else:
        if f == "1":
            return c + string
        else:
            return string + c
 
for i in range(0,q):
    query.append(input().split())
for i in query:
    if i[0] == "1":
        s = func(s,i[0],'','')
    else:
        s = func(s,i[0],i[1],i[2])
print(s)

Submission status: Time Limit Exceeded

Solution code:
left = []
right = []
s = input()
q = int(input())
left_first = True
for i in range(q):
    query = input().split()
    if query[0] == '1':
        left_first = not left_first
    else:
        if (left_first and query[1] == '1') or (not left_first and query[1] == '2'):
            left.insert(0,query[2])
        else:
            right.append(query[2])
s = ''.join(left) + s + ''.join(right)
if not left_first:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
# D - String Formation
s = [x for x in input()]
q = int(input())
odd = 0
for i in range(q):
    t,*fc = input().split()
    if t=='1': odd ^= 1 #t1
    else: #t2
        if odd ^ (int(fc[0])-1): s = s+[fc[1]]
        else: s = [fc[1]] + s
print(*s[::-1] if odd else s,sep='')

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
isReverse = False
for i in range(0, q):
    query = input()
    if query[0] == '1':
        isReverse = not isReverse 
    elif (query[0] == '2')&(query[2]=='1'):
        if isReverse:
            s = ''.join([s,query[4]])
        else:
            s = ''.join([query[4],s])
    else:
        if isReverse:
            s = ''.join([query[4],s])
        else:
            s = ''.join([s,query[4]])
if isReverse:
    s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = []
lead = ''
end = ''
inversion_flg = False

for j in range(q):
    i = input().split()
    if i[0] == "1":
        if inversion_flg:
            inversion_flg = False 
        else:
            inversion_flg = True
    else:
        if inversion_flg:
            if i[1] == "1":
                i[1] = 2
            else:
                i[1] = 1
        if int(i[1]) == 1:
            lead+=i[2]
        else:
            end+=i[2]

lead = lead[::-1]
s = lead+s+end
if inversion_flg:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = []
lead = ''
end = ''
inversion_flg = False

for j in range(q):
    i = input().split()
    if int(i[0]) == 1:
        if inversion_flg:
            inversion_flg = False 
        else:
            inversion_flg = True
    else:
        if inversion_flg:
            if int(i[1]) == 1:
                i[1] = 2
            else:
                i[1] = 1
        if int(i[1]) == 1:
            lead+=i[2]
        else:
            end+=i[2]

lead = lead[::-1]
s = lead+s+end
if inversion_flg:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(str(input()))
q = int(input())
hanten = 0

for i in range(q):
    x = input().split()
    if len(x) == 1:
        t = x[0]
        if t == "1" and hanten == 0:
            hanten = 1
        elif t == "1" and hanten == 1:
            hanten = 0

    else:
        f = int(x[1])
        c = str(x[2])
        if f == 1 and hanten == 0:
            s.insert(0, c)

        elif f == 1 and hanten == 1:
            s.append(c)

        elif f == 2 and hanten == 0:
            s.append(c)

        elif f == 2 and hanten == 1:
            s.insert(0, c)

else:
    d = len(s)
    if hanten == 1:
        for i in range(d):
            print(s[d-1-i], end="")
    else:
        for i in range(d):
            print(s[i], end="")

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
flag = True
query=input()
while(True) :
  try :
    query = query.split()
   
    if(query[0] == "1") :
      flag = not(flag)
    else :
      if(((query[1] == "1") and flag) or ((query[1] == "2") and not(flag))) :
        S.insert(0, query[2])
      else :
        S.append(query[2])
      
    query=input()
  except EOFError:
    break

if(not(flag)) :
  S.reverse()
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = []
lead = ''
end = ''
inversion_flg = False

for j in range(q):
    i = input().split()
    if int(i[0]) == 1:
        inversion_flg = False if inversion_flg else True
    else:
        if inversion_flg:
            i[1] = 2 if int(i[1]) == 1 else 1
        if int(i[1]) == 1:
            lead+=i[2]
        else:
            end+=i[2]

lead = lead[::-1]
s = lead+s+end
if inversion_flg:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
flag = True
S = input()
Q = int(input())
for i in range(Q):
  T = input().split()
  if T[0] == "1":
    if flag == True:
      flag = False
    else:
      flag = True
  else:
    if flag == True:
      if T[1] == "1":
        S = T[-1] + S
      else:
        S = S + T[-1]
    else:
      if T[1] == "1":
        S = S + T[-1]
      else:
        S = T[-1] + S
if flag == True:
  print(S)    
else:
  T = S[::-1]
  print(T)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
flag = True

for q in range(0, Q) :
  query = input().split()
  if(query[0] == "1") :
    flag = not(flag)
  else :
    if(((query[1] == "1") and flag) or ((query[1] == "2") and not(flag))) :
      S.insert(0, query[2])
    else :
      S.append(query[2])

if(not(flag)) :
  S.reverse()
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = []
lead = ''
end = ''
inversion_flg = False

for i in range(q):
    query.append(input().split())
for i in query:
    if int(i[0]) == 1:
        inversion_flg = False if inversion_flg else True
    else:
        if inversion_flg:
            i[1] = 2 if int(i[1]) == 1 else 1
        if int(i[1]) == 1:
            lead+=i[2]
        else:
            end+=i[2]

lead = lead[::-1]
s = lead+s+end
if inversion_flg:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import numpy as np


def main():
    a = input()
    n = int(input())

    order = True
    reverse_count = 0
    for _ in range(n):
        input_ = input().split(' ')
        if input_[0] == '2':
            if order:
                if input_[1] == '1':
                    a = input_[2] + a
                if input_[1] == '2':
                    a = a + input_[2]
            else:
                if input_[1] == '1':
                    a = a + input_[2]
                if input_[1] == '2':
                    a = input_[2] + a
        elif input_[0] == '1':
            order = not order
            reverse_count += 1

    if reverse_count % 2 == 1:
        a = a[::-1]

    print(a)

    return 0


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# D - String Formation
s = input()
q = int(input())

fb = ['','']
odd = 0
for i in range(q):
    t,*fc = input().split()
    if t=='1': odd ^= 1 #t1
    else: #t2
        fb[odd ^ (int(fc[0])-1)] += fc[1]
        # if odd: #odd:reverse
        #     if fc[0]=='1': fb[1] += fc[1] #f1
        #     else: fb[0] += fc[1] #f2
        # else: #even
        #     if fc[0]=='1': fb[0] += fc[1] #f1
        #     else: fb[1] += fc[1] #f2

if odd: print(fb[1][::-1] + s[::-1] + fb[0])
else: print(fb[0][::-1] + s + fb[1])

Submission status: Time Limit Exceeded

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

for q in range(0, Q) :
  query = input().split()
  if(query[0] == "1") :
    S.reverse()
  else :
    if(query[1] == "1") :
      S.insert(0, query[2])
    else :
      S.append(query[2])

print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
head = []
tail = []

cnt = 0
for _ in range(n):
    li = list(input().split())
    if li[0] == '1':
        head, tail = tail, head
        s = s[::-1]
        cnt += 1
    else:
        if li[1] == '1':
            head += li[2]
        else:
            tail += li[2]

ans = head[::-1] + s + tail
print(*ans , sep='')

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
head = []
tail = []

cnt = 0
for _ in range(n):
    li = list(input().split())
    if li[0] == '1':
        head, tail = tail, head
        s = s[::-1]
        cnt += 1
    else:
        if li[1] == '1':
            head.append(li[2])
        else:
            tail.append(li[2])

ans = head[::-1] + s + tail
print(*ans , sep='')

Submission status: Time Limit Exceeded

Solution code:
import sys
S =input()
Q=int(input())
orders=[]
for line in sys.stdin.readlines():
    orders.append(line.rstrip().split(" "))
dire=1
right=""
left=""
center=""
for order in orders:
    if order[0]=="1":
        dire=dire*(-1)
        center=right
        right=left[::-1]
        left=center[::-1]
    elif order[0]=="2" and order[1]=="1":
        left=order[2]+left
    elif order[0]=="2" and order[1]=="2":
        right=right+order[2]
if dire==1:
    S=S
elif dire==-1:
    S=S[::-1]
print(left+S+right)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
hanten = True
atm = ""
kts = ""
for i in range(q):
    l = input().split()
    if l[0] == "1":
        hanten = not hanten
    else:
        if l[1] == "1":
            if hanten:
                atm = l[2] + atm
            else:
                kts = kts + l[2]
        else:
            if hanten:
                kts = kts + l[2]
            else:
                atm = l[2] + atm

ans = atm + s + kts
if hanten == False:
    print(ans[::-1])
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
li=[S[i] for i in range(len(S))]
Q=int(input())
for i in range(Q):
    query=input().split()
    if len(query)==1:
        query_1=query
        li.reverse()
    else:
        query_2 = query
        if query_2[1]=="1":
            li.insert(0,query_2[2])
        else:
            li.append(query_2[2])
print("".join(li))