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:
st=input()
N = int(input())
t = [0] * N
x = [0] * N
y = [0] * N
sw=0
for i in range(N):
    try:
        t[i], x[i], y[i] = map(str,input().split())
    except ValueError :
        t[i]='1'
        x[i]=''
        y[i]=''
    if t[i]=='2':
        if sw==0:
            if x[i]=='1':
                st=y[i]+st
            elif x[i]=='2':
                st=st+y[i]
        elif sw==1:
            if x[i]=='1':
                st=st+y[i]
            elif x[i]=='2':
                st=y[i]+st
    elif t[i]=='1' and sw==0:
        sw=1
    elif t[i]=='1' and sw==1:
        sw=0

if sw==1:
    print(st[::-1])
else:
    print(st)

Submission status: Time Limit Exceeded

Solution code:
isReversed = False
s = input()
q = int(input())
for i in range(q):
  t = list(input().split())
  if t[0] =='1':
    isReversed = not bool(isReversed)
  else:
    if (t[1] =='1' and isReversed == False) or (t[1] =='2' and isReversed == True) :
      s = t[2] +s
    else:
      s = s + t[2]
                                              
      
if isReversed:
  print(s[::-1])
else:
  print(s)

Submission status: Time Limit Exceeded

Solution code:
isReversed = False
s = input()
q = int(input())
for i in range(q):
  t = list(input().split())
  if t[0] =='1':
    isReversed = not bool(isReversed)
  else:
    if (t[1] =='1' and isReversed == False) or (t[1] =='2' and isReversed == True) :
      s = t[2] +s
    else:
      s = s + t[2]
                                              
      
if isReversed:
  s = s[::-1]
      
print(s)

Submission status: Time Limit Exceeded

Solution code:
isReversed = False
s = input()
q = int(input())
for i in range(q):
  t = list(input().split())
  if t[0] =='1':
    isReversed = not bool(isReversed)
  else:
    if t[1] =='1':
      if isReversed:
        s =s + t[2]
      else:
      	s =  t[2] + s
    else :
      if isReversed:
        s =t[2]+s
      else:
      	s =s + t[2]
if isReversed:
  s = s[::-1]
      
print(s)

Submission status: Time Limit Exceeded

Solution code:
st=input()
N = int(input())
t = [0] * N
x = [0] * N
y = [0] * N
for i in range(N):
    try:
        t[i], x[i], y[i] = map(str,input().split())
    except ValueError :
        t[i]='1'
        x[i]=''
        y[i]=''

    if t[i]=='1':
        st=st[::-1]
    elif x[i]=='1':
        st=y[i]+st
    elif x[i]=='2':
        st=st+y[i]

print(st)

Submission status: Time Limit Exceeded

Solution code:
st=input()
N = int(input())
t = [0] * N
x = [0] * N
y = [0] * N
for i in range(N):
    try:
        t[i], x[i], y[i] = map(str,input().split())
    except ValueError :
        t[i]='1'
        x[i]=''
        y[i]=''
for j in range(N):
    if t[j]=='1':
        st=st[::-1]
    elif t[j]=='2':
        if x[j]=='1':
            st=y[j]+st
        elif x[j]=='2':
            st=st+y[j]
print(st)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

string = input()
x = deque()
for s in string:
    x.append(s)

B = int(input())
for i in range(B):
    cmd = input().split()
    if len(cmd) == 1 and cmd[0] == "1":
        x.reverse()
    else:
        _, n, c = cmd
        if n == "1":
            x.appendleft(c)
        else:
            x.append(c)
print("".join(list(x)))

Submission status: Time Limit Exceeded

Solution code:
string = input()
B = int(input())

for i in range(B):
    cmd = input().split()
    if len(cmd) == 1 and cmd[0] == "1":
        string = string[::-1]
    else:
        _, n, c = cmd
        # n = int(n)
        if n == "1":
            string = c + string
        else:
            string = string + c
print(string)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
  t = list(input().split())
  if t[0] =='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()
n = int(input())
for i in range(n):
    order = list(input().split())
    if len(order) == 3:
        if int(order[1]) == 1:
            s = order[2] + s
        else:
            s += order[2]
    else:
        s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
cnt = 0
for i in range(q):
    t = input()
    if t == '1':
        cnt = 1 - cnt
    else:
        t2, f, c = t.split()
        if cnt == 0:
            if f == '1':
                s = c+s
            else:
                s += c
        else:
            if f == '1':
                s += c
            else:
                s = c+s

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


