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 = list(input().strip())
q = int(input().strip())
queries = []
one_count = 0
one_counts = []
for i in range(q):
    inp = input().split()
    queries.append(inp)
    if inp[0] == "1":
        one_count += 1
    one_counts.append(one_count)
one_counts = list(map(lambda x: (one_count - x), one_counts))
if one_count % 2 == 1:
    s = s[::-1]
for i in range(q):
    if queries[i][0] == "1":
        continue
    elif queries[i][0] == "2":
        if queries[i][1] == "1":
            if one_counts[i] % 2 == 0:
                s.insert(0, queries[i][2])
            else:
                s.append(queries[i][2])
        elif queries[i][1] == "2":
            if one_counts[i] % 2 == 0:
                s.append(queries[i][2])
            else:
                s.insert(0, queries[i][2])
print("".join(s))

Submission status: Time Limit Exceeded

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

cnt = 0
FR = ""
BA = ""

for i in range(Q):
    if len(L[i]) == 1:
        cnt += 1
    else:
        if (int(L[i][1])+cnt)%2 == 0:
            BA = BA + L[i][2]
        else:
            FR = L[i][2]+FR

if cnt%2 == 0:
    print(FR+S+BA)
else:
    new_S = ''.join(list(reversed(FR+S+BA)))
    print(new_S)

Submission status: Time Limit Exceeded

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


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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

s=list(str(input()))
q=int(input())
hantei = 0

for i in range(q):
    uery=list(map(str,input().split()))
    
    if len(uery)==1:
        hantei += 1
        
    elif len(uery)>1:
        if uery[1]=="1":
            
            if hantei%2==0:
                s.insert(0,uery[2])
            else:
                s.append(uery[2])
            
        elif uery[1]=="2":
            if hantei%2==0:
                s.append(uery[2])
            else:
                s.insert(0,uery[2])

if hantei%2==1:
    s.reverse()
    print("".join(s))
else:
    print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
Q=int(input())
cnt=0
for i in range(Q):  
  L=list(input().split())  
  if L[0]=="1":
    cnt+=1
  else:
    if cnt%2==1:
      S=list(reversed(S))
      cnt=0
    if L[1]=="1":
      S=list(L[2])+S
    else:
      S=S+list(L[2])
if cnt%2==1:
  S=list(reversed(S))
ans=''.join(S)
print(ans)

Submission status: Time Limit Exceeded

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


reverse = False
start_text = ''
end_text = ''
for _ in range(Q):
    input_text = list(input().split())
    if len(input_text) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        if input_text[1] == '1':  # 先頭に
            if reverse:
                end_text += str(input_text[2])
            else:
                start_text = str(input_text[2]) + start_text
        else:  # 末尾に
            if reverse:
                start_text = str(input_text[2]) + start_text
            else:
                end_text += str(input_text[2])

if reverse:
    text = ''.join(texts)
    text = start_text + text + end_text
    text = list(text)[::-1]
    print(''.join(text))
else:
    text = ''.join(texts)
    text = start_text + text + end_text
    print(text)

Submission status: Time Limit Exceeded

Solution code:
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':
        """
        print('query 1 ', end="")
        print(q)
        S = S[::-1]
        """
        rev = (rev+1) % 2
    else:
        """
        print('query 2 ', end="")
        print(q)
        """
        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())
pre = 0
reverse = 0
for _ in range(q):
    query = input()
    if query == '1':
        if pre:
            pre = 0
        else:
            pre = 1
        reverse += 1
    else:
        _, f, c = query.split()
        if f == '1':
            if pre == 0:
                s = c + s
            else:
                s = s + c
        else:
            if pre == 1:
                s = c + s
            else:
                s = s + c
if reverse % 2 == 1:
    s = s[::-1]
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 = "".join([S,Q[i][2]])#先頭
        else :
            S1 = "".join([Q[i][2],S])
            S = S1

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        s = s[::-1]
    else:
        if a[1] == "1":
            s.insert(0,a[2])
        else:
            s.append(a[2])
print(*s,sep="")

Submission status: Time Limit Exceeded

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

    for i in range(Q):
        # print(S)
        q = list(input().split())

        if q[0] == '1':
            if len(S) > 1:
                S = S[::-1]
        elif q[0] == '2':
            if q[1] == '1':
                S = q[2] + S
            elif q[1] == '2':
                S = S + q[2]
            else:
                raise ValueError(
                    'F({}) is {}, which is invalid!'.format(i, q[1]))
        else:
            raise ValueError('T({}) is {}, which is invalid!'.format(i, q[0]))

    print(''.join(S))


##########
d()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

l_q = []
for i in range(q):
  l_q.append( list(input().split())  ) 

#クエリ処理
c = 0
for i in l_q:

  if i[0] == "1":
    c = (c+1)%2

  else:
    if (c+int(i[1]))%2 ==1:
      s = i[2] + s
    else:
      s = s + i[2]  
  
