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())
hanten = True
atm = ""
kts = ""
for i in range(q):
    l = input().split()
    if l[0] == "1":
        hanten = not hanten
    else:
        if l[1] == "1":
            if hanten == True:
                atm = l[2] + atm
            else:
                kts = kts + l[2]
        else:
            if hanten == True:
                kts = kts + l[2]
            else:
                atm = l[2] + atm

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

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

T_list = []
reverse_flg = False

for q in range(Q):
    T = stdin.readline()[:-1]

    if T == '1':
        if reverse_flg:
            reverse_flg = False
        else:
            reverse_flg = True
    
    else:
        t, f, c = T.split()

        if (f == '1' and reverse_flg == False) or (f == '2' and reverse_flg == True):
            tmp_s = [c, S]
            S = ''.join(tmp_s)
        else:
            tmp_s = [S, c]
            S = ''.join(tmp_s)

if reverse_flg:
    S = ''.join(list(reversed(S)))
    
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

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

T_list = []
reverse_flg = False

for q in range(Q):
    T = stdin.readline()[:-1]

    if T == '1':
        if reverse_flg:
            reverse_flg = False
        else:
            reverse_flg = True
    
    else:
        t, f, c = T.split()

        if (f == '1' and reverse_flg == False) or (f == '2' and reverse_flg == True):
            tmp_s = [c, S]
            S = ''.join(tmp_s)
        else:
            tmp_s = [S, c]
            S = ''.join(tmp_s)

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

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = list(input())

Q = int(input())

cnt = 0
for i in range(Q):
    l = list(map(str,input().split()))
    if int(l[0]) == 1:
        cnt += 1
    else:
        if int(l[1]) == 1:
            if cnt % 2 == 1:
                S.append(l[2])
            else:
                S.insert(0,l[2])
        else:
            if cnt % 2 == 1:
                S.insert(0,l[2])
            else:
                S.append(l[2])

if cnt % 2 == 1:
    S = S[::-1]
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

d = deque(S)

for q in Q:
    if q[0] == "1":
        d.reverse()
    elif q[0] == "2":
        if q[1] == "1":
            d.appendleft(q[2])
        else:
            d.append(q[2])
            
print("".join(d))

Submission status: Time Limit Exceeded

Solution code:

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

tmplist = []
T = 0
for i in range(Q):
    Query = list(input().split())
    if len(Query) == 1:
        T = T+1
    else:
        if T%2 == 0:
            if Query[1] == '1':
                S.insert(0,Query[2])
            else:
                S.append(Query[2])
        else:
            if Query[1] == '2':
                S.insert(0,Query[2])
            else:
                S.append(Query[2])
#    print("len(Query)={}, Query={}, T={}, S={}".format(len(Query), Query,T,''.join(S)))

if T%2 == 1:
    #S.reverse()
    S=S[::-1]

#print("ans={}".format(''.join(S)))
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
hanten = 0
check = 0
for i in range(q):
    l = tuple(map(str, input().split()))
    if l[0] == "1":
        hanten += 1
        check += 1
    else:
        if l[1] == "1":
            if check%2 == 0:
                s = l[2] + s
            else:
                s = s + l[2]
        else:
            if check%2 == 0:
                s = s + l[2]
            else:
                s = l[2] + s

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
hanten = 0
check = 0
for i in range(q):
    l = list(map(str, input().split()))
    if l[0] == "1":
        hanten += 1
        check += 1
    else:
        if l[1] == "1":
            if check%2 == 0:
                s = l[2] + s
            else:
                s = s + l[2]
        else:
            if check%2 == 0:
                s = s + l[2]
            else:
                s = l[2] + s

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

print(s)

Submission status: Time Limit Exceeded

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

reverse = 0
for query in queries:
    if len(query) == 1:
        reverse = 1 - reverse

    else:
        is_append_left = (reverse + int(query[1])) % 2
        if is_append_left:
            S = query[2] + S
        else:
            S += query[2]

if reverse:  # reverse処理は重い．実際，最後の一回で良い．
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
import collections

s = input()
q = int(input())
rev = 0
ans = ['S']

for _ in range(q):
  query = list(input().split())
  if int(query[0]) == 1:
    rev += 1
  else:
    if int(query[1]) == 1:      # 先頭
      if rev%2 == 0:
        ans = [query[2]] + ans
      else:
        ans = ans + [query[2]]
    else:                       # 末尾
      if rev%2 == 0:
        ans = ans + [query[2]]
      else:
        ans = [query[2]] + ans