def getQuery():
    yield input().split()


for _ in range(q):
    for query in getQuery():
        if query[0] == "1":
            reverseFlag = not reverseFlag
        else:
            if query[1] == "1":
                s = (s + query[2]) if reverseFlag else (query[2] + s)
            else:
                s = (query[2] + s) if reverseFlag else (s + query[2])

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

print(s)

Submission status: Time Limit Exceeded

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

rev_flag = False
for i in range(q):
    tmp_in = list(input().split())

    if tmp_in[0] == '1':
        if rev_flag:
            rev_flag = False
        else:
            rev_flag = True
    else:
        if rev_flag:
            if tmp_in[1] == '1':
                s.append(tmp_in[2])
            else:
                s.insert(0, (tmp_in[2]))
        else:
            if tmp_in[1] == '1':
                s.insert(0, tmp_in[2])
            else:
                s.append(tmp_in[2])

if rev_flag:
    s.reverse()

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

Submission status: Time Limit Exceeded

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

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

    if tmp_in[0] == '1':
        s.reverse()
    else:
        if tmp_in[1] == '1':
            s.insert(0, tmp_in[2])
        else:
            s.append(tmp_in[2])

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    query = input().split(' ')
    if len(query) == 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:
S = input()
Q = int(input())

for i in range(Q):
    query = input().split(' ')
    if len(query) == 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 okikae(s):
    a = ''
    for i in range(len(s)):
        a = a + s[-1-i]
    return a

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

a = [0] * q

t = 0

for i in range(q):
    a[i] = list(map(str, input().split()))
    if a[i] == ['1']:
        t = t + 1
    else:
        if (a[i][1] == '1' and t % 2 == 0) or (a[i][1] == '2' and t % 2 == 1):
            s = a[i][2] + s
        else:
            s = s + a[i][2]
       
if t % 2 == 1:
    s = okikae(s)
            
print(s)

Submission status: Time Limit Exceeded

Solution code:
def okikae(s):
    a = ''
    for i in range(len(s)):
        a = a + s[-1-i]
    return a

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

a = [0] * q

t = 0

for i in range(q):
    a[i] = list(map(str, input().split()))
    if a[i] == ['1']:
        t = t + 1
    else:
        if a[i][1] == '1':
            if t % 2 == 0:
                s = a[i][2] + s
            else:
                s = s + a[i][2]
        else:
            if t % 2 == 0:
                s = s + a[i][2]
            else:
                s = a[i][2] + s

if t % 2 == 1:
    s = okikae(s)
            
print(s)

Submission status: Time Limit Exceeded

Solution code:
def okikae(s):
    a = ''
    for i in range(len(s)):
        a = a + s[-1-i]
    return a

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

a = [0] * q

for i in range(q):
    a[i] = list(map(str, input().split()))
    
for i in range(q):
    if a[i] == ['1']:
        s = okikae(s)
    else:
        if a[i][1] == '1':
            s = a[i][2] + s
        else:
            s = s + a[i][2]
            
print(s)

Submission status: Time Limit Exceeded

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

list_s = list(s)

a = [0] * q

for i in range(q):
    a[i] = list(map(str, input().split()))
    