if c == 1:
  s = ''.join(list(reversed(s)))
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

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

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(len(L)):
    if L[i][0] == '1':
        count += 1
    if L[i][0] == '2':
        if (count % 2) == 0:
            if L[i][1] == '1':
                S = [L[i][2]] + S
            else:
                S = S + [L[i][2]]
        else:
            if L[i][1] == '2':
                S = [L[i][2]] + S
            else:
                S = S + [L[i][2]]



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

Submission status: Time Limit Exceeded

Solution code:
s = [i for i in input()]
Q = int(input())
 
f = 0
 
for _ in range(Q):
    q = input()
    if int(q[0]) == 1:
        f = abs(f-1)
        continue
    
    _, F, c = q.split()
    if f+int(F) % 2 == 1:
        s = [c] + s
    else:
        s = s + [c]
        
if f:
    print(''.join(s[::-1]))
else:
    print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
Q=int(input())
for i in range(Q):  
  L=list(input().split())  
  if L[0]=="1":
    S=list(reversed(S))
  else:
    if L[1]=="1":
      S=list(L[2])+S
    else:
      S=S+list(L[2])
ans=''.join(S)
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(1,Q+1):
    T = input()
    if len(T) == 1:
        L = ''
        for j in range(len(S)):
            L += S[-j-1]
        S = L
    else:
        if T[2] == '1':
            S = T[4] + S
        elif T[2] == '2':
            S = S + T[4]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n=int(input())
l = [input().split(" ") for _ in range(n)]

for i in range(n):
    if l[i][0]=='1':
        s=s[::-1]
    elif l[i][0]=='2':
        if l[i][1]=='1':
            s = [l[i][2]]+s
        else:
            s.append(l[i][2])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math as m

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

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

    if now == "f":
        now = "f"

        for q in qs:
            if q[0] == "1":
                if now == "b":
                    now = "f"
                else:
                    now = "b"
            else:
                if (q[1] == "1" and now == "f") or (q[1] == "2" and now == "b"):
                    front += q[2]
                else:
                    back += q[2]

        res = front[::-1] + s + back
    else:
        now = "f"

        for q in qs:
            if q[0] == "1":
                if now == "b":
                    now = "f"
                else:
                    now = "b"
            else:
                if (q[1] == "1" and now == "f") or (q[1] == "2" and now == "b"):
                    front += q[2]
                else:
                    back += q[2]
        res = back[::-1] + s[::-1] + front
        
    return res

if __name__ == "__main__":
    cin = np.array(input().split(" "))
    s, *_ = cin
    cin = np.array(input().split(" ")).astype("int")
    n, *_ = cin

    qs = []

    for i in range(n):
        cin = np.array(input().split(" "))
        qs.append(list(cin))
    
    cout = main(s=s, n=n, qs=qs)
    print(cout)

Submission status: Time Limit Exceeded

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



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 li[0] == '1':
        S.reverse()
    else:
        if li[1] == '1':
            S[0:0] = li[2]
        elif li[1] == '2':
            S.extend(li[2])
        
print(''.join(S))

Submission status: Time Limit Exceeded

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

for q in querys:
    query = q.split()
    if len(query) == 1:
        is_reverse = not is_reverse
        continue
    _, f, c = query
    if is_reverse:
        
        if f == "1":
            S = S + c
        elif f == "2":
            S = c + S
    else:
        if f == "1":
            S = c + S
        elif f == "2":
            S = S + c
if is_reverse:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

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

for i in range(1,Q+1):
    T = input()
    if len(T) == 1:
        L = ''
        for j in range(len(S)):
            L += S[-j-1]
        S = L
    else:
        if T[2] == '1':
            S = T[4] + S
        elif T[2] == '2':
            S = S + T[4]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
o = 1
for i in range(q):
    x = input().split()
    if x[0] == "1":
        o = o*-1
    else:
        if (x[1] == "1" and o == 1) or (x[1] == "2" and o == -1):
            s = x[2] + s
        else:
            s = s + x[2]

s = s[::o]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

d = deque(['S'])

#d = ['S']

rev = 0

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

#ans = deque()

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

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

#print(*ans,sep='')

Submission status: Time Limit Exceeded

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

import sys
import math

debug = False

def log(text):
    if debug:
        print(text)


def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        # for line in sys.stdin:
        #     lines.append(line)
        lines.append(input())
        lines.append(input())
        q = int(lines[1])
        for _ in range(q):
            lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    
    s = lines[0]
    q = int(lines[1])
    query = []
    for i in range(2, 2+q):
        contents = lines[i].split(' ')
        if len(contents) == 1:
            query.append((contents[0], None, None))
        else:
            query.append(contents)

    return (s, q, query)


