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

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

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


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

Submission status: Time Limit Exceeded

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

rev=0
for i in range(Q):

    #T= map(str,input().split())
    X=list(map(str,input().split()))

    if X[0] == '1':
        rev+=1
    else:
        if X[1] =='1':
            if rev%2==0:
                S=X[2]+S
            else:
                S+=X[2]
        else:
            if rev%2==0:
                S+=X[2]
            else:
                S=X[2]+S

if rev%2!=0:
    S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

ans = ''
if t1 == -1:
    ans = ''.join(list(reversed(s)))
else:
    ans = s

print(ans)

Submission status: Time Limit Exceeded

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

ans = s
for i in range(n):
  q = list(input().split())
  if int(q[0]) == 1:
    temp = ans
    ans = ''
    for i in reversed(range(len(temp))):
      ans += temp[i]
  elif int(q[0]) == 2:
    if int(q[1]) == 1:
      ans = q[2] + ans
    else:
      ans = ans + q[2]
  
print(ans)

Submission status: Time Limit Exceeded

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

mode = 1

for i in range(Q):
  TFC = list(map(str, input().split()))
  if "1" in TFC[0]:
    mode *= -1
    continue
  if mode == 1:
    if "1" in TFC[1]:
      S = ''.join([TFC[2].strip(),S])
    else:
      S = ''.join([S,TFC[2].strip()])
  else:
    if "1" in TFC[1]:
      S = ''.join([S,TFC[2].strip()])
    else:
      S = ''.join([TFC[2].strip(),S])

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
for i in range(1,q+1):
  queue = list(input().split())
  if len(queue) > 1:
    if queue[1] == '1':
      s.insert(0,queue[2])
    elif queue[1] == '2':
      s.append(queue[2])
  else:
    s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    q = [0]*Q
    for i in range(Q):
        q[i] = list(input().split())
        
    rev = 0
    for i in range(Q):
        if q[i][0] == "1":
            rev += 1
        else:
            if (q[i][1] == "1" and rev%2 == 0) or (q[i][1] == "2" and rev%2 == 1):
                S = q[i][2] + S
            else:
                S = S + q[i][2]

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
for i in range(int(input())):
    query = input()
    if query[0] == "1":
        s = s[::-1]
        continue

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
ftemp, btemp = '', ''

Query = []
for i in range(Q):
  Query.append(list(input()))
  if i != 0:
    if Query[i-1][0] == '1' and Query[i][0] == '1':
      Query[i-1][0], Query[i][0] = 0, 0
    
for i in range(Q):
  if Query[i][0] == '2':
    if Query[i][2] == '1':
      ftemp = Query[i][4] + ftemp
    else: #Query[i][1] == 2
      btemp = btemp + Query[i][4]
  elif Query[i][0] == '1':
    S = ftemp + S + btemp
    S = S[::-1]
    ftemp, btemp = '', ''

S = ftemp + S + btemp
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    Query = list(input().split())
    #print(Query)
    if len(Query) == 1:
        S = S[::-1]
        #print(S)
    else:
        if Query[1] == '1':
            S = Query[2] + S
            #print(S + '+++1')
        else:
            S += Query[2]
            #print(S + '+++2')

print(S)

Submission status: Time Limit Exceeded

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


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

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

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

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

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

S = input().rstrip()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]

ans = S
flag = True

for i in range(Q):
    if query[i][0] == '1':
        if flag:
            flag = False
        else:
            flag = True
        continue
    elif query[i][0] == '2':
        f = query[i][1]
        c = query[i][2].rstrip()
        if f == '1' and flag:
            ans = c + ans
        elif f == '1' and not flag:
            ans = ans + c
        elif f == '2' and flag:
            ans = ans + c
        elif f == '2' and not flag:
            ans = c + ans
if flag:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

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

q_list = []

rev_c = 0
count = 0

for _ in range(q):
  v = input()
  q_list.append(v)
  if v == '1':
    rev_c += 1

if rev_c % 2 == 1:
  s = s[::-1]
  
for v in q_list:
  if v == '1':
    count += 1
  else:
    num = int(v[2])
    char = v[4]
    if ((rev_c-count)%2==0 and num==1) or  ((rev_c-count)%2==1 and num == 2):
      s = ''.join([char,s])
    else:
      s = ''.join([s,char])
print(s)

Submission status: Time Limit Exceeded

