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:
def main():
    import sys
    input = sys.stdin.readline

    s = input()
    s = s[0:len(s)-1]
    Q = int(input())
    lq = [input().split() for _ in range(Q)]

    flg = 0 # 0:sonomama 1:gyakuten
    for q in lq:
        if q[0] == "1":
            if flg == 0:
                flg = 1
            else:
                flg = 0
        elif q[0]=="2":
            tmp = q[2]
            if (q[1] == "1" and flg ==0) or (q[1] == "2" and flg ==1):
                tmp += s
                s = tmp
            elif (q[1] == "2" and flg == 0) or (q[1] == "1" and flg == 1):
                s += tmp

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

V = list(input())
S = deque()
S.extend(V)
Q = int(input())

r = 0
for i in range(Q):
    c = sys.stdin.readline().split()
    
    #print('s:', S)
    #print('c:', c)

    if c[0] == '1':
        if r == 0: r+= 1
        else: r -= 1
        continue

    else:            
        if r == 1:
            S.reverse()
            r -= 1

        if c[1] == '1':
            S.appendleft(c[2])
        else:
            S.append(c[2])

if r == 1:
    S.reverse()

print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
s = []
l = input()
for i in range(len(l)):
	s.append(l[i:i+1])
g = 0
q = int(input())
for i in range(q):
	d = input().split(" ")
	if d[0] == "1":
		if g == 0:
			g = 1
		else:
			g = 0
	if d[0] == "2":
		f = d[1]
		if g == 0:
			if f == "1":
				s.insert(0, d[2])
			else:
				s.append(d[2])
		else:
			if f == "1":
				s.append(d[2])
			else:
				s.insert(0, d[2])
if g == 1:
	s.reverse()
	ans = "".join(s)
	print(ans)
else:
	ans = "".join(s)
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    s = input()
    q = int(input())
    ans = s
    rev_count = 0
    alist =[]

    for i in range(q):
        a = [i for i in input().split()]
        alist.append(a)
        if len(a) == 1:
            rev_count = rev_count +1

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

    for i in alist:
        if len(i) == 1:
            rev_count = rev_count -1
        else:
            if int(i[1]) == 1:
                if rev_count % 2 == 1:
                    ans =  ans + i[2]
                else:
                    ans =  i[2] + ans

            else:
                if rev_count % 2 == 1:
                    ans =  i[2] + ans
                else:
                    ans = ans + i[2]

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
cnt=0
for i in range(q):
    j=tuple(input().split())
    if  int(j[0])!=1 :
        if (cnt+int(j[1]))%2==1:
            s=j[2]+s
        else:
            s=s+j[2]
    else:
        cnt+=1
if cnt%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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


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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

flag = False
for q_num in q_list:
    if int(q_num[0]) == 1:
        if flag == False:
            flag = True
        else:
            flag = False
    
    if int(q_num[0]) == 2:
        if int(q_num[1]) == 1 and flag == False or int(q_num[1]) == 2 and flag == True:
            s = q_num[2] + s
        else:
            s = s + q_num[2]

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
list=[]
mojiretu=[z for z in S]
for a in range(Q):
    b=input().split()
    list.append(b)
for c in list:
    if len(c)==1:
        mojiretu.reverse()
    else:
        if c[1]=='1':
            mojiretu.insert(0,c[2])
        else:
            mojiretu.append(c[2])
print(''.join(mojiretu))

Submission status: Time Limit Exceeded

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


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

    queries = [input().split() for i in range(q)]

    for query in queries:
        if "1" == query[0]:
            s = s[::-1]
        elif "2" == query[0]:
            if "1" == query[1]:
                s = query[2] + s
            elif "2" == query[1]:
                s += query[2]

    print(s)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


def main():
    # Failed to predict input format
    S = input()
    Q = int(input())
    Queries = []
    for i in range(Q):
        q = input().split()
        if len(q) == 1:
            Queries.append(int(q[0]))
        else:
            Queries.append([int(q[0]), int(q[1]), q[2]])

    cumsum_Queries = [0]
    current = 0
    for i in range(1, Q):
        if Queries[i] == 1:
            current += 1
            cumsum_Queries.append(current)
        else:
            cumsum_Queries.append(current)

    cumsum_Queries = [current - cumsum_Queries[i] for i in range(Q)]

    if current % 2 == 0:
        S = S
    else:
        S = S[::-1]

    for i in range(Q):
        if Queries[i] == 1:
            continue
        else:
            if Queries[i][1] == 1:
                if cumsum_Queries[i] % 2 == 0:
                    S = Queries[i][2] + S
                else:
                    S += Queries[i][2][::-1]
            else:
                if cumsum_Queries[i] % 2 == 0:
                    S += Queries[i][2]
                else:
                    S = Queries[i][2][::-1] + S
    print(S)

    pass



# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main_slow():
    # Failed to predict input format
    S = input()
    Q = int(input())
    Queries = []
    for i in range(Q):
        q = input().split()
        if len(q) == 1:
            Queries.append(int(q[0]))
        else:
            Queries.append([int(q[0]), int(q[1]), q[2]])
    for i in range(Q):
        if Queries[i] == 1:
            S = S[::-1]
        else:
            if Queries[i][1] == 1:
                S = Queries[i][2] + S
            elif Queries[i][1] == 2:
                S = S + Queries[i][2]
    print(S)
    pass

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
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]
    #print("1")
    #print(ans)
  else:
    if(query[i][1]=="1"):
      ans = query[i][2] + ans
      #print("21")
      #print(ans)
    else:
      ans = ans + query[i][2]
      #print("22")
      #print(ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
typeIn = input()
S = []
for i in typeIn:
    S.append(i)
Q = int(input())
Query = []
for i in range(Q):
    a = list(input().split())
    Query.append(a)
for i in Query:
    if i[0] == '2':
        i[0] = int(i[0])
        i[1] = int(i[1])
    else:
        i[0] = int(i[0])
for i in Query:
    if i[0]==1:
        S.reverse()
    if i[0]==2:
        if i[1]==1:
            S.insert(0,i[2])
        if i[1]==2:
            S.append(i[2])
out = listToStr = ''.join([str(elem) for elem in S])
print(listToStr)

Submission status: Time Limit Exceeded

Solution code:
S=input()
n=int(input())
l = [list(input()) for _ in range(n)]

for i in range(n):
    if l[i][0]=='1':
        new_S = ''.join(list(reversed(S)))
        S=new_S
    else:
        a=l[i][4]
        if l[i][2]=='1':
            S=a+S
        else:
            S=S+a
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
Q=int(input())
which=0#0 normal #1 reverse
check=[list(map(str,input().split())) for i in range(Q)]
#print(check)
for i in range(Q):
    if check[i][0]=="1":
        which+=1
        which%=2
        continue
    if check[i][0]!="1":
        if which==0:
            if check[i][1]=="1":
                S.insert(0,check[i][2])
            if check[i][1]=="2":
                S.append(check[i][2])
        if which==1:
            if check[i][1]=="1":
                S.append(check[i][2])
            if check[i][1]=="2":
                S.insert(0,check[i][2])
if which==1:
    S=S[::-1]
print("".join(S))

Submission status: Time Limit Exceeded

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

add_to_last = []
add_to_beginning = []

mode = 1
for i in range(Q):
  query = input()
  if query=='1':
    mode *= -1
  else:
    _, F, C = query.split()
    if F=='1':
      if mode==1:
        add_to_beginning.insert(0,C)
      else:
        add_to_last.append(C)
    else:
      if mode==1:
        add_to_last.append(C)
      else:
        add_to_beginning.insert(0,C)

S = "".join(add_to_beginning+[S]+add_to_last)

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
Q = [input() for _ in range(q)]

s = list(s)
insert = s.insert
append = s.append
flag = True

for query in Q:
    if len(query) == 1:
        flag = not flag
    else:
        query = query.split()
        if query[1] == '1':
            if flag:
                insert(0, query[2])
            else:
                append(query[2])
        else:
            if flag:
                append(query[2])
            else:
                insert(0, query[2])

if not flag:
    s.reverse()

print(*s, sep='')

Submission status: Time Limit Exceeded

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    s = input()
    q = int(input())
    ans = deque()
    swap = False
    for i in range(len(s)):
        ans.append(s[i])

    for i in range(q):
        #print(ans, swap)
        inp = input()
        if len(inp) == 1:
            hoge = int(inp)
            if swap:
                swap = False
            else:
                swap = True
        else:
            # print(tuple(inp))
            fi, ci = tuple(inp)[2], tuple(inp)[4]
                       
            if int(fi) == 1:
                if not swap:
                    ans.appendleft(ci)
                else:
                    ans.append(ci)
            if int(fi) == 2:
                if not swap:
                    ans.append(ci)
                else:
                    ans.appendleft(ci)
    if swap:
        ans = list(ans)[::-1]
    else:
        ans = list(ans)
    s_ans = str()
    for i in range(len(ans)):
        s_ans += ans[i]
    print(s_ans)
                
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