def solve(s, q, query):


    query2 = []
    reverse_count = 0
    tmp_reverse_count = 0
    for i in range(q):
        (t, f, c) = (query[i][0], query[i][1], query[i][2])
        if t == '1':
            reverse_count = reverse_count + 1
            tmp_reverse_count = tmp_reverse_count + 1
            if i == q - 1 and tmp_reverse_count > 0:
                if tmp_reverse_count % 2 == 0:
                    pass
                else:
                    query2.append(('1', None, None))
                tmp_reverse_count = 0
        else:
            if tmp_reverse_count > 0:
                if tmp_reverse_count % 2 == 0:
                    pass
                else:
                    query2.append(('1', None, None))
                tmp_reverse_count = 0
            query2.append(query[i])
                


    tmp = ' ' 
    for (t, f, c) in query2:
        if debug:
            log("t=%s, f=%s, c=%s" % (t, f, c))
        if t == '1':
            tmp = tmp[::-1]
        else:
            if f == '1':
                tmp = c + tmp
            else:
                tmp = tmp + c

    tmp2 = s
    if reverse_count % 2 == 1:
        tmp2 = s[::-1]

    result = tmp.replace(' ', tmp2)

    return result 


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Time Limit Exceeded

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

l_empty = []

for i in range(q):
    l_empty = []
    #t
    t=input().split()
    if t[0] == "1":
        s = s[::-1]
    elif t[1] == "1":
        l_empty.append(t[2])
        l_empty.append(s)
        s = "".join(l_empty)
    else:
        l_empty.append(s)
        l_empty.append(t[2])
        s = "".join(l_empty)
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
ans = ''
flag = False
cnt = 0

for i in range(q):
    l = list(map(str,input().split()))
    t = int(l[0])-1
    if t:
        f = int(l[1])
        c = str(l[2])
        if f + flag == 2:
            s = s + c
        else:
            s = c + s
    else:
        flag = not flag
        cnt += 1

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

S_deque = list(input())
S_deque = deque(S_deque[:-1])
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 = input()
Q = int(input())
qlst = []
for i in range(Q):
    qlst.append(input().split())
ans = S
rtmp = 0
for i in range(Q):
    if qlst[i][0] == '1':
        rtmp += 1
    else:
        if rtmp % 2 == 0:
            if qlst[i][1] == '1':
                ans = qlst[i][2] + ans
            else:
                ans = ans + qlst[i][2]
        else:
            if qlst[i][1] == '2':
                ans = qlst[i][2] + ans
            else:
                ans = ans + qlst[i][2]

if rtmp % 2 != 0:
    print(ans[::-1])
else:
    print(ans)

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):
            # s = A[2] + s
            b = A[2] + b
        else:
            # s = s+ A[2]
            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:

S=list(str((input())))
Q=int(input())
t=0
for _ in range(Q):
    T=list(map(str,input().split()))
    if T[0]=="1":
        t+=1
    else:
        if (T[1]=="1" and t%2 == 0) or (T[1]=="2" and t%2 != 0):
            S.insert(0,T[2])
        else:
            S.append(T[2])


if t%2 != 0:
    S.reverse()

print("".join(S))

Submission status: Time Limit Exceeded

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

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
i_str = str(input())
q_number = int(input())

for i in range(q_number):
    #print('i_str',i_str)
    lines = input().split()
    if len(lines)>1:
        if lines[1] == '1':
            i_str = lines[2] + i_str
        elif lines[1] == '2':
            i_str = i_str + lines[2]
    else:
        i_str = i_str[::-1]
print(i_str)

Submission status: Time Limit Exceeded

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

c = 0
for i in range(n):
    if q[i][0] == "1":
        c += 1
    if q[i][0] == "2":
        if q[i][1] == "1" and c % 2 == 0 or q[i][1] == "2" and c % 2 == 1:
            s.insert(0, q[i][2])
        elif q[i][1] == "2" and c % 2 == 0 or q[i][1] == "1" and c % 2 == 1 :
            s.append(q[i][2])

if c % 2 == 1:
    s = s[::-1] 