Solution code:
A=input()
N=int(input())
for i in range(N):
  B=input().split()
  if int(B[0])==1:
    A = A[::-1]
  else:
    if int(B[1])==1:
      A=B[2]+A
    else:
      A=A+B[2]
print(A)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
count = 1
rc = 0
while count < q+1:
    line = list(input())
    if line[0] == "2":
        if rc%2 != 0:
            s = s[::-1]
            rc = 0
        if line[2] == "1":
            s = line[4] + s
        else:
            s = s + line[4]
    else:
        rc += 1
    count += 1
if rc%2 != 0:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

print(''.join(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":
            slist = [query[2],s]
            s = ''.join(slist)
            # s = query[2] + s
        else:
            slist = [s,query[2]]
            s = ''.join(slist)
            # s = s + query[2]

    #print("{}回目:{}".format(i+1,s))

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
S = []
for i in range(len(s)):
  S.append(s[i])
Q = int(input())
flag = 0
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        flag += 1
    else:
        if flag % 2 == 0:         
            if q[1] == "1":
                S.insert(0,q[2])
            else:
                S.append(q[2])
        else:
            if q[1] == "1":
                S.append(q[2])
            else:
                S.insert(0,q[2])
if flag%2 == 0:
    S = "".join(S)
    print(S)
else:
    S = S[::-1]
    S = "".join(S)
    print(S)

Submission status: Time Limit Exceeded

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

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

# リバースフラグ
r_flag = 0

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

print(get_str)

Submission status: Time Limit Exceeded

Solution code:
a=input()
q=int(input())
val=0
for i in range(q):
    b=input().split()
    if b[0]=="1":
        val+=1
    else:
        if b[1]=="1":
            if val%2==0:
                a=b[2]+a
            else:
                a+=b[2]
        else:
            if val%2==0:
                a+=b[2]
            else:
                a=b[2]+a
if val%2==0:
    print(a)
else:
    print(a[::-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
q_list=[input().split() for _ in range(q)]
for temp in q_list:
  if int(temp[0])==1:
    s = ''.join(list(reversed(s)))
    #s=s[::-1]
  else:
    if int(temp[1])==1:
      s=temp[2]+s
    else:
      s=s+temp[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
num = 0
ans = "0"

for _ in range(q):
  query = list(input().split())
  if len(query) == 1:
    num += 1
  else:
    if int(query[1]) == 1:
      if num%2 == 0:        
        # 先頭
        ans = query[2] + ans
      else:
        ans = ans + query[2]
    else:
      if num%2 == 0:
        # 後方
        ans = ans + query[2]
      else:
        ans = query[2] + ans
        
mes = ans.replace("0", s)
if num%2 == 0:
  print(mes)
else:
  print(mes[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
pre, post = [], []
Q = int(input())
reversed = False
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    reversed = not reversed
    pre, post = post, pre
  else:  # query[0] == 2
    F, C = query[1:]
    if F == '1':
      if reversed:
        pre.append(C)
      else:
        pre = [C] + pre
    else:  # F == '2'
      if reversed:
        post = [C] + post
      else:
      	post.append(C)
if reversed:
  print(''.join(pre[::-1]) + S[::-1] + ''.join(post[::-1]))
else:
  print(''.join(pre) + S + ''.join(post))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Q_list = list()
for i in range(Q):
    Q_list.append(input().split())

def operate(i):
    global count, S_n, word
    str_list = [S_n]
    if Q_list[i][0] == '1':
        count += 1
    else:
        if count % 2 == 0:
            if Q_list[i][1] == '1':
                word = [Q_list[i][2]] + str_list
            else:
                word = str_list + [Q_list[i][2]]
        else:
            if Q_list[i][1] == '1':
                word = str_list + [Q_list[i][2]]
            else:
                word = [Q_list[i][2]] + str_list
    
    S_n = ''.join(word)

S_list = [i for i in S]
check = 0
for i in range(Q):
    if Q_list[i][0] == '1':
        check += 1

if check % 2 == 1:
    S_list = list(reversed(S_list))

S_n = ''.join(S_list)

count = check % 2
for i in range(Q):
    operate(i)
print(''.join(S_n))

Submission status: Time Limit Exceeded

Solution code:
import sys


S = input()
Q = int(input())
h = 0

for i in range(Q):
    A = sys.stdin.readline()
    if A[0] == '1' and len(S)!=1:
        h = h + 1
    else:
        if h % 2 == 0:
            if A[2] == '1':
                S = A[4]+S
            else:
                S = S+A[4]
        else:
            if A[2] == '1':
                S = S+A[4]
            else:
                S = A[4]+S

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

Submission status: Time Limit Exceeded

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

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

print(*s, sep="")

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S_deque = deque(input())
Q = int(input())

for _ in range(Q):
  item = list(input().split())
  if item[0] == '1':
    S_deque = deque(reversed(S_deque))

  else:
    if item[1] == '1':
      S_deque.appendleft(item[2])

    else:
      S_deque.append(item[2])

S = list(S_deque)
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
q = [list(map(str, input().split())) for _ in range(n)]

for i in range(n):
    if q[i][0] == "1":
        s = s[::-1]
    if q[i][0] == "2":
        if q[i][1] == "1":
            s.insert(0, q[i][2])
        elif q[i][1] == "2":
            s.append(q[i][2])

print("".join(s))

Submission status: Time Limit Exceeded

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

sign = 1
head = ""
tail = ""
for i in range(Q):
    Q = [i for i in input().split()]
    if Q[0] == "1":
        sign *= -1
    else:
        if Q[1] == "1" and sign == 1:
            head = Q[2] + head
        elif Q[1] == "1" and sign == -1:
            tail = tail + Q[2]
        elif Q[1] == "2" and sign == 1:
            tail = tail + Q[2]
        elif Q[1] == "2" and sign == -1:
            head = Q[2] + head

if sign==1:
    print(head + S + tail)
elif sign==-1:
    S = (head + S + tail)[::-1]
    print(S)

Submission status: Time Limit Exceeded

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

if is_reversed:
    print(reversed_val)
else:
    print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()

q = int(input())
r = 0
b = ""
c = ""
for i in range(q):
    A = list(input().split())
    if A[0] == '1':
        r += 1
    else:
        if (A[1] == '1' and r % 2 == 0) or (A[1] != '1' and r % 2 != 0):
            b = A[2] + b
        else:
            c = c + A[2]
s = b+s+c
if r % 2 == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

T = 0
F = 1
C = 2

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

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

# print(s,q,query)

cnt = 0

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

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

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

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

Submission status: Time Limit Exceeded

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

result_list = list(S)
for i, que in enumerate(Query):
    if len(que) == 1:
        result_list.reverse()
        
    else:
        que_list = que.split()
        if que_list[1] == "1":
            result_list.insert(0, que_list[2])
        else:
            result_list.append(que_list[2])

print(''.join(result_list))

Submission status: Time Limit Exceeded

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

rev = False

for query in QUERY:
    # 反転の操作
    if query[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    else:
        # 先頭に追加する場合
        if query[1] == '1':
            # 反転しているとき
            if rev:
                # 末尾に追加
                S = S + query[2]
            # 反転していないとき
            else:
                # 先頭に追加
                S = query[2] + S
        # 末尾に追加する場合
        else:
            # 反転しているとき
            if rev:
                # 先頭に追加
                S = query[2] + S
            # 反転していないとき
            else:
                # 末尾に追加
                S = S + query[2]
if rev:
    print(S[::-1])
else:
    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:
a = input()
q = int(input())
i=0
for i in range(q):
    qi=input()
    if qi==str(1):
        a = a[::-1]
    else:
        qi = qi.split()
        if qi[1]==str(1):
            a = qi[2]+a
        elif qi[1]==str(2):
            a = a + qi[2]
print(a)

Submission status: Time Limit Exceeded

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

def main():
    s = input()[:-1]
    q = int(input())
    tmp = [input().split() for _ in range(q)]

    for i in range(len(tmp)):
        if len(tmp[i]) == 1:
            s = s[::-1]
        else:
            f = int(tmp[i][1])
            c = tmp[i][2]
            if f == 1:
                s = c + s
            else:
                s = s + c
                
    print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


for i in range(Q):
  if 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:
s = input()
q = int(input())
def f(s):
    if s == '1':
        return '1' , '0' , '0'
    else:
        lst = s.split()
        return lst[0], lst[1], str(lst[2] )
qu = [f(input()) for i in range(q)]

def hanten(c):
    new_c = c[::-1]
    return new_c
def f1(c, d):
    return d + c
def f2(c, d):
    return c + d

for a, b, c in qu:
    if a == '1':
        s= hanten(s)
    if a=='2' and b=='1':
            d = c
            s = f1(s, d)
    if a=='2'and b== '2':
            d = c
            s = f2(s,d)
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

s = list(sys.stdin.readline().rstrip())
q = int(sys.stdin.readline())
s = deque(s)

for _ in range(q):
    query = sys.stdin.readline().split()
    if len(query) == 1:
        s.reverse()
    elif query[1] == '1':
        s.appendleft(query[2])
    else:
        s.append(query[2])

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
import sys
a = sys.stdin.read().split('\n')[:-1]
s = list(a[0])
q = int(a[1])
f = 1
for i in range(2, q + 2):
    if a[i][0] == '1':
        f = 1 - f
    else:
        l = len(s)
        s.insert(0 if a[i][2] == '21'[f] else l, a[i][4])
if f == 1:
    print(''.join(s))
else:
    print(''.join(reversed(s)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    elif Query[1] == "1" and flag == 1:
        S.append(Query[2])

    elif Query[1] == "2" and flag == 0:
        S.append(Query[2])

    elif Query[1] == "2" and flag == 1:
        S.insert(0, Query[2])

if flag == 0:
    print("".join(S))

else:
    S.reverse()
    print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import copy

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

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

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

Submission status: Time Limit Exceeded

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


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

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

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

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

    for i, q in enumerate(Queries):
        if q == 1:
            continue
        else:
            if q[1] == 1:
                if cumsum_Queries[i] % 2 == 0:
                    S = q[2] + S
                else:
                    S += q[2][::-1]
            else:
                if cumsum_Queries[i] % 2 == 0:
                    S += q[2]
                else:
                    S = q[2][::-1] + S
    print(S)
    pass

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#D

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

for i in range(q):
    t = input().split()
    
    if t[0] == "1":
        s = "".join(list(reversed(s)))
    else:
        if t[1] == "1":
            s = t[2] + s
        else:
            s += t[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()

Q = int(input())
temp = S
for i in range(Q):
    List=list(input().split())
    if int(List[0]) == 1:
        temp = temp[::-1]
    else:
        if int(List[1])== 1:
            temp = List[2]+temp
        else:
             temp = temp+List[2]


print(temp)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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


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

Submission status: Time Limit Exceeded

Solution code:
S=str(input())
Q=int(input())
a=[]
c=[0 for i in range(Q+1)]
for i in range(Q):
  T=list(map(str,input().split()))
  a.append(T)
  if a[i][0]=='1':
    c[i+1]=c[i]+1
  else:
    c[i+1]=c[i]
C=c[Q]
if C%2==1:
  S=S[::-1]
for i in range(Q):
  if a[i][0]=='2' and C%2==0:
    if (a[i][1]=='1' and c[i]%2==0) or (a[i][1]=='2' and c[i]%2==1): 
      S=a[i][2]+S
    else:
      S=S+a[i][2]
  elif a[i][0]=='2' and C%2==1:
    if (a[i][1]=='1' and c[i]%2==0) or (a[i][1]=='2' and c[i]%2==1): 
      S=S+a[i][2]
    else:
      S=a[i][2]+S
print(S)

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:
            if tmp[2] == '1':
                s = tmp[4] + s
            else:
                s = s + tmp[4]
    print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
A=input()
N=int(input())
for i in range(N):
  B=input().split()
  if int(B[0])==1:
    A = ''.join(list(reversed(A)))
  else:
    if int(B[1])==1:
      A=B[2]+A
    else:
      A=A+B[2]
print(A)

Submission status: Time Limit Exceeded

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

#list = [input().split() for l in range(q)]
for i in range(q):
    list = input().strip().split()
    if(list[0]=='1'):
        hanten = not hanten
    elif((list[1]=='1' and not hanten) or (list[1]=='2' and hanten)):
        s = list[2] + s
    else:
        s = s + list[2]

if(hanten):
    s = s[::-1]


print(s)

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)))
  elif 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 = list(input())
Q = int(input())
query = [input().split() for i in range(Q)]
for i in query:
    if i[0] == '1':
        S = list(reversed(S))
    elif i[0]=='2' and i[1]=='1':
        S.insert(0, i[2])
    elif i[0]=='2' and i[1]=='2':
        S.append(i[2])
print(''.join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

rev = False
for i in range(Q):
  q = input()
  if q[0] == '1':
    rev = not rev
  else:
    if q[2] == '1' and rev or q[2] == '2' and not rev:
      S += q[4]
    else:
      S = q[4] + S
      
print(S[::-1] if rev else S)