for i in range(q):
    if a[i] == ['1']:
        for j in range(len(list_s) // 2):
            z = list_s[j]
            y = list_s[-1-j]
            list_s[-1-j] = z
            list_s[j] = y
    else:
        if a[i][1] == '1':
            list_s.insert(0, a[i][2])
        else:
            list_s.append(a[i][2])

s = ''.join(list_s)
            
print(s)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
ans=S
cnt=0
for _ in range(Q):
    i=(input().split())
    if(int(i[0]) == 1):
        cnt= (cnt+1) % 2
    elif((cnt== 0 and i[1]=='1') or (cnt == 1 and i[1]=='2')):
        ans= i[2] + ans
    else:
        ans=''.join([ans,i[2]])
print(ans[::-1]) if(cnt % 2 == 1) else print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
lis=[]
for _ in range(Q):
    lis.append(input().split())
ans=[S]
cnt=0
for i in lis:
    if(int(i[0]) == 1):
        cnt= (cnt+1) % 2
    elif((cnt== 0 and i[1]=='1') or (cnt == 1 and i[1]=='2')):
        ans.insert(0,i[2])
    else:
        ans.extend(i[2])
ans=''.join(ans)
print(ans[::-1]) if(cnt % 2 == 1) else print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
lis=[]
for _ in range(Q):
    lis.append(input().split())
ans=S
cnt=0
for i in lis:
    if(int(i[0]) == 1):
        cnt= (cnt+1) % 2
    elif((cnt== 0 and i[1]=='1') or (cnt == 1 and i[1]=='2')):
        ans= i[2] + ans
    else:
        ans=''.join([ans,i[2]])
print(ans[::-1]) if(cnt % 2 == 1) else print(ans)

Submission status: Time Limit Exceeded

Solution code:

S=input()
Q=int(input())
lis=[]
for _ in range(Q):
    lis.append(input().split())
ans=S
cnt=0
for i in lis:
    if(int(i[0]) == 1):
        cnt= (cnt+1) % 2
    elif((cnt== 0 and i[1]=='1') or (cnt == 1 and i[1]=='2')):
        ans= i[2] + ans
    else:
        ans+=i[2]

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
lis=[]
for _ in range(Q):
    lis.append(input().split())
ans=S
cnt=0
for i in lis:
    if(i[0] == '1'):
        cnt+=1
    elif((cnt % 2 == 0 and i[1]=='1') or (cnt % 2 == 1 and i[1]=='2')):
        ans= i[2] + ans
    else:
        ans+=i[2]

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
lis=[]
for _ in range(Q):
    lis.append(input().split())
ans=S
cnt=0
for i in lis:
    if(i[0] == '1'):
        cnt+=1
    elif((cnt % 2 == 0 and i[1]=='1') or (cnt % 2 == 1 and i[1]=='2')):
        ans= i[2] + ans
    else:
        ans+=i[2]

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

Submission status: Time Limit Exceeded

Solution code:
def D():
    S = raw_input()
    Q = int(raw_input())
    for qdx in range(Q):
        parts = raw_input().split()
        if len(parts) == 1:
            S = S[::-1]
        else:
            _, F, C = parts
            if F == '1':
                S = C + S
            else:
                S = S + C
    return S

print(D())

Submission status: Time Limit Exceeded

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

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

    if len(query_i) == 1:
        count += 1

    else:

        if query_i[1] == "1":
            if count % 2 == 0:
                s = query_i[2] + s

            else:
                s += query_i[2]

        else:
            if count % 2 == 0:
                s += query_i[2]

            else:
                s = query_i[2] + s

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

Submission status: Time Limit Exceeded

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

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

    if query_i[0] == "1":
        s = s[::-1]

    else:
        if query_i[1] == "1":
            s = query_i[2] + s

        else:
            s += query_i[2]

print(s)

Submission status: Time Limit Exceeded

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

rev = 1
fr = ""
rr = ""

for i in range(q):
    ss=input()
    if int(ss[0])==1:
        rev *= -1
    else:
        f,c=int(ss[2]),ss[4]
        if f == 2:
            f = -1
        
        if f*rev ==1:
            fr+=c
        else:
            rr+=c

if rev == 1:
    ans = fr[::-1]+s+rr
else:
    ans = rr[::-1]+s[::-1]+fr
print(ans)

Submission status: Time Limit Exceeded

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

rev = 1
fr = ""
rr = ""

for i in range(q):
    ss=input()
    if int(ss[0])==1:
        rev *= -1
    else:
        f,c=int(ss[2]),ss[4]
        if f == 2:
            f = -1
        
        if f*rev ==1:
            fr+=c
        else:
            rr+=c

ans = fr[::-1]+s+rr
if rev == -1:
    ans = ans[::-1]
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:
    if t[1] == "1":
      s = t[2] + s
    elif t[1] == "2":
      s = s + t[2]
  else:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        S.reverse()
    elif q[1] == '1':
        S.insert(0, q[2])
    else:
        S.append(q[2])

print(''.join(S))

Submission status: Time Limit Exceeded

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

        else:
            if case==1:
                S=temp[2]+S
            else:
                S=S+temp[2]
if case==1:
    S=S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

        if int(temp[1])==2:
            if case==1:
                S=temp[2]+S
            if case==0:
                S=S+temp[2]
if case==1:
    S=S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = list(input())
q = int(input())
order_flag = 0
for i in range(q):
    
    buf = input().split()
    if len(buf) == 1:
        order_flag += 1

    else:
        if buf[1] == '1':
            if order_flag % 2 == 0:
                s_buf = deque(s)
                s_buf.appendleft(buf[2])
                s = list(s_buf)
            else:
                s.append(buf[2])
        else:
            if order_flag % 2 == 1:
                s_buf = deque(s)
                s_buf.appendleft(buf[2])
                s = list(s_buf)
            else:
                s.append(buf[2])

if order_flag % 2 == 1:
    s.reverse()

for i in range(len(s)):
    print(s[i], end="")

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for i in range(q):
    buf = input().split()
    if len(buf) == 1:
        s.reverse()

    else:
        if buf[1] == '1':
            # s.insert(0, buf[2])
            s_buf = deque(s)
            s_buf.appendleft(buf[2])
            s = list(s_buf)
        else:
            s.append(buf[2])

for i in range(len(s)):
    print(s[i], end="")

Submission status: Time Limit Exceeded

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

for i in range(q):
    buf = input().split()
    if len(buf) == 1:
        s.reverse()

    else:
        if buf[1] == '1':
            # s.insert(0, buf[2])
            s.reverse()
            s.append(buf[2])
            s.reverse()
        else:
            s.append(buf[2])

for i in range(len(s)):
    print(s[i], end="")

Submission status: Time Limit Exceeded

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

for i in range(q):
    buf = input().split()
    if len(buf) == 1:
        s.reverse()

    else:
        if buf[1] == '1':
            s.insert(0, buf[2])
        else:
            s.append(buf[2])

for i in range(len(s)):
    print(s[i], end="")

Submission status: Time Limit Exceeded

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

for i in range(q):
    buf = input().split()
    if len(buf) == 1:
        s = s[::-1]

    else:
        if buf[1] == '1':
            s.insert(0, buf[2])
        else:
            s.append(buf[2])

for i in range(len(s)):
    print(s[i], end="")

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
for i in range(Q):
    temp=input().split()
    if len(temp)==1:
        S=S[::-1]
    else:
        if int(temp[1])==1:
            S="".join([temp[2],S])

        if int(temp[1])==2:
            S="".join([S,temp[2]])
			
print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
    buf = input().split()
    if len(buf) == 1:
        # s_first = s[0]
        # s_last = s[-1]
        # s[0] = s_last
        # s[-1] = s_first
        s = s[::-1]

    elif len(buf) == 3:
        if buf[1] == '1':
            s.insert(0, buf[2])
        elif buf[1] == '2':
            s.append(buf[2])

for i in range(len(s)):
    print(s[i], end="")

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
for i in range(Q):
    temp=input().split()
    if len(temp)==1:
        S=S[::-1]
    else:
        if int(temp[1])==1:
            S=temp[2]+S

        if int(temp[1])==2:
            S=S+temp[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
cnt = 0
for _ in range(q):
    t = input()
    if t[0] == '1':
        cnt += 1
    elif t[2] == '1':
        if cnt%2 == 0:
            s = t[4] + s
        else:
            s = s + t[4]
    else:
        if cnt%2 == 0:
            s = s + t[4]
        else:
            s = t[4] + s
if cnt%2 == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


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

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

    print(s)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

S_list = list(S)

r = 1
a = []
b = []

for i in range(Q):
    q = input().split()
    if q[0] == '1':
        r *= -1
        continue
    elif len(q) == 3:
        if q[1] == '1':
            if r == -1:
                b.append(q[2])
            else:
                a.insert(0, q[2])
        else:
            if r == -1:
                a.insert(0, q[2])
            else:
                b.append(q[2])
                
answer = a + S_list + b

if r == -1:
    answer = answer[::-1]
    
print(''.join(answer))

Submission status: Time Limit Exceeded

Solution code:
string_list = list(input())
n = int(input())

for i in range(n):
  line = list(input().split())
  if int(line[0]) == 1:
    string_list.reverse()
  elif int(line[0]) == 2:
    if int(line[1]) == 1:
      string_list = [line[2]]+ string_list
    elif int(line[1]) == 2:
      string_list.append(line[2])
for s in string_list:
  print(s, end ="")

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
s = input()
q = int(input())
cnt = 0
for i in range(q):
    query = input().split()
    if len(query) == 1:
        cnt += 1
    else:
        if cnt % 2 == 0:
            if query[1] == "1":
                s = query[2] + s
            else:
                s += query[2]
        else:
            if query[1] == "1":
                s += query[2]
            else:
                s = query[2] + s

if cnt % 2 != 0:
    s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
def query1(b):
    return not b

def query2(F, C, string, b):
    if (F == '1' and b) or (F == '2' and not b):
        return C + string
    else:
        return string + C

S = input()
Q = int(input())
r = True

for i in range(Q):
    Query = input().split()
    if len(Query) == 1:
        r = query1(r)
    else:
        S = query2(Query[1], Query[2], S, r)

if r:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
def query1(string):
    return string[::-1]

def query2(F, C, string):
    if F == '1':
        return C + string
    elif F == '2':
        return string + C

S = input()
Q = int(input())
for i in range(Q):
    Query = input().split()
    if len(Query) == 1:
        S = query1(S)
    else:
        S = query2(Query[1], Query[2], S)

print(S)

Submission status: Time Limit Exceeded

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

rev = True

for i in range(q):
    q = list(input().split())
    if(int(q[0]) == 1):
        rev = rev ^ True
    else:
        n = int(q[1])
        if((n == 1 and rev) or (n == 2 and not rev)):
            s.insert(0, q[2])
        else:
            s.append(q[2])

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

print(''.join(s))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

rev = True

for i in range(q):
    q = list(input().split())
    if(int(q[0]) == 1):
        rev = rev ^ True
    else:
        txt = ''.join(q[2:])
        if((int(q[1]) == 1 and rev == True) or (int(q[1]) == 2 and rev == False)):
            s = txt + s
        else:
            s += txt

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
q = []
for i in range(n):
  query = list(input().split())
  q.append(query)
  
a = 1
for i in range(n):
  if int(q[i][0]) == 1:
    a *= -1
  else:
    if int(q[i][1]) == 1:
      if a > 0:
        s = q[i][2] + s
      else:
        s = s + q[i][2]
    else:
      if a > 0:
        s = s + q[i][2]
      else:
        s = q[i][2] + s
if a > 0:
  print(s)
else:
  s = s[::-1]
  print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
qnum=int(input())
q=[input().split() for i in range(qnum)]
dire=True
for i in q:
  if i[0]=="1":
    if dire==True:
      dire=False
    else:
      dire=True
  else:
    if i[1]=="1":
      if dire==True:
        s=i[2]+s
      else:
        s=s+i[2]
    else:
      if dire==True:
        s=s+i[2]
      else:
        s=i[2]+s
if dire==True:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
qnum=int(input())
q=[input().split() for i in range(qnum)]
for i in q:
  if i[0]=="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=list(input())
q=int(input())
direction=1
T=['1' for j in range(10**5)]
for i in range(q):
  query=list(input().split())
  if query[0]=='1':
    direction=-1*direction
  else:
    value=-1+2*(int(query[1])-1)
    e=value*direction
    if e==-1:
      S.insert(0,query[2])
    else:
      S.append(query[2])
if direction==-1:
  for j in range(len(S)):
    T[j]=S[len(S)-j-1]
  S=T[:len(S)]
moji=''.join(S)
print(moji)

Submission status: Time Limit Exceeded

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

rev_flag = 0

bef = ''
aft = ''

for i in range(Q):
    if Query[i][0] == '1':
        rev_flag = (rev_flag + 1) % 2
    if Query[i][0] == '2':
        if Query[i][1] == '1':
            if rev_flag == 0:
                bef = Query[i][2] + bef
            elif rev_flag == 1:
                aft = aft + Query[i][2]
        elif Query[i][1] == '2':
            if rev_flag == 1:
                bef = Query[i][2] + bef
            elif rev_flag == 0:
                aft = aft + Query[i][2]

S = bef + S + aft
if rev_flag == 1:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = [list(map(str, input().split())) for i in range(Q)]
 
rev_flag = 0
 
for i in range(Q):
    if Query[i][0] == '1':
        rev_flag = (rev_flag + 1) % 2
    if Query[i][0] == '2':
        if Query[i][1] == '1':
            if rev_flag == 0:
                S = Query[i][2] + S
            elif rev_flag == 1:
                S = S + Query[i][2]
        elif Query[i][1] == '2':
            if rev_flag == 1:
                S = Query[i][2] + S
            elif rev_flag == 0:
                S = S + Query[i][2]
 
if rev_flag == 1:
    S = S[::-1]
 
print(S)