ans = ''.join(ans)
mes = ans.replace("S", s)
if rev%2 == 0:
  print(mes)
else:
  print(mes[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
r = False#ひっくり返されたか
for i in range(q):
  t=list(input().split())
  if len(t) == 1:#一つしか入力されていない
    r = not(r)
  else:
    if (t[1] == '1' and r == False):
      s = t[2] + s
    elif (t[1] == '2' and r == True):
      s = t[2] + s
    else:
      s = s + t[2]
if r:
  print(s[::-1])
else:
  print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
rev = 0
ans = 'S'

for _ in range(q):
  query = list(input().split())
  if int(query[0]) == 1:
    rev += 1
  else:
    if int(query[1]) == 1:      # 先頭
      if rev%2 == 0:
        ans = query[2] + ans
      else:
        ans = ans + query[2]
    else:                       # 末尾
      if rev%2 == 0:
        ans = ans + query[2]
      else:
        ans = query[2] + ans

mes = ans.replace("S", s)
if rev%2 == 0:
  print(mes)
else:
  print(mes[::-1])

Submission status: Time Limit Exceeded

Solution code:

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

tmplist = []
T = 0
for i in range(Q):
    Query = list(input().split())
    if len(Query) == 1:
        T = T+1
    else:
        if T%2 == 0:
            if Query[1] == '1':
                S.insert(0,Query[2])
            else:
                S.append(Query[2])
        else:
            if Query[1] == '2':
                S.insert(0,Query[2])
            else:
                S.append(Query[2])
#    print("len(Query)={}, Query={}, T={}, S={}".format(len(Query), Query,T,''.join(S)))

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

#print("ans={}".format(''.join(S)))
print(''.join(S))

Submission status: Time Limit Exceeded

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

reverse = 0
for query in queries:
    if len(query) == 1:
        reverse = 1 - reverse

    else:
        is_append_left = (reverse + int(query[1])) % 2
        if is_append_left:
            S = query[2] + S
        else:
            S += query[2]

if reverse:  # reverse処理は重い．実際，最後の一回で良い．
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

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


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

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

Submission status: Time Limit Exceeded

Solution code:
import sys

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

ans = s

for i in range(q):
  query = input().strip()
  if query == '1':
    ans = ans[::-1]
  else:
    _,f,c = query.split()
    if f == '1':
      c += ans
      ans = c
    else:
      ans += c
      
print(ans)

Submission status: Time Limit Exceeded

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


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

if (flip % 2) == 0:
    print(''.join(S))
else:
    print(''.join(S[::-1]))

Submission status: Time Limit Exceeded

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

def add(c ,head, tail, r, ri, f):
    r_m = ri % 2
    # print(r, ri)
    if (r-ri) % 2 == 1:
        f = 1 if f == 2 else 2
    # print(c, r_m, f)
    # if (r_m == 0 and f == 1) or (r_m == 1 and f == 2):
    if f==1:
        head = [c] + head
    else:
        tail = tail + [c]

    return head, tail

def put(c, f, ri, m):
    m.append((c, f, ri))
    return m

def join(s, head, tail, r):
    s_l = list(s)
    if r % 2 == 1:
        s_l = s_l[::-1]
    return ''.join(head + s_l + tail)

# def reverse(head, tail):
#     return tail[::-1], head[::-1]


r = 0
head = []
tail = []
m = []
for _ in range(Q):
    q = [x for x in input().split(' ')] 
    if len(q) == 1:
        r += 1
    else:
        _, f, c = q
        f = int(f)
        m = put(c, f, r, m)

for c, f, ri in m:
    head, tail = add(c, head, tail, r, ri, f)

ans = join(S, head, tail, r)
print(ans)

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())
for i in range(q):
  t=list(input().split())
  if len(t) == 1:#一つしか入力されていない
    s = s[::-1]#逆にする
  else:
    if t[1] == '1':
      s = t[2] + s
    else:
      s = s + t[2]
print(s)

Submission status: Time Limit Exceeded

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

mod = 0 #反転が何回目か（先頭がどっちか）,偶数回の時0，奇数回の時1

for _ in range(Q):
    Query = input()
    if Query[0] == "1":
        mod = (1 + mod) % 2
    else:
        if Query[2] == "1":
            if mod == 0:
                S = Query[4] + S
            else:
                S = S + Query[4]
        else:
            if mod == 0:
                S = S + Query[4]
            else:
                S = Query[4] + S
    #print("Qans",S)
if mod==0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
#python3
def main():
    s = input()
    q = int(input())
    query = []
    for i in range(q):
        t = input()
        query.append(t)
    for i in range(q):
        if query[i][0] == '1':
            #sを反転する
            s = ''.join(reversed(s))
            continue
        if query[i][0] == '2':
            t, f, c = map(str, query[i].split())  
            #あとで消す
            query[i] = (t, f, c)
            #f == 1のとき先頭にcを、f==2のとき最後尾にcを追加する
            if f == '1':
                s = c + s 
            if f == '2':
                s = s + c
    print(s)
main()

Submission status: Time Limit Exceeded

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

def add(c ,head, tail, r, f):
    r_m = r % 2
    if f == 1:
        head = [c] + head
    else:
        tail = tail + [c]

    return head, tail

def join(s, head, tail, r):
    s_l = list(s)
    if r % 2 == 1:
        s_l = s_l[::-1]
    return ''.join(head + s_l + tail)

def reverse(head, tail):
    return tail[::-1], head[::-1]


r = 0
head = []
tail = []
for _ in range(Q):
    q = [x for x in input().split(' ')] 
    if len(q) == 1:
        r += 1
        head, tail = reverse(head, tail)
    else:
        _, f, c = q
        f = int(f)
        head, tail = add(c, head, tail, r, f)

ans = join(S, head, tail, r)
print(ans)

Submission status: Time Limit Exceeded

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


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

    if (len(q) == 1):
        reverse *= - 1
    else:
        judge = int(q[1])
        cha = q[2]
        
        if (reverse == 1):
            if (judge == 1):
                S = cha + S
            else:
                S = S + cha
        elif (reverse == -1):
            if (judge == 1):
                S = S + cha
            else:
                S = cha + S
                

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
queries = list()
for ii in range(0,q):
  queries.append(input())
  
#print(queries)
# True -> straight, False -> back
order_flag = True

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

if order_flag:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

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

def gcd(a: int, b: int):
    """ https://cocodrips.hateblo.jp/entry/2014/03/05/143623
    """
    while b:
        a, b = b, a%b
    return a

def lcm(a: int, b: int):
    return a * b // gcd(a, b)


def main():
	#N,A,B = map(int, input().split())
	S = input().rstrip()
	Q = int(input())
	#vA = list(map(int, input().split()))
	#vX = [input().rstrip() for _ in [0,]*N]
	
	rev = 0		# reverse
	
	DF = "" #dq([]) # reversed Head!!
	DR = "" #dq([])
	for q in range(Q):
		tq = input().split()
		if tq[0]=="1":
			rev ^= 1
		else:
			T,F,C = tq
			if F=="1":
				# C to top
				if rev == 0:
					DF += C #DF.appendleft(C) 
				else:
					DR += C #DR.append(C)
			else:
				# C to bottom
				if rev == 0:
					DR += C #DR.append(C)
				else:
					DF += C #DF.appendleft(C)	
	
	
	if rev == False:
		res = DF[::-1] + S + DR
	else:
		res = DR[::-1] + S[::-1] + DF

	print(res)
	
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s,_,*q=open(0)
a=s[:-1]
p=1
for q in q:
  try:
    q,f,c=q.split()
    if(f<'2')^p:a+=c
    else:a=c+a
  except:p^=1
print(a[::p or-1])

Submission status: Time Limit Exceeded

Solution code:
import sys

s = input()
q = int(input())
rev = False

for query in sys.stdin:
    if query.strip() == '1': # sys.stdin does not remove line breaks. (vs. input() removes line breaks)
        if rev:
            rev = False
        else:
            rev = True
    else:
        t,f,c = query.split()
        if f == '1':
            if rev == True:
                s = s + c
            else:
                s = c + s
        elif f == '2':
            if rev == True:
                s = c + s
            else:
                s = s + c
if rev == True:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

flag = True

for i in query:
    if i[0]=="1":
        flag = not flag
#        S=S[::-1]
    else:
        if flag:
            if i[1]=="1":
                S=i[2]+S
            else:
                S=S+i[2]
        else:
            if i[1]=="1":
                S=S+i[2][::-1]
            else:
                S=i[2][::-1]+S
if flag:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

# 文字列格納リスト
ans = S

# クエリ格納リスト
q_s = []

# for i in range(Q):
#   _q = list(map(str, input().split()))
#   if _q[0] == "2":
#     q_s.append(_q)
#   elif _q[0] == "1":
#     if len(q_s) > 0:
#       if q_s[-1][0] == "1":
#         del q_s[-1]
#       else:
#         q_s.append(_q)
#     elif len(q_s) == 0:
#       q_s.append(_q)
# 状態確認用の変数
# 1 = 順列
# -1 = 逆列
pos = 1
for j in range(Q):
  i = list(map(str, input().split()))
# for i in q_s:
  if i[0] == "1":
    # 反転クエリ時はフラグを反転
    pos = pos * -1
  elif i[0] == "2":
    if pos == 1 and i[1] == "1":
      # 順列時は前に追加
      ans.insert(0,i[2])
    elif pos == -1 and i[1] == "1":
      # 逆列時は後ろに追加
      ans.append(i[2])
    elif pos == 1 and i[1] == "2":
      # 順列時は後ろに追加
      ans.append(i[2])
    elif pos == -1 and i[1] == "2":
      # 逆列時は前に追加
      ans.insert(0,i[2])
if pos == -1:
  # クエリ消化時点で逆順なら文字列を反転
  ans.reverse()
print("".join(ans))

Submission status: Time Limit Exceeded

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

# 文字列格納リスト
ans = S

# クエリ格納リスト
q_s = []

for i in range(Q):
  _q = list(map(str, input().split()))
  if _q[0] == "2":
    q_s.append(_q)
  elif _q[0] == "1":
    if len(q_s) > 0:
      if q_s[-1][0] == "1":
        del q_s[-1]
      else:
        q_s.append(_q)
    elif len(q_s) == 0:
      q_s.append(_q)
# 状態確認用の変数
# 1 = 順列
# -1 = 逆列
pos = 1
for i in q_s:
  if i[0] == "1":
    # 反転クエリ時はフラグを反転
    pos = pos * -1
  elif i[0] == "2":
    if pos == 1 and i[1] == "1":
      # 順列時は前に追加
      ans.insert(0,i[2])
    elif pos == -1 and i[1] == "1":
      # 逆列時は後ろに追加
      ans.append(i[2])
    elif pos == 1 and i[1] == "2":
      # 順列時は後ろに追加
      ans.append(i[2])
    elif pos == -1 and i[1] == "2":
      # 逆列時は前に追加
      ans.insert(0,i[2])
if pos == -1:
  # クエリ消化時点で逆順なら文字列を反転
  ans.reverse()
print("".join(ans))

Submission status: Time Limit Exceeded

Solution code:
S = input()

Q = int(input())
revcount = 0
ans = []
T = []
 
for i in range(Q):
  TS = input()
  if TS == "1":
    revcount += 1
  else:
    T.append(TS.rstrip().split(" "))
    T[len(T)-1][1] = str((int(T[len(T)-1][1]) + revcount % 2 + 1) % 2 + 1)
    
if revcount % 2 == 1:
  ans.append(S[::-1])
else:
  ans.append(S)
  
for i in range(len(T)):
  if (int(T[i][1]) == 1 and revcount % 2 == 0) or (int(T[i][1]) == 2 and revcount % 2 == 1):
    ans.insert(0,T[i][2])
  else:
    ans.append(T[i][2])
ans2 = "".join(ans)
print(ans2)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
reverse = False
for i in range(Q):
  q = input()
  if q == "1":
    reverse = not reverse
  else:
    q,f,c = q.split()
    if (f == "1" and not reverse) or (f == "2" and reverse):
      S = [c]+S
    else:
      S = S+[c]
if not reverse:
  print("".join(S))
else:
  print("".join(reversed(S)))

Submission status: Time Limit Exceeded

Solution code:
s,_,*q=open(0)
a=['',s[:-1]]
p=1
for q in q:
  try:
    q,f,c=q.split()
    i=(f<'2')^p
    a[i]=''.join((a[i],c))
  except:p^=1
print(a[p^1][::-1]+a[p])

Submission status: Time Limit Exceeded

Solution code:
s,_,*q=open(0)
a=['','']
p=1
for q in q:
  try:
    q,f,c=q.split()
    a[(f<'2')^p]+=c
  except:p^=1
print(a[p^1][::-1]+s[:-1][::p or-1]+a[p])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for c in range(Q):
    q = input().split(" ")
    if q[0] == "2":
        if q[1] == "1":
            S = q[2] + S
        else:
            S += q[2]
    else: # 逆順にする
        S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for c in range(Q):
    q = input().split(" ")
    if q[0] == "2":
        if q[1] == "1":
            S.insert(0, q[2])
        else:
            S.append(q[2])
    else: # 逆順にする
        S.reverse()
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
f=input
s=f()
n=int(f())
t=0
for i in range(n):
  q=f()
  if q[0]=='1': t=1-t
  elif int(q[2])+t==2: s+=q[4]
  else: s=q[4]+s
print(''.join([s,s[::-1]][t]))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
s=[i for i in input()]
for _ in range(int(input())):
    a=list(map(str,input().split()))
    if len(a)==1:
        s.reverse()
    else:
        if a[1]=='1': s.insert(0,a[2])
        else: s.append(a[2])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
def q1(s):
    #reverse
    return s[::-1]
def q2(s,f,char):
    if int(f) == 1:
        s = char + s
    else:
        s= s + char
    return s
  
s = input()
q = int(input())
count = 0 
while count<q:
    count+=1
    query = input().split()

    if len(query)==1 : #反転処理の方
        #2連続で反転指示なら無視する
        #最後のクエリなら次を読まずに反転
        if count == q: 
            s = q1(s)
        else:
            count+=1
            query = input().split()
            if not len(query)==1:    
                s=q1(s)
                s=q2(s,query[1],query[2])
    else: #文字追加の方
        s=q2(s,query[1],query[2])
print(s)

Submission status: Time Limit Exceeded

Solution code:
def q1(s):
    #reverse
    return s[::-1]
def q2(s,f,char):
    if int(f) == 1:
        s = char + s
    else:
        s= s + char
    return s


s = input()
q = int(input())
for i in range(q):
    query = input().split()
    if len(query)==1:
        s=q1(s)
    else:
        s=q2(s,query[1],query[2])
print(s)

Submission status: Time Limit Exceeded

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

def main():
    s = input()
    q = int(input())
    is_rev = False
    for _ in range(0, q):
        query = input()
        if len(query) == 1:
            is_rev = not is_rev
        else:
            query = query.split(' ')
            query = [int(query[0]), int(query[1]), query[2]]
            if (query[1] == 1 and is_rev) or (query[1] == 2 and not is_rev):
                s = s + query[2]
            else:
                s = query[2] + s
    if is_rev:
        print(s[::-1])
    else:
        print(s)

main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
rev = False
for _ in range(q):
    query = input()
    if query == "1":
        rev = not rev
    else:
        _, f, c = query.split(" ")
        if f == "1":
            if rev:
                s = s + c
            else:
                s = c + s
        else:
            if rev:
                s = c + s
            else:
                s = s + c

if rev:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
S = input()
Q = int(input())
# print("S:", S, "Q:", Q)

lst = [input() for _ in range(Q)]
# print(lst)
flg = True
# print(S)
for i in lst:
    # print("i[0]:", i[0])
    if i[0] == '1':
        flg = not flg
    else:
        # print("i[2]:", i[2])
        # print("i[4]:", i[4])
        if flg == False:
            if i[2] == '1':
                S = S + i[4] 
                # print(S)
            else:
                S = i[4] + S
                # print(S)
        else:
            if i[2] == '1':
                S = i[4] + S
                # print(S)
            else:
                S = S + i[4]
                # print(S)
if flg == False:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
 
S = list(input())
Q = int(input())
is_forward = True
 
for i in range(Q):
    query = input()
 
    if query == '1':
        is_forward = not is_forward
    else:
        T, F, C = query.split()
        if is_forward:
            if F == '1':
                S.insert(0, C)
            else:
                S.append(C)
        else:
            if F == '1':
                S.append(C)
            else:
                S.insert(0, C)

if not is_forward:
  S.reverse()
 
print(''.join(S))

Submission status: Time Limit Exceeded

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

for i in range(0,Q):
    if len(A[i]) == 1:
        S = S[::-1]
    else:
        if A[i][1] == '1':
            S = A[i][2] + S
        if A[i][1] == '2':
            S = S + A[i][2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s,_,*q=open(0)
a=['',s[:-1]]
p=1
for q in q:p^=q<'2';a[(q<'2 2')^p]+=q[-2]*(q>'2')
print(a[p^1][::-1]+a[p])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
rev = False
F = []

for i in range(Q):
    X = list(input().split(" "))

    if X[0] == "1" and rev is False:
        rev = True
    elif X[0] == "1" and rev is True:
        rev = False
    elif X[0] == "2" and X[1] == "1" and rev is False:
        F.insert(0, X[2])
    elif X[0] == "2" and X[1] == "1" and rev is True:
        S.append(X[2])
    elif X[0] == "2" and X[1] == "2" and rev is False:
        S.append(X[2])
    elif X[0] == "2" and X[1] == "2" and rev is True:
        F.insert(0, X[2])

F.extend(S)

if rev is True:
    F.reverse()

print(*F, sep="")