cnt = 0
for _ in range(q):
  t = input()
  if t[0] == "1":
    cnt += 1
  else:
    if cnt%2 == 1:
      s = "".join(list(reversed(s)))
      cnt = 0
    _,f,c = t.split(" ")
    if f == "1":
      s = c + s
    else:
      s = s + c
if cnt%2 == 1:
  s = "".join(list(reversed(s)))
  cnt = 0

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#D
#入力
S=input()
Q=int(input())
#Query=[]
#for i in range(Q):
#    Query.append(list(input().split()))
    
    
#処理
t=0
for j in range(Q):
    i=list(input().split())
    if i[0]=="1":
        t+=1
    elif i[0]=="2":
        if t%2==1:
            S=S[::-1]
        t=0
        if i[1]=="1":
            S=i[2]+S
        elif i[1]=="2":
            S=S+i[2]
if t%2==1:
    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())

for i in query:
    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 = input()
q = int(input())
for i in range(q):
  query = input()
  if query == str(1):
    s = ''.join(list(reversed(s)))
  else:
    t, f, c = query.split(' ')
    f = int(f)
    if f == 2:
      s += c
    elif f == 1:
      s = c + s
print(s)

Submission status: Time Limit Exceeded

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

for query in P:
    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:
s = list(input())
q = int(input())