print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            T1 += 1
        else:
            if T1 % 2 == 0:
                s = FrontS + s + BackS
                FrontS = ""
                BackS = ""

                if T[1] == "1":
                    FrontS = T[2] + FrontS
                else:
                    BackS += T[2]
            else:
                s = FrontS + s + BackS
                FrontS = ""
                BackS = ""
                
                if T[1] == "1":
                    BackS = T[2] + BackS
                else:
                    FrontS = T[2] + FrontS
    
    if BackS != "":
        s += BackS
    if FrontS != "":
        s = FrontS + s

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
n = int(input())
q = [input().split() for x in range(n)]
for i in range(n):
    if int(q[i][0]) == 1:
        s = "".join(list(reversed(s)))
    else:
        if int(q[i][1]) == 1:
            s = "{}{}".format(q[i][2], s)
        else:
            s = "{}{}".format(s, q[i][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)]
    cumsum_Queries_preprocessing = [1 if q[0] == '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[0] != '1':
            if q[1] == '1':
                if s % 2 == 0:
                    S = q[2] + S
                else:
                    S += q[2]
            else:
                if s % 2 == 0:
                    S += q[2]
                else:
                    S = q[2] + S
    print(S)
    pass

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

cnt_reverse = 0
for i in range(q):
  query = input()
  
  if query[0] == '1':
    cnt_reverse += 1
  elif (query[2] == '1' and cnt_reverse % 2==0) or (query[2] == '2' and cnt_reverse % 2==1):
    s = query[4] + s
  else:
    s  = s + query[4]
if cnt_reverse%2==1:
  s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
check = 0
Query = []
for i in range(Q):
  Query.append(list(input()))

for i in range(Q):
  if len(Query[i]) == 5:
    Query[i].pop(1)
    Query[i].pop(2)
    Query[i][0], Query[i][1] = int(Query[i][0]), int(Query[i][1])
  else:
    Query[i][0] = int(Query[i][0])

for i in range(Q):
  if check % 2 == 0:
    if Query[i][0] == 1:
      check += 1
    else: #Query[i][0] == 2
      if Query[i][1] == 1:
        S = Query[i][2] + S
      else: #Query[i][1] == 2
        S = S + Query[i][2]
  else: # check % 2 == 1
    if Query[i][0] == 1:
      check += 1
    else: #Query[i][0] == 2
      if Query[i][1] == 2:
        S = Query[i][2] + S
      else: #Query[i][1] == 2
        S = S + Query[i][2]

if check % 2 == 1:
  S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
#n=int(input())
#n,m=map(int,input().split())
#a=[int(x) for _ in input().split()]

s=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]
    t.clear
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 += ans[i]
    else:
        for i in range(len(ans)):
            s_ans += ans[i]
    
    print(s_ans)
                
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

s = input()
q = int(input())
ans = []
ans_s = ''
one_count = 0
key = 0
for i in range(q):
    ans.append(list(input().split()))
for i in ans:
    if int(i[0])==1:
        one_count += 1
    else:
        if one_count%2==0:
            if int(i[1]) == 1:
                #ans_q.insert(0,i[2])
                s = i[2]+s
                #ans_s = ans_s+i[2]
            else:
                s = s+i[2]
                #ans_q.insert(len(ans_q),i[2])
                #ans_s = i[2]+ans_s
                #key += 1
        else:
            if int(i[1]) == 1:
                #ans_q.insert(len(ans_q),i[2])
                s = s+i[2]
                #ans_s = i[2]+ans_s

            else:
                #ans_q.2insert(0,i[2])
                s = i[2]+s
                #ans_s = ans_s+i[2]

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

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:
    ans = ''.join(list(reversed(ans)))
#    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:
S=input()
Q=int(input())
import sys
flag=False
 
for _ in range(Q):
  s = list(sys.stdin.readline())
  if s[0]=="1":
    if flag:
      flag=False
    else:
      flag=True
  else:
    if s[2]=="1":
      if flag:
        S=S+s[4]
      else:
        S=s[4]+S        
    else:
      if flag:
        S=s[4]+S
      else:              
        S=S+s[4]
        
if flag:
  S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
d = input()
Q = int(input())
for q in range(Q):
    ls = input().split()
    if ls[0] == '1':
        d = d[::-1]
    else:
        if (ls[1]== '1'):
            
            d = ''.join([ls[2],d])
        else:
            
            d = ''.join([d,ls[2]])
print (d)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = input()
for i in range(int(n)):
    tmp = input()
    if len(tmp) == 1:
        s = s[::-1]
    else:
        t, f, c = tmp.split()
        if int(f) == 1:
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
#文字列
s=list(input())
n=int(input())
for i in range(n):
    li = input().split()
    if(li[0]=="1"):
        s.reverse()
    else:
        if(li[1]=="1"):
            s.insert(0, li[2])
        else:
            s.append(li[2])

print("".join(s))

Submission status: Time Limit Exceeded

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

    for query_list in query:
        if query_list[0] == '1':
            s = s[::-1]
        else:
            if query_list[1] == '1':
                s = s[::-1]
                s.append(query_list[2])
                s = s[::-1]
            else:
                s.append(query_list[2])

    print(''.join(s))

main()

Submission status: Time Limit Exceeded

Solution code:
s = input()

n = int(input())

for i in range(n):
    tmp = input()
    t = int(tmp[0])
    if t==1:
        s=s[-1::-1]
    elif t == 2:
        f = int(tmp[2])
        if f == 1:
            s = tmp[4]+s
        else:
            s = s+tmp[4]
print(s)