Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Time Limit Exceeded

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

for query in query_q:
    if query[0] == '1':
        S = S[::-1]
    if query[0] == '2':
        if query[1] == '1':
            S = query[2] + S
        elif query[1] == '2':
            S = S + query[2]

print(S)

Submission status: Time Limit Exceeded

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

for i in range(0,Q):
  lst = list(input().split(" "))
  if lst[0] == "1":
    S = S[::-1]
  elif lst[0] == "2" and lst[1] == "1":
    S.insert(0,lst[2])
  elif lst[0] == "2" and lst[1] == "2":
    S.append(lst[2])
  
  
print("".join(S))

Submission status: Time Limit Exceeded

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

mode = True

for i in range(Q):
  TFC = list(map(str, input().split()))
  if TFC[0] == "1":
    mode = not mode
    continue
  else:
    if mode:
      if TFC[1] == "1":
        S = TFC[2] + S
      else:
        S = S + TFC[2]
    else:
      if TFC[1] == "1":
        S = S + TFC[2]
      else:
        S = TFC[2] + S
        
if mode:
  print(S)
  exit()
print(S[::-1])

Submission status: Time Limit Exceeded

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

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

            s = operation[4] + s
        else:
            s += operation[4]


print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = []
for _ in range(q):
  query.append(input().split())

for i in query:
  if len(i) == 1:
    s = s[::-1]
  else:
    _, f, c = i
    if f == "1":
      s = c + s
    else:
      s = s + c
print(s)

Submission status: Time Limit Exceeded

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

# qs = list()
# for i in range(q):
#     qs.append(input().split())

ns = s
for i in range(q):
    query = input().split()
    if len(query) == 1:
        ns = ns[::-1]
    else:
        f, c = int(query[1]), query[2]
        if f == 1:
            ns = c + ns
        else:
            ns = ns + c

print(ns)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def try_int(s):
    try:
        return int(s)
    except:
        return s

s = input()
nq = int(input())
flg = 0
for i in range(nq):
    q = [try_int(p) for p in input().split()]

    if len(q) == 1:
        flg += 1
    else:
        if flg % 2 == 0:
            if q[1] == 1:
                s = q[2]+s
            else:
                s = s+q[2]
        else:
            if q[1] == 1:
                s = s+q[2]
            else:
                s = q[2]+s
if flg % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
rev = False
for _ in range(int(input())):
  t = input()
  if t == "1":
    rev = not rev
  else:
    t = t.split()
    if (t[1] == "1" and not rev) or (t[1] == "2" and rev):
      s = t[2] + s
    else:
      s += t[2]
if rev:
  for i in reversed(s):
    print(i, end="")
else:
  print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = deque([x for x in input()])
Q = int(input())
for _ in range(Q):
    inp = input()
    if len(inp) == 1:
        T = int(inp)
        S.reverse()
    else:
        T, F = map(int, inp.split()[:2])
        C = inp.split()[-1]
        if F == 1:
            S.appendleft(C)
        else:
            S.append(C)

print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def addflont(s,c):
    return c+s
def addback(s,c):
    return s+c

reverse = 0

for i in range(q):
    t = [str(i) for i in input().split()]

    if t[0]=="1":
        reverse += 1
    if t[0]=="2":
        if t[1]=="1":
            if reverse%2==0:
                s = addflont(s,t[2])
            else:
                s = addback(s,t[2])
        elif t[1]=="2":
            if reverse%2==0:
                s = addback(s,t[2])
            else:
                s = addflont(s,t[2])

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
ans = S
revers_count = 0

for i in range(Q):
    Q_i = list(input().split())
    if Q_i[0] == "1":
        revers_count += 1
    else:
        if (Q_i[1] == "1" and revers_count % 2 == 0) or (Q_i[1] == "2" and revers_count % 2 == 1):
            ans = Q_i[2] + ans
        else:
            ans = ans + Q_i[2]

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

print(ans)

Submission status: Time Limit Exceeded

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

flag = 0
for x in X:
    if int(x[0]) == 1:
        # s = s[::-1]
        if flag ==0:
            flag = -1
        else:
            flag = 0
        continue
    if int(x[1]) +flag == 1:
        s = x[2] +s
    else:
        s = s +x[2]
if flag == -1:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):
    q_i = input().split(' ')
    t_i = q_i[0]
    if t_i == '1':
        is_r = not is_r
    else:
        f_i = q_i[1]
        if (f_i == '1' and not is_r) or (f_i == '2' and is_r):
            s = q_i[2] + s
        else:
            s = s + q_i[2]

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

