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())
reverse = 1
for i in range(Q):
    q = list(input().split())
    if(int(q[0]) == 1):
        reverse *= -1
    else:
        if(reverse == 1):
            if(int(q[1]) == 1):
                S = q[2] + S
            else:
                S = S + q[2]
        else:
            if(int(q[1]) == 1):
                S = S + q[2]
            else:
                S = q[2] + S
if(reverse == 1):
    print(S)
else:
    print("".join(reversed(S)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
Q = int(input())
query = list(list(input().split()) for _ in range(Q))
mode = 1
for q in query:
    if len(q)==1:
        mode *= -1
    else:
        f,c = q[1], q[2]
        if (mode>0 and f=='1') or (mode<0 and f=='2'):
            s = '{}{}'.format(c,s)
        else:
            s = '{}{}'.format(s,c)
print(s if mode>0 else s[::-1])

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(' ')
        query.append(contents)

    return (s, q, query)


def solve(s, q, query):

    tmp = ' ' 
    reverse_count = 0
    for contents in query:
        if contents[0] == '1':
            tmp = tmp[::-1]
            reverse_count = reverse_count + 1
        else:
            if contents[1] == '1':
                tmp = contents[2] + tmp
            else:
                tmp = tmp + contents[2]

    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:
import numpy as np

s = input()

roops = int(input())

grid = []
for i in range(roops):
    array = list(map(str,input().strip().split()))
    grid.append(array)
#print(grid)

for i in grid:
    if len(i)==1:
        s = ''.join(list(reversed(s)))
    else:
        if i[1]=="1":
            s = i[2]+s
        else:
            s = s+i[2]
print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  q = list(input())
  if q[0] == '1':
    S.reverse()
  else:
    if q[2] == '1':
      S.insert(0, q[4])
    else:
      S.append(q[4])
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
re = list(input())
Q = int(input())
sousa = [input().split() for i in range(Q)]
for i in sousa:
  if len(i) == 1:
    re.reverse()
  else:
    if i[1] == '1':
      k = list(i[2])
      re = k + re
    else:
      k = list(i[2])
      re += k
print(''.join(re))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
state = True
front = ""
ending = ""
for _ in range(q):
  qi = str(input())
  if qi[0] == "2":
    fi,ci = map(str,qi[2:].split())
    if state == True:
      if fi == "1":
        front += ci
      else:
        ending += ci
    else:
      if fi == "1":
        ending += ci
      else:
        front += ci
  else:
    state = not state
if state:
  first = front[::-1]
  lastt = ending
else:
  first = ending[::-1]
  lastt = front
  s = s[::-1]
print(first+s+lastt)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def main():

    S = stdin.readline().rstrip() 
    Q = int(input())
    rev = 0
    for _ in range(Q):
        q = stdin.readline()

        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    S = "{}{}".format(q[4], S)
                else:
                    S = "{}{}".format(S, q[4])
            else:
                if rev ==0:
                    S = "{}{}".format(S, q[4])
                else:
                    S = "{}{}".format(q[4], S)
    if rev == 1:
        S = S[::-1]

    print(S)

main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
sc = s[::-1]
n = int(input())
l = [list(input().split()) for i in range(n)]

last = 1

for i in range(n):
    if l[i][0] == '1':
        last = last * -1
    elif l[i][1] == '1' and last > 0:
            s = l[i][2] + s
            sc += l[i][2]
    elif l[i][1] == '2' and last > 0:
            s += l[i][2]
            sc = l[i][2] + sc
    elif l[i][1] == '1' and last < 0:
            s += l[i][2]
            sc = l[i][2] + sc
    else:
            s = l[i][2] + s
            sc += l[i][2]

if last > 0:
    print(s)
else:
    print(sc)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

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

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

    else:
      S_deque.append(item[2])

print(''.join(S_deque))

Submission status: Time Limit Exceeded

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

for i in Query:
    if int(i[0]) == 1:
        kaisu = kaisu + 1
    elif int(i[0]) == 2:
        if kaisu % 2 == 0:
            if int(i[1]) == 1:
                S = i[2] + S
            else:
                S = S + i[2]
        else:
            if int(i[1]) == 2:
                S = i[2] + S
            else:
                S = S + i[2]

if kaisu % 2 == 0:
    print(S)
else:
    print(''.join(list(reversed(S))))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
a = [0]*Q
for i in range(0, Q):
    a[i] = list(input().split())
    if a[i][0] == "1":
        S = S[::-1]
    elif a[i][1] == "1":
        S = a[i][2]+S
    else:
        S = S+a[i][2]
print (S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = deque(input())
q = int(input())
for _ in range(q):
    Q = list(map(str,input().split()))
    if int(Q[0]) == 1:
        if len(s) > 1:
            s=deque(list(s)[::-1])
    else:
        if int(Q[1]) == 1:
            s.appendleft(Q[2])
        else:
            s.append(Q[2])

            
print("".join(s))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    listA=input().split()
    if listA[0]=='1':
        S = S[::-1]
    else:
        if listA[1]=='1':
            S = listA[2]+S
        else:
            S = S+listA[2]

print(S)

Submission status: Time Limit Exceeded

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

for query in Queries:
    if query[0] == "1":
        S = S[::-1]
    else:
        if query[1] == "1":
            S = query[2] + S
        else:
            S = S + query[2]
            
print(S)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    jun = True
    for _ in range(q):
        qu = input()
        if qu == '1':
            jun = not jun
        else:
            if qu[2] == '1':
                if jun:
                    s = qu[4] + s
                else:
                    s = s + qu[4]
            else:
                if jun:
                    s = s + qu[4]
                else:
                    s = qu[4] + s
    if not jun:
        s = s[::-1]
    print(s)
main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s=input()
query=int(input())

li_q = []
for _ in range(query):
    li_q.append(input().split())

rev = False
li_str = [s]
for q in li_q:
    if q[0] == '1':
        rev = not rev
    else:
        if q[1] == '1':
            if rev:
                li_str.append(q[2])
            else:
                q_str = deque(li_str)
                q_str.appendleft(q[2])
                li_str = list(q_str)
        else:
            if rev:
                q_str = deque(li_str)
                q_str.appendleft(q[2])
                li_str = list(q_str)
            else:
                li_str.append(q[2])
                
ans = ''.join(li_str)
if rev:
    ans = ans[::-1]
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(str(input()))
N = int(input())


order = []
state = True

for n in range(N):
    order = input().split()
    if order[0] == '2':
        if (order[1] == '1') == state:
            S.insert(0,order[2])
        else:
            S.append(order[2])
    elif order[0] == '1':
        if state:
            state = False
        else:
            state = True


if state:
    out = ''.join(S)
else:
    out = ''.join(S[::-1])

print(out)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys
sys.setrecursionlimit(10 ** 7)
readline = sys.stdin.readline


def main():
    a = input()
    d = deque(a)
    n = int(input())
    for i in range(n):
        j = list(readline().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 = ""
    e = list(d)
    for j in e:
        ans = ans + j
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
S = input()
Q = int(input())

flip = 0
for i in range(Q):
  st = input()
  if(st[0] == '1'):
    flip += 1
  else:
    if((st[2] == '1' and flip % 2 == 0) or (st[2] == '2' and flip % 2 == 1)):
      nS = st[4] + S
    else:
      nS = S + st[4]
    S = nS

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

Submission status: Time Limit Exceeded

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

  ans = S
  for i in range(Q):
    if len(query[i])==1:
      ans = ans[::-1]
    else:
      if(query[i][1]=="1"):
        #ans = ''.join(query[i][2])
        ans = query[i][2] + ans
      else:
        #ans = ''.join(query[i][2])
        ans = ans + query[i][2]
  print(ans)
  
if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

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

    if t == "1":
        rev += 1
    else:
        if rev%2 == 0:
            if f == "1":
#                s.insert(0, c)
                l = c+l
            else:
#                s.append(c)
                r += c
        else:
            if f == "1":
#                s.append(c)
                r += c
            else:
#                s.insert(0, c)
                l = c+l

s = l + s + r

if rev%2 == 1:
    *s, = reversed(s)

print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(q):
  query = list(input().split())
  if (int(query[0]) == 1):
    s = s[::-1]
  else:
    if (int(query[1]) == 1):
      s = list(s)
      s.insert(0, query[2])
      s = tuple(s)
    else:
      s = s + (query[2],)

print(''.join(s))

Submission status: Time Limit Exceeded

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

count = 0

for query in P:
    if query[0]=="1":
        count+=1
    else:
        if query[1]=="1":
            if count%2==0:
                S=query[2]+S
            else:
                S=S+query[2]
        else:
            if count%2==0:
                S=S+query[2]
            else:
                S=query[2]+S
if count%2==1:
    S=S[::-1]
    
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
lst = []
for i in range(q):
    lst.insert(0, input().split())

counter = 0
while len(lst) != 0:
    x = lst.pop()
    if str(x[0]) == '1':
        counter += 1

    else:
        if counter % 2 != 0:
            s = s[::-1]
            counter = 0
        if str(x[1]) == '1':
            s = str(x[2]) + s

        else:
            s = s + str(x[2])

if counter % 2 != 0:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
N=int(input())
for i in range(N):
    a=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("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    Query = input()
    if Query[0] == "1":
        Flag = not Flag
    else:
        QueryList = Query.split()
        if Flag:
            if QueryList[1] =="1":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]
        else:
            inv_add = QueryList[2][::-1]
            if QueryList[1] =="2":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]

if Flag:
    print(S)
else:
    for i in range(len(S)):
        print(S[len(S)-i-1], end="")

Submission status: Time Limit Exceeded

Solution code:
def ddd(s , q , ql):

    ans = ""
    leftans = "" #1
    rightans = "" #2
    count = 0

    for i in range(q):
        if int(ql[i][0]) == 2:
            #ans = ans[::-1]
            if count % 2 == 0:
                if int(ql[i][1]) == 1:
                    leftans = ql[i][2] + leftans
                else:
                    rightans = rightans + ql[i][2]
            else:
                if int(ql[i][1]) == 1:
                    rightans = rightans + ql[i][2]
                else:
                    leftans = ql[i][2] + leftans

        else:
            count += 1

    if count % 2 == 1:
        ans = rightans[::-1] + s[::-1] + leftans[::-1]
    else:
        ans = leftans + s + rightans

    return ans

def main():
    s = str(input())
    q = int(input())
    ql = [list(map(str , input().split())) for i in range(q)]
    print(ddd(s , q , ql))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
qn = int(input())
q = []
rev = False

for i in range(qn):
    qi = input().split()
    if qi[0] == "1":
        rev = not rev
    else:
        if rev:
            if qi[1] == "1":
                qi[1] = "2"
            else:
                qi[1] = "1"
        q.append((2, (int(qi[1]), qi[2])))
if rev:
    q.append((1, ()))
for i in range(len(q)):
    if q[i][0] == 1:
        s = s[::-1]
    if q[i][0] == 2:
        if q[i][1][0] == 1:
            s = [q[i][1][1]] + s
        else:
            s.append(q[i][1][1])
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
l=[]

for i in range(n):
    l.append(list(input().split()))


def re(s):
    return s[::-1]
    
def fa(s,a):
    a=a+s
    return a

def la(s,a):
    s=s+a
    return s

for i in range(n):
    if len(l[i])==1:
        s=re(s)
    elif l[i][1]=='1':
        s=fa(s,l[i][2])
    else:
        s=la(s,l[i][2])
   
print(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"
        else:
            if (q[1] == "1" and now == "f") or (q[1] == "2" and now == "b"):
                front += q[2]
            else:
                back += q[2]

    if now == "f":
        res = front[::-1] + s + back
    else:
        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:
S = input()
Q = int(input())
QUERY = [input().split(" ") for i in range(Q)]

for query in QUERY:
    #print(query)
    if query[0] == "1":#反転
        S = S[::-1]
    else:#=="2"
        if query[1] == "1":
            S = query[2] + S
        else:#=="2"
            S += query[2]
print(S)

Submission status: Time Limit Exceeded

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

for i in range(0,q):
    line = input()
    if len(line) == 1:
        s = s[::-1]
    else:
        t, f, c = line.split(" ")
        f = int(f)
        if f == 1:
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=raw_input()
q=int(raw_input())
while(q>0):
    t=raw_input().split(" ")
    if(t[0]=='1'): s=s[::-1]
    else:
        if(t[1]=='1'):s=(t[2]+s)
        else:s=(s+t[2])
    q=q-1
print s

Submission status: Time Limit Exceeded

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

rev=1
for i in range(q):
    qu=input()
    if qu=="1":
        rev=-rev
    else:
        t,f,c=qu.split()
        if f=="1" and rev==1:
            s=c+s
#            print(s)
        elif f=="2" and rev==1:
            s=s+c
#            print(s)
        elif f=="1" and rev==-1:
            s=s+c
#            print(s)
        elif f=="2" and rev==-1:
            s=c+s
#            print(s)
if rev==-1:
    s=s[::-1]
#    print(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
S = input()
Q = int(input())
bool = True
for i in range(0, Q):
	s = input()
	if s[0] == "1":
		bool = not bool
	else:
		if s[2] == "1":
			if bool:
				S = s[-1] + S
			else:
				S += s[-1]
		else:
			if bool:
				S += s[-1]
			else:
				S = s[-1] + S
if bool:
	print(S)
else:
	print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
S=input()[:-1]
Q=int(input())
r = False
for _ in range(Q):
  qs = input()[:-1].split(' ')
  if len(qs) == 1:
    r = not r
  else:
    if (qs[1] == '1' and (not r)) or (qs[1] == '2' and r):
      S = qs[2] + S
    else:
      S = S + qs[2]
if r:
  print(S[::-1])
else:
  print(S)

Submission status: Time Limit Exceeded

Solution code:
def patern_one(s):
    return s[::-1]

def patern_two(s, f, c):
    if f == 1:
        return c + s
    else:
        return s + c

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

for i in range(q):
    input_line = input().split()
    if input_line[0] == "1":
        s = patern_one(s)
        continue
    if input_line[0] == "2":
        s = patern_two(s, int(input_line[1]), input_line[2])
print(s)

Submission status: Time Limit Exceeded

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

#一番最後に1度だけ反転すればよい
reverse_count = 0
for i in range(q):
  y = input().split()
  if y[0] == '1':
    s = s[::-1] 
  elif y[0] == '2':
    if y[1] == '1':
      #文字列の先頭に追加。反転回数が奇数なら、末尾に追加と同義
      if reverse_count % 2 == 1:
        s = s + y[2]
      else:
        s = y[2] + s
    elif y[1] == '2':
      #文字列の末尾に追加。反転回数が奇数なら、先頭に追加と同義
      if reverse_count % 2 == 1:
        s = y[2] + s
      else:
        s = s + y[2] 

#最後に一度だけ反転させる
if reverse_count % 2 == 0:
  #元の順序に戻るので、そのまま
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
queries = []
for _ in range(Q):
  q = list(input().split()) # 文字列のリスト
  queries.append(q)

r_count = 0
for query in queries:
  t = query[0]
  if (t == '1'):
    r_count += 1
  elif (t == '2'):
    f = query[1]
    new_S = ""
    if(f == '1' and (r_count % 2 == 0)):
      new_S = query[2] + S
    elif(f == '1' and (r_count % 2 == 1)):
      new_S = S + query[2]
    elif(f == '2' and (r_count % 2 == 0)):
      new_S = S + query[2]
    elif(f == '2' and (r_count % 2 == 1)):
      new_S = query[2] + S
    S = new_S

if (r_count % 2 == 1):
  r_S = S[::-1]
  S = r_S

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
for i in range(Q):

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

    if X[0] == '1':
        S = S[::-1]
    else:
        if X[1] =='1':
            S = X[2] + S
        else:
            S = S + X[2]
print(S)

Submission status: Time Limit Exceeded

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

Ques = []
for i in range(nQ):
  Ques.append(input())
  
for iQ in Ques:
  iQ = iQ.split()
  if int(iQ[0]) == 1:
    S = S[::-1]
  else:
    if int(iQ[1]) == 1:
      S = iQ[2] + S
    elif int(iQ[1]) == 2:
      S = S + iQ[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def main():
    S = input()
    S = deque([c for c in S])
    Q = int(input())
    rvCt = 0

    head, tail = deque([]), deque([])
    for _ in range(Q):
        query = input().split()
        if query[0] == '1':
            rvCt += 1
        else:
            if rvCt % 2 == 1:
                head += S
                head += tail
                S = head
                S.reverse()
                head, tail = deque([]), deque([])
            rvCt = 0
            if query[1] == '1':
                head.appendleft(query[2])
            else:
                tail.append(query[2])

    if rvCt % 2 == 1:
        head += S
        head += tail
        S = head
        S.reverse()
    else:
        head += S
        head += tail
        S = head
    
    print(''.join(S))

main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(1, Q+1):
    query = input()
    if len(query) == 1:
        S = S[::-1]
    else:
        n, F, C = query.split()
        if F == '1':
            s_list = [C, S]
        else:
            s_list = [S, C]
        S = ''.join(s_list)
print(S)

Submission status: Time Limit Exceeded

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


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

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

        if qi[0] == '1':
            s = s[::-1]
        else:
            if qi[1] == '1':
                s = qi[2] + s
            else:
                s += qi[2]

    print(s)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    inp = input()
    if len(inp) == 1:
        S = S[::-1]
    else:
        T, F = map(int, inp.split()[:2])
        C = inp.split()[-1]
        if F == 1:
            S = C + S
        else:
            S = S + C

print(S)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    i = input()
    if len(i) >= 2:
        i, F, C = i.split()
    if int(i) == 1:
        S = S[::-1]
    else:
        if int(F) == 1:
            S = C + S
        else:
            S = S + C

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
ans = s
    
for _ in range(q):
    tmp = input()
    if tmp == "1":
        ans = ans[::-1]
    else:
        tmp = tmp.split(" ")
        anstmp = list(ans)
        if tmp[1] == "1":
            anstmp.insert(0, tmp[2])
        elif tmp[1] == "2":
            anstmp.append(tmp[2])
        ans = "".join(anstmp)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = list(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 = s[::-1]
                s.append(query[2])
                s = s[::-1]
            elif query[1] == "2":
                s.append(query[2])
        else:
            if query[1] == "1":
                s.append(query[2])
            elif query[1] == "2":
                s = s[::-1]
                s.append(query[2])
                s = s[::-1]

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

Submission status: Time Limit Exceeded

Solution code:
# Problem D - String Formation

# input process
S = input()
Q = int(input()) # query number

# initialization
sento = 1

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

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

Submission status: Time Limit Exceeded

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

    for i in range(Q):
        A = input()
        if A[0] == '1' and len(S)!=1:
            h = h + 1
        else:
            if h % 2 == 0:
                if A[2] == '1':
                    S = A[4]+S
                else:
                    S = S+A[4]
            else:
                if A[2] == '1':
                    S = S+A[4]
                else:
                    S = A[4]+S
                                        
    if h % 2 == 0:
        print(S)
    else:
        print(S[::-1])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
for i in range(q):
    tmp_q = list(input().split())
    if(int(tmp_q[0])==1):
        s.reverse()
        #print(s)
    else:
        list_tmp = []
        #print(tmp_q)
        if(int(tmp_q[1])==1):
            list_tmp = list(tmp_q[2])
            #print(list_tmp)
            list_tmp.extend(s)
            s = list_tmp
            #print(s)
        else:
            #print(tmp_q)
            s.extend(list(tmp_q[2]))
print(''.join(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 q[i][0] == "1":
    S = S[::-1]
    continue
  if q[i][0] == "2":
    if q[i][1] == "1":#先頭に追加
      S = q[i][2] + S
      continue
    if q[i][1] == "2":#末尾に追加
      S += q[i][2]
      continue

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = input()
q = ['']*int(n)

for i in range(0,int(n)):
  q[i] = input().split()
  
for i in range(0,int(n)):
  if(q[i][0] == "1"):
    s = s[::-1]
  elif(q[i][0] == "2"):
    if(q[i][1] == "1"):
      s = q[i][2] + s
    else:
      s = s + q[i][2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = []

def func(string, t, f, c):
    if int(t) == 1:
        new_str = ''.join(list(reversed(string)))
        return new_str
    else:
        if int(f) == 1:
            return c + string
        else:
            return string + c

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for _ in range(q):
    query = input()
    try:
        t = int(query)
        s = s[::-1]
    except ValueError:
        t,f,c = map(str,query.split())
        if f == "1":
            s = c + s
        if f == "2":
            s = s + c
print(s)