for i in range(q):
    a = list(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:
s=input()
query=int(input())

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

ans = s
rev = False
for q in li_q:
    if q[0] == '1':
        rev = not rev
    else:
        if q[1] == '1':
            if rev:
                ans = ans + q[2]
            else:
                ans = q[2] + ans
        else:
            if rev:
                ans = q[2] + ans
            else:
                ans = ans + q[2]
if rev:
    ans = ans[::-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def solve():
    a = input()
    a = list(a)
    q = int(input())
    
    rev = 0
    for _ in range(q):
        _q = [i for i in input().split()]
        t, f, c =0, 0, 0
        t = int(_q[0])
        if t == 2:
            f, c = int(_q[1]), _q[2]
        
        if t == 1:
            rev = 1 - rev
        elif t == 2:
            if (f + rev) % 2 == 1:
                a.insert(0, c)
            else:
                a.append(c)
                
    if rev == 1:
        a = list(reversed(a))
    
    print("".join(a))


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse = 0
head = []
tail = []
for i in range(Q):
    s = input().split()
    if int(s[0]) == 1:
        reverse = (reverse + 1) % 2
    else:
        if int(s[1]) == 1:
            if reverse == 1:
                tail.append(s[2])
            else:
                head.insert(0, s[2])
        else:
            if reverse == 1:
                head.insert(0, s[2])
            else:
                tail.append(s[2])
head = ''.join(head)
tail = ''.join(tail)
S = head + S + tail
if reverse == 1:
    print(S[::-1])
else:
    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:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
top = ""
end = ""
count = 0
for i in range(q):
    a = input()
    if len(a) == 1:
        count += 1
    else:
        a = list(a.split())
        if (count + int(a[1])) % 2 == 0:
            end = end + a[2]
        else:
            top = a[2] + top
ans = top + s + end
if count % 2 == 1:
    ans = ans[::-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

q = int(input())
r = 0
b = ""
c = ""
for i in range(q):
    A = list(input().split())
    if A[0] == '1':
        r += 1
    else:
        if (A[1] == '1' and r % 2 == 0) or (A[1] != '1' and r % 2 != 0):
            b = A[2] + b
        else:
            c += A[2]

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=input()
S_list=deque()
for k in range(len(S)):
    S_list.append(S[k])
Q=int(input())
kouho=[input().split() for i in range(Q)]
"""for i in range(Q):
    kari=input().split()
    kouho.append(kari)"""
for j in range(Q):
    if kouho[j][0]=="1":
        S_list.reverse()
    else:
        if kouho[j][1]=="1":
            S_list.appendleft(kouho[j][2])
        else:
            S_list.append(kouho[j][2])
print(*S_list,sep="")

Submission status: Time Limit Exceeded

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

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

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

print("".join(s))

Submission status: Time Limit Exceeded

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

rev_s = False
for i in range(q):
   tmp = list(input().split())
   if len(tmp) == 1:
      if rev_s == True:
         rev_s = False
      else:
         rev_s = True
   else:
      f, c = int(tmp[1]), tmp[2]
      if rev_s == False:
         if f == 1:
            s = c + s
         else:
            s = s + c
      else:
         if f == 1:
            s = s + c
         else:
            s = c + s

if rev_s == True:
   print(s[::-1])
else:
   print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for _ in range(q):
    t=list(map(str,input().split()))
    if int(t[0])==1:
        s=s[::-1]
        #print(s)
    if int(t[0])==2:
        f=int(t[1])
        if f==1:
            p=t[2]
            s=p+s
            #print(s)
        if f==2:
            z=t[2]
            s=s+z
            #print(s)
print(s)

Submission status: Time Limit Exceeded

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

def hanten(c):
    new_c = ''.join(list(reversed(c)))
    return new_c
def f1(c, d):
    return d + c
def f2(c, d):
    return c + d

for i in range(q):
    if qu[i][0] == '1':
        s= hanten(s)
    if qu[i][0] == '2':
        if qu[i][1] == '1':
            d = qu[i][2]
            s = f1(s, d)
        if qu[i][1] == '2':
            d = qu[i][2]
            s = f2(s,d)
print(s)

Submission status: Time Limit Exceeded

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

print(s)

Submission status: Time Limit Exceeded

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

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

  else:
    if item[1] == '1':
      S.insert(0, item[2])

    else:
      S.append(item[2])

print(''.join(S))

Submission status: Time Limit Exceeded

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


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flag_reversed = False
ret = S
front_ones = []
back_ones = []
for _ in range(Q):
    temp = input()
    if (len(temp) == 1):
        flag_reversed = not flag_reversed
    else:
        T, F, C = [x for x in temp.split(" ")]
        F = int(F)
        add_begin = True
        if (F == 2):
            add_begin = False
        if (flag_reversed):
            add_begin = not add_begin
        if (add_begin):
            front_ones.insert(0, C)
        else:
            back_ones.append(C)

ret = ''.join(front_ones) + ret + ''.join(back_ones)

if (flag_reversed):
    ret = ''.join(reversed(ret))
print(ret)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = list(input())
S = deque(S)
Q = int(input())
qlis = []
for _ in range(Q):
    qlis.append(input().split())

for i in range(Q):
    if len(qlis[i]) == 1:
        S.reverse()
    else:
        if int(qlis[i][1]) == 2:
            S.append(qlis[i][2])
        else:
            S.appendleft(qlis[i][2])

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

Submission status: Time Limit Exceeded

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

s = input()
nq = int(input())
flg = 0
for i in range(nq):
    q = input()
    if len(q) == 1:
        flg += 1
    else:
        if flg % 2 == 0:
            if q[2] == '1':
                s = q[-1]+s
            else:
                s = s+q[-1]
        else:
            if q[2] == '1':
                s = s+q[-1]
            else:
                s = q[-1]+s
if flg % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

c=0

for i in range(Q):
    if Query[i][0]=='1':
        c=c+1
    else:
        if ((Query[i][1]=='1')and(c%2==0)) or ((Query[i][1]=='2')and(c%2==1)):
            S=Query[i][2]+S
        else:
            S = S+Query[i][2]

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

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
ans=input()
n=int(input())

for i in range(n):
    k=input().split()
   
    if k[0]=="2" and k[1]=="1":
        ans=k[2]+ans
    elif k[0]=="2" and k[1]=="2":
        ans=ans+k[2]
    elif k[0]=="1":
        ans=ans[::-1]

print(ans)

Submission status: Time Limit Exceeded

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

isreverse = False
head=[]
tail=[]

for i in range(Q):
    query = input()
    if str(query)[0]=="1":
        isreverse = isreverse^True
    else:
        F = int(query.split()[1])
        C = str(query.split()[2])
        if (F==1 and isreverse==False) or (F==2 and isreverse==True):
            head.insert(0,C)
        else:
            tail.append(C)


if isreverse==False:
    print("".join(head)+S+"".join(tail))
else:
    print("".join(reversed(tail))+S[::-1]+"".join(reversed(head)))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
ok=1
for i in range(q):
  query=list(input().split())
  if query[0]=='1':
    ok=(ok+1)%2
  else:
    f,c=query[1],query[2]
    if ok:
      if f=='1':
        s=c+s
      else:
        s+=c
    else:
      if f=='1':
        s=s+c
      else:
        s=c+s
if ok:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
st = input()
q = int(input())
for i in range(q):
    ipt = input()
    if ipt == "1":
        st = st[::-1]
    else:
        _, f, c = ipt.split()
        if f == "1":
            st = c + st
        else:
            st = st + c
print(st)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import deque
s = deque(input())
q = int(input())
a = deque()
for i in range(q):
    a.append([str(i) for i in input().split()])

for i in range(q):
    if a[i][0] == '1':
        s.reverse()
    if a[i][0] == '2':
        if a[i][1] == '1':
            s.appendleft(a[i][2])
        if a[i][1] == '2':
            s.append(a[i][2])
print(''.join(s))

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
def main():
  s=input()
  q=int(input())
  rev=0
  for _ in range(q):
    query=input().split()
    if(query[0]=='1'):
      rev=1-rev
    elif((int(query[1])+rev)%2==1):
      s=query[2]+s
    else:
      s=s+query[2]
  if rev==1:
    return s[::-1]
  return s
print(main())

Submission status: Time Limit Exceeded

Solution code:
S=(input())

Q=int(input())

for _ in range(Q):
    temp=list(input().split())
    if len(temp)==1:
        S=S[::-1]
    else:
        if temp[1]=="1":
            S=temp[2]+S
        else:
            S=S+temp[2]
            
print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
total_t=1
for i in range(q):
  t=input()
  if t=="1":
    total_t=total_t*(-1)
  else:
    f=(t.split())[1]
    c=(t.split())[2]
    if (f=="1" and total_t==1) or (f=="2" and total_t==-1):
      s=c+s
    else:
      s=s+c
if total_t==-1:
  new_s=s
  s=new_s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys
input=sys.stdin.readline
moji=deque([])
s=input().rstrip()
for i in range(len(s)):
	moji.append(ord(s[i]))
q=int(input())
rev=0
for _ in range(q):
	qw=input().split()
	if qw[0]=="1":
		rev+=1
	else:
		f=qw[1]
		c=qw[2]
		if f=="1":
			if rev%2==0:
				moji.appendleft(ord(c))
			else:
				moji.append(ord(c))
		else:
			if rev%2==0:
				moji.append(ord(c))
			else:
				moji.appendleft(ord(c))
ans=""
alph="abcdefghijklmnopqrstuvwxyz"
for i in range(len(moji)):
	ans+=alph[moji[i]-97]
if rev%2==0:
	print(ans)
else:
	print(ans[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
ans = S
flag = 0
for i in range(Q):
  cnt = 0
  q = list(map(str, input().split()))
  if(len(q)==1):
    cnt += 1
    if(flag==0):
      flag = 1
    else:
      flag = 0
  else:
    cnt = 0
    if(q[1]=="1"):
      if(flag==0):
        ans = q[2] + ans
      else:
        ans += q[2]
    else:
      if(flag==0):
        ans += q[2]
      else:
        ans = q[2] + ans
if( flag%2 == 0 ):
  print(ans)
else:
  print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
a = sys.stdin.read().split('\n')[:-1]
s = a[0]
q = int(a[1])
f = 1
#for i in range(0, q):
for i in range(2, q + 2):
    if a[i][0] == '1':
        f = 1 - f
    if a[i][0] == '2':
        if a[i][2] == '21'[f]:
            s = a[i][4] + s
        else:
            s += a[i][4]
if f == 1:
    print(s)
else:
    b = ''
    for c in reversed(s):
        b += c
    print(b)

Submission status: Time Limit Exceeded

Solution code:
# 入力文字列
get_str = input()

# 操作回数
N = int(input())

# 操作
for i in range(N):
    # 取得操作
    inp_cmd = input().split()

    # inp_cmd[0] = 1 なら反転
    if(int(inp_cmd[0]) == 1):
        get_str = get_str[::-1]
    # inp_cmd[0] = 2
    else:
        # inp_cmd[1]なら、先頭に文字追加
        if(int(inp_cmd[1]) == 1):
            get_str = inp_cmd[2] + get_str
        # inp_cmd[2]なら、末尾に文字追加
        else:
            get_str = get_str + inp_cmd[2]
print(get_str)

Submission status: Time Limit Exceeded

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

for i in range(q):
    query = input().split()
    if query[0] == "1":
        count += 1
        rev = not rev
        continue

    if query[1] == "1":

        if rev:
            t = [s, query[2]]
            s = "".join(t)
        else:
            t = [query[2], s]
            s = "".join(t)
    else:
        if rev:
            t = [query[2], s]
            s = "".join(t)
        else:
            t = [s, query[2]]
            s = "".join(t)

if count & 1:
    print(s[::-1])
else:
    print(s)