Submission status: Time Limit Exceeded

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


for _ in range(Q):
  input = sys.stdin.readline
  s = list(input())
  if s[0]=="1":
    S=S[::-1]
  else:
    if s[2]=="1":
      S="".join([s[4],S])
    else:
      S="".join([S,s[4]])
      
print(S)

Submission status: Time Limit Exceeded

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

hanten = 0
for i in range(q):
    if que[i][0] == "1":
        if hanten == 0:
            hanten = 1
        else:
            hanten = 0
    elif que[i][0] == "2":
        if hanten == 0:
            if que[i][1] == "1":
                sub = [que[i][2], s]
                s = ''.join(sub)
            elif que[i][1] == "2":
                sub = [s,que[i][2]]
                s = ''.join(sub)
        else:
            if que[i][1] == "1":
                sub = [s,que[i][2]]
                s = ''.join(sub)
            elif que[i][1] == "2":
                sub = [que[i][2], s]
                s = ''.join(sub)
if hanten == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

for i in arr:
    if len(i) == 1:
        s = s[::-1]
    else:
        if i[1] == '1':
            s = i[2] + s
        else:
            s = s + i[2]
print(s)

Submission status: Time Limit Exceeded

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

querys = []
for i in range(Q):
    querys.append(input().split())
reverse = 1
for i, query in enumerate(querys):
    # query = querys[i]
    if query[0] == "1":
        S = S[::-1]
    else:
        f = int(query[1])
        c = query[2]
        if f == 1:
            S = c + S
        else:
            S = S + c
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
str_list_ = []
#for q in range(Q):
#    str_list_.append( input().split(' ') )

str_list_ = [input().split(' ') for q in range(Q)]

for q in range(Q):
    #print(q)
    #str_list = input().split(' ')
    str_list = str_list_[q]
    #print(str_list)
    if str_list[0]=='1':
        #head=S[0]
        #tail=S[len(S)-1]
        #S=S[1:]
        #S=tail+S
        #S=S[0:-1]
        #S=S+head
        #S=tail+S[1:-1]+head
        #S=S[len(S)-1]+S[1:-1]+S[0]
        #S='%s%s%s'%(S[len(S)-1],S[1:-1],S[0])
        S=S[::-1]
        #print(S)
    elif str_list[0]=='2':
        if str_list[1]=='1':
            #S=str_list[2]+S
            S='%s%s'%(str_list[2], S)
        elif str_list[1]=='2':
            #S=S+str_list[2]
            S='%s%s'%(S, str_list[2])
        #print(S)
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for q in range(1, Q + 1):
    inp = input().split(" ")
    if (int(inp[0]) == 1):
        S = S[::-1]
    else:
        if (int(inp[1]) == 1):
            S = inp[2] + S
        elif (int(inp[1]) == 2):
            S = S + inp[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input() 
Q = int(input())
process_list = [input().split() for i in range(Q)]  #[ [2, 1, p], [1], [2, 2, c], [1] ]

for lists in process_list:
    if lists[0] == "1":
        S = S[::-1]
    elif lists[0] == "2":
        if lists[1] == "1":
            S = lists[2] + S
        elif lists[1] == "2":
            S = S + lists[2]

print(S)

Submission status: Time Limit Exceeded

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

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

reverse = False

ans = S

for i in range(Q):
    query = input()
    if query == "1":
        reverse = not reverse

    else:
        a,b,c = list(query.split())
        if (b == "1" and not reverse) or (b == "2" and reverse):
            ans = c + ans
        else:
            ans = ans + c

if reverse:
    print(ans[::-1])
else:
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    T = input().split()
    if len(T) == 1:
        S = "".join(list(reversed(S)))
    else:
        if T[1] == "1":
            S = T[2] + S
        else:
            S = S + T[2]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Query = []
for i in range(Q):
  Query.append(list(map(str, input())))

for i in range(len(Query)):
  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 Query[i][0] == 1:
    S = S[::-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]
      
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
S = input()
Q = int(input())
pin = 0
for i in range(Q):
  t = list(sys.stdin.readline().split())
  if int(t[0]) == 1:
    pin += 1
  else:
    if int(t[1]) % 2 == pin % 2 :
      S = S + t[2]
    else:
      S = t[2] + S
if pin % 2 == 1:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = 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 += query[2]
    else:
        s = query[2] + s
if direction % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(Q):
    if T[i][0] == '1':
        cnt += 1
    else:
        if cnt % 2 == 0:
            if T[i][1] == '1':
                S = T[i][2] + S
            else:
                S = S + T[i][2]
        else:
            if T[i][1] == '2':
                S = T[i][2] + S
            else:
                S = S + T[i][2]
if cnt % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
queries = []
rev = [0]*q
flag=0
for i in range(q):
    query = list( input().split() )
    queries.append( query )
    if query==["1"]:
        flag+=1
    rev[i]=flag

n_rev = queries.count(["1"])
rev = [rr+n_rev for rr in rev]
left=""
right=""
for i,query in enumerate(queries):
    if query!=["1"]:
        if rev[i]%2==0:
            if query[1]=="1":
                left=query[2]+left
            else:
                right=right+query[2]
        else:
            if query[1]=="1":
                right=right+query[2]
            else:
                left=query[2]+left 
#     print(i,left,s,right)
if n_rev%2==1:
    s=s[::-1]
print(left+s+right)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for i in range(q):
  que=input().split()
  if que[0]==str(1):
    s=s[::-1]
  else:
    if que[1]==str(1):
      s=que[2]+s
    else:
      s=s+que[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
rev = 0
revcnt = 0
for _ in range(q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        revcnt += 1
        if rev == 0:
            rev = 1
        elif rev == 1:
            rev = 0

    elif query[0] == "2":
        if rev == 0:
            if query[1] == "1":
                s = query[2] + s
            elif query[1] == "2":
                s = s + query[2]
        else:
            if query[1] == "1":
                s = s + query[2]
            elif query[1] == "2":
                s = query[2] + s

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

Submission status: Time Limit Exceeded

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

if(r==-1):
    s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


def main():
    a = input()
    d = deque(a)
    n = int(input())
    for i in range(n):
        j = input().split()
        if len(j) == 3 and j[1] == "1":
            d.appendleft(j[2])
        elif len(j) == 3 and j[1] == "2":
            d.append(j[2])
        else:
            d.reverse()
    ans = ""
    for j in d:
        ans = ans + j
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    a = input()
    Q = int(input())
    qs = [input().split() for _ in range(Q)]
    ans = a
    for q in qs:
        if len(q) == 1:
            # ans = ''.join(list(reversed(ans)))
            ans = ans[::-1]
        else:
            if int(q[1]) == 1:
                ans = q[2] + ans
            else:
                ans = ans + q[2]
    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = 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 = queries[i][2] + s
            else:
                s += queries[i][2]
        elif queries[i][1] == "2":
            if one_counts[i] % 2 == 0:
                s += queries[i][2]
            else:
                s = queries[i][2] + s
print(s)

Submission status: Time Limit Exceeded

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

flag = True

for i in range(Q):
    q = list(map(str, input().split()))
    if len(q) == 1:
        flag = not flag
    else:
        if (q[1] == '1' and flag == True) or (q[1] == '2' and flag == False):
            S.insert(0, q[2])
        else:
            S.append(q[2])

if flag == True:
    print(*S, sep="")
else:
    S.reverse()
    print(*S, sep="")

Submission status: Time Limit Exceeded

Solution code:
def main():
	S=input()                                               # 1行1列
	Q=int(input())                                          # 1行1列
	Q_LIST=[input().split() for i in range(Q)]                # N行C列・・・Cは任意

	dir = 0 # 0が正常

	List = [S]

	for query in Q_LIST:
		if query[0] == '1':
			if dir == 0:
				dir = 1
			else:
				dir = 0

		if dir == 0:
			HEAD = '1'
			BACK = '2'
		else:
			HEAD = '2'
			BACK = '1'

		if query[0] == '2':
			if query[1] == HEAD:
				List.insert(0, query[2])
			if query[1] == BACK:
				List.append(query[2])

	S = ''.join(List)

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    rev = False
    for i in range(q):
        tfc = input().split()

        if tfc[0] == '1':
            rev = not rev
        else:
            f = tfc[1]
            c = tfc[2]
            if (f == '1' and not rev) or (f != '1' and rev):
                s = c + s
            else:
                s = s + c

    if rev:
        s = s[::-1]

    print(s)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
a = input().rstrip()
n = int(input().rstrip())
st = True
for _ in range(n):
    q  = [x for x in input().rstrip().split()]
    if int(q[0]) == 1:
        if st:
            st = False
        else:
            st = True
    else:
        if int(q[1]) == 1:
            if st:
                a = q[2] + a
            else:
                a = a + q[2]
        else:
            if st:
                a = a + q[2]
            else:
                a = q[2] + a
if not st:
    a = a[::-1]
print(a)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query_l = []
for i in range(q):
    if i == 0:
        query_l.append(list(input().split()))
    else:
        query = list(input().split())
        if (query[0] == '1') and (query_l[i-1] == '1'):
            query_l.pop(i-1)
        else:
            query_l.append(query)

for query in query_l:
    if int(query[0]) == 1:
        s = s[::-1]
    else:
        if int(query[1]) == 1:
            s = query[2] + s
        else:
            s = s + query[2]
print (s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    A = list(map(str,input().split()))
    if A[0] == '1' and len(S)!=1:
        h = h + 1
    else:
        if h % 2 == 0:
            if A[1] == '1':
                S = A[2]+S
            else:
                S = S+A[2]
        else:
            if A[1] == '1':
                S = S+A[2]
            else:
                S = A[2]+S

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
count_1=0
l=[]
num =int(input())
for i in range(num):
    q=input().rstrip().split()
    l.append(q)

for i in l:
    if i[0]=="1":
        s=s[::-1]
        
    elif i[1]=="1":
    
        s=i[2]+s
    elif i[1]=="2":
        s=s+i[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    s = input()
    q = int(input())
    qall = []
    for _ in range(q):
        qall.append(input())

    for i in range(q):
        if qall[i] == "1":
            s = s[::-1]
        else:
            z = qall[i].split()
            b = int(z[1])
            c = z[2]
            if b == 1:
                s = c + s
            else:
                s = s + c
    print(s)
resolve()

Submission status: Time Limit Exceeded

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

for i in range(q):
    t = input().split()
    if t[0] == '1':
        s = s[::-1]
    elif t[0] == '2':
        if t[1] == '1':
            s = t[2]+s
        elif t[1] == '2':
            s = s + t[2]

print (s)

Submission status: Time Limit Exceeded

Solution code:
s = 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 = t[4] + s
        else:
            s += t[4]
    else:
        if c%2 == 0:
            s += t[4]
        else:
            s = t[4] + s

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

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

def main():
    S = readline().strip().decode('utf-8')
    Q = int(readline())
    Query = [tuple(readline().decode('utf-8').split()) for _ in range(Q)]
    for q in Query:
        if q[0] == '1':
            S = S[::-1]
        else:
            if q[1] == '1':
                S = q[2] + S
            else:
                S += q[2]
    print(S)

    return
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

def add_head(s, c):
    return c + s

def add_tail(s, c):
    return s + c

f = 0

for _ in range(Q):
    q = input()
    if int(q[0]) == 1:
        f = abs(f-1)
        continue
    
    _, F, c = q.split()
    if (f == 0 and int(F) == 1) or (f == 1 and int(F) == 2):
        s = c + s
    elif (f == 1 and int(F) == 1) or (f == 0 and int(F) == 2):
        s = s + c
        
if f:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
lst = []

for i in range(Q):
    tmp = list(input().split())
    lst.append(tmp)

reverse = 0
c = 1
for i in range(Q):

    if int(lst[i][0]) == 1:
        reverse += 1
        c += 1
    
    if int(lst[i][0]) == 2:
        if c%2 == 1:
            if int(lst[i][1]) == 1:
                S.insert(0,lst[i][2])
            else:
                S.append(lst[i][2])
        else:
            if int(lst[i][1]) == 2:
                S.insert(0,lst[i][2])
            else:
                S.append(lst[i][2])

if reverse%2 == 1:
    S.reverse()

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

Submission status: Time Limit Exceeded

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

def syori(item):
    global S
    if item[0] == '1':
        S.reverse()
    else:
        # Ti = 2 の時
        if item[1] == '1':
            S = [item[2]] + S
        else:
            S = S + [item[2]]

for item in L:
    syori(item)
# print(L)
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------


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

r=0

for i in range(q):
	t = input()
	
	if t=='1':
		r = (r+1)%2
#		p("t,r,s",t,r,s)
		continue
	
	_,f,c = t.split()

	z=r
	if f=='1': z =  r
	if f=='2': z = (r+1)%2
	
	if z==0: s = c + s
	if z==1: s = s + c

#	p("t,r,_,f,c,z,s","[",t,"]",r,_,f,c,z,s)

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

Submission status: Time Limit Exceeded

Solution code:
*s, = input()
_q = int(input())
for _ in range(_q):
    *q, = map(str, input().split())
    q.extend([" ", " "])
    t = q[0]
    f = q[1]
    c = q[2]

    if t == "1":
        *s, = reversed(s)
    else:
        if f == "1":
            s.insert(0, c)
        else:
            s.append(c)

print("".join(s))

Submission status: Time Limit Exceeded

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

for i in range(q):
  query = list(input().split())
  if (int(query[0]) == 1):
    s.reverse()
  else:
    if (int(query[1]) == 1):
      s.insert(0, query[2])
    else:
      s.append(query[2])
  
print(''.join(s))

Submission status: Time Limit Exceeded

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

for i in range(Q):
  list_ = list(input().split())
  T = int(list_[0])
  if T == 1:
    t_count += 1
  else:
    if t_count%2 == 1:
      S.reverse()
      t_count = 0
    F, C = int(list_[1]), list_[2]
    if F == 1:
      S.insert(0,C)
    else:
      S.append(C)
if t_count%2 == 1:
      S.reverse()
ans = ''.join(S)
print(ans)

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:
    s.reverse()
  else:
    if queue[1] == '1':
      s.insert(0,queue[2])
    else:
      s.append(queue[2])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    qus = [tuple(map(str, input().split())) for _ in range(q)]
    for qu in qus:
        if len(qu) == 1:
            s = s[::-1]
        else:
            if qu[1] == '1':
                s = qu[2] + s
            else:
                s = s + qu[2]

    print(s)
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  S=input()
  N=int(input())
  for i in range(N):
    tmp=[i for i in input().split() ]
    if tmp[0]=='1':
      S=''.join(list(reversed(S)))
    else:
      if tmp[1]=='1':
        new=tmp[2]+S
        S=new
      else:
        new=S+tmp[2]
        S=new
  print(S)
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

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

isRev = False

for i in range(q):
    query = list(input().split())
    if len(query) == 1:
        isRev = not isRev
    else:
        if isRev:
            s.reverse()
        if query[1] == '1':
            s = [query[2]] + s
        elif query[1] == '2':
            s = s + [query[2]]
        isRev = False
            
if isRev:
    s.reverse()
        
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]

ans=deque()
for s in S:
  ans.append(s)
for q in query:
  if len(q)==1:
    ans.reverse()
  else:
    _,f,c=q
    if f=='1':
      ans.appendleft(c)
    else:
      ans.append(c)
print(*ans,sep='')

Submission status: Time Limit Exceeded

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

# total = 0
num = 0
for i in range(Q):
    tmp = input().split()

    if tmp[0] == "1":
        # total += 1
        num += 1

    elif tmp[0] == "2":
        num = num % 2
        dada = num + int(tmp[1])

        if dada == 2:
            S = S + tmp[2]

        else:
            S = tmp[2] + S
        # else:
            # S = S + tmp[2]



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

Submission status: Time Limit Exceeded

Solution code:
# ABC158 D
s=input()
q=int(input())
l=[]

for i in range(q):
    l.append(list(map(str,input().split())))
# print(l)
t_str=s
for ll in l:
    if ll[0]=='1':
        t_str=t_str[::-1]
    elif ll[0]=='2':
        if ll[1]=='1':
            t_str=ll[2]+t_str
        elif ll[1]=='2':
            t_str+=ll[2]
print(t_str)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    from collections import deque
    d=deque()
    s=list(input())
    d.extend(s)
    #print(d)
    q=int(input())
    Q=[list(map(str,input().split())) for i in range(q)]

    #print(Q)

    for i in range(q):
        if int(Q[i][0])==1:
            d.reverse()
            #print(s)
        elif int(Q[i][0])==2:
            if int(Q[i][1])==1:
                d.appendleft(Q[i][2])
                #print(s)
            else:
                d.append(Q[i][2])
                #print(s)
    print(''.join(list(d)))






resolve()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
d = deque(S)
for x in query:
    if int(x[0]) == 1:
        d.reverse()
    elif int(x[0]) == 2:
        if int(x[1]) == 1:
            d.appendleft(x[2])
        elif int(x[1]) == 2:
            d.append(x[2])
print(*d, sep='')