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:
from collections import deque

s=deque(list(input()))
for i in range(int(input())):
  dir=input().split()
  if int(dir[0])==1:s.reverse()
  else:
    if int(dir[1])==1:s.appendleft(dir[2])
    else:s.append(dir[2])
s=list(s)
print(*s,sep='')

Submission status: Time Limit Exceeded

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

st = True
for _ in range(Q):
    Query = input().split()
    if Query[0] == '1':
        st = not st
    else:
        if (Query[1] == '1') ^ st:
            S += Query[2]
        else:
            S = Query[2] + S
if st:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

r = False
for _ in range(Q):
    query = input()
    if query == '1':
        r ^= True
    else:
        _, f, c = query.split()
        front = f == '1'

        if r ^ front:
            S = c + S
        else:
            S = S + c

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
direction = True
for _ in range(Q):
    line = input()
    if line[0] == '1':
        direction = not direction
    else:
        if (direction and line[2] == '1') or (not direction and line[2] == '2'):
            S = line[4] + S
        else:
            S = S + line[4]
print(S if direction else S[::-1])

Submission status: Time Limit Exceeded

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

c = input()
c = c[:len(c)-1]
q = int(input())

num = len(c)

hantei = 1

for i in range(q):
    q_i= list(input())
    #print(q_i)
    if q_i[0] == '1':
        hantei *= -1
    elif (q_i[2] =='1' and hantei == 1) or (q_i[2] == '2' and hantei == -1):
            c = q_i[4] +c
    elif (q_i[2] =='2' and hantei == 1) or (q_i[2] == '1' and hantei == -1):
            c += q_i[4]

if hantei == -1:
    c = c[::-1]


print(c)

Submission status: Time Limit Exceeded

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

c = input()
c = c[:len(c)-1]
q = int(input())

num = len(c)

hantei = 0

for i in range(q):
    q_i= list(input())
    #print(q_i)
    if q_i[0] == '1' and hantei ==0:
        hantei = 1
    elif q_i[0] == '1' and hantei == 1:
        hantei = 0
    elif (q_i[2] =='1' and hantei == 0) or (q_i[2] == '2' and hantei == 1):
            c = q_i[4] +c
    elif (q_i[2] =='2' and hantei == 0) or (q_i[2] == '1' and hantei ==1):
            c += q_i[4]



if hantei == 1:
    c = c[::-1]


print(c)

Submission status: Time Limit Exceeded

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

c = list(input())
c = c[:len(c)-1]
q = int(input())

num = len(c)

q_i =  [list(input()) for _ in range(q)]

#print(q_i)

hantei = 0

for i in range(q):
    #q_i= list(input())
    #print(q_i)
    if q_i[i][0] == '1' and hantei ==0:
        hantei = 1
    elif q_i[i][0] == '1' and hantei == 1:
        hantei = 0
    elif (q_i[i][2] =='1' and hantei == 0) or (q_i[i][2] == '2' and hantei == 1):
            c = [q_i[i][4]] +c
    elif (q_i[i][2] =='2' and hantei == 0) or (q_i[i][2] == '1' and hantei ==1):
            c += [q_i[i][4]]



if hantei == 0:
    ans = ''
    for i in range(len(c)):
        ans += c[i]
else:
    ans = ''
    for i in range(len(c)):
        ans = c[i] + ans


print(ans)

Submission status: Time Limit Exceeded

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

c = list(input())
c = c[:len(c)-1]
q = int(input())

num = len(c)

hantei = 0

for i in range(q):
    q_i= list(input())
    #print(q_i)
    if q_i[0] == '1' and hantei ==0:
        hantei = 1
    elif q_i[0] == '1' and hantei == 1:
        hantei = 0
    elif (q_i[2] =='1' and hantei == 0) or (q_i[2] == '2' and hantei == 1):
            c = [q_i[4]] +c
    elif (q_i[2] =='2' and hantei == 0) or (q_i[2] == '1' and hantei ==1):
            c += [q_i[4]]



if hantei == 0:
    ans = ''
    for i in range(len(c)):
        ans += c[i]
else:
    ans = ''
    for i in range(len(c)):
        ans = c[i] + ans


print(ans)

Submission status: Time Limit Exceeded

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

rev_count = 0
for i in range(Q):
    que = input()
    if que[0] == '1':
        rev_count +=1
    elif que[0] == '2':
        if (rev_count + int(que[2]))%2 == 0:
            s = s + que[4]
        else:
            s = que[4] + s

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

Submission status: Time Limit Exceeded

Solution code:
#158_D
s = input()
q = int(input())

rev = 1
for _ in range(q):
    a = list(input().split())
    if a[0] == "1":
        rev *= -1
    elif a[0] == "2":
        if (a[1]=="1" and rev==1) or (a[1]=="2" and rev==-1):
            s = a[2] + s
        elif (a[1]=="2" and rev==1) or (a[1]=="1" and rev==-1):
            s = s + a[2]
if rev == -1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
#158_D
s = input()
q = int(input())
for _ in range(q):
    a = list(input().split())
    if a[0] == "1":
        s = s[::-1]
    elif a[0] == "2":
        if a[1] == "1":
            s = a[2] + s
        elif a[1] == "2":
            s = s + a[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = [c for c in input()]
    q = int(input())
    is_fwd = True
    for i in range(q):
        fields = [i for i in input().split()]
        t = int(fields[0])
        if t == 1:
            is_fwd = not is_fwd
        elif t == 2:
            f = int(fields[1])
            c = fields[2]
            if (f == 1) == is_fwd:
                s = [c] + s
            else:
                s.append(c)

    if is_fwd:
        print(''.join(s))
    else:
        print(''.join(reversed(s)))

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    is_fwd = True
    for i in range(q):
        fields = [i for i in input().split()]
        t = int(fields[0])
        if t == 1:
            is_fwd = not is_fwd
        elif t == 2:
            f = int(fields[1])
            c = fields[2]
            if (f == 1) == is_fwd:
                s = c + s
            else:
                s += c

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

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    for i in range(q):
        fields = [i for i in input().split()]
        t = int(fields[0])
        if t == 1:
            s = s[::-1]
        elif t == 2:
            f = int(fields[1])
            c = fields[2]
            if f == 1:
                s = c + s
            elif f == 2:
                s += c

    print(s)

main()

Submission status: Time Limit Exceeded

Solution code:
S=input()
N=int(input())
count=0
for _ in range(N):
    *Q,=input().split()
    if len(Q)==1:
        count+=1
    else:
        if (count+int(Q[1]))%2:
            S=Q[2]+S
        else:
            S=S+Q[2]
print(S[::-1] if count%2 else S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
N=int(input())
for _ in range(N):
    *Q,=input().split()
    if len(Q)==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())
for i in range(Q):
  q = input()
  if len(q)==1:S=S[::-1]
  else:
    n,f,c=q.split()
    if f=='1':S=c+S
    else:S=S+c
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

print(s)

Submission status: Time Limit Exceeded

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

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

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

print(s)

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
r=True
for i in range(n):
    q = input()
    if q[0] == "1":
        r=not(r)
    else:
        t, f, c = map(str, q.split())
        if (f == "1" and r==True) or (f=="2" and r==False):
            s = c+s
        else:
            s =s+c
if r==False:
    s=s[::-1]

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())

flag = False
s = rr()
q = ri()
for _ in range(q):
    query = list(rs())
    if query[0] == '1':
        flag = not flag
        continue
    else:
        f = int(query[1])
        c = query[-1]
        if flag == True:
            if f == 1:
                s += c
            else:
                s = c+s
        else:
            if f == 1:
                s = c+s
            else:
                s += c
if flag:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())

flag = False
s = rr()
q = ri()
for _ in range(q):
    query = list(rs())
    if query[0] == '1':
        flag = not flag
        continue
    else:
        f = int(query[1])
        c = query[-1]
        if flag == True:
            if f == 1:
                s += c
            else:
                s = c+s
        else:
            if f == 1:
                s = c+s
            else:
                s += c
if flag:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

s = input().rstrip()
q = int(input())
query = [input().split() for _ in range(q)]

flag = False
for i in range(q):
    qq = query[i]
    if qq[0] == '1':
        flag = not(flag)
    else:
        if flag:
            if qq[1] == '1':
                s = s + qq[2]
            else:
                s = qq[2] + s
        else:
            if qq[1] == '1':
                s = qq[2] + s
            else:
                s = s + qq[2]

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

s = input().rstrip()
q = int(input())
query = [input().rstrip().split() for _ in range(q)]

flag = False
for i in range(q):
    qq = query[i]
    if qq[0] == '1':
        flag = not(flag)
    else:
        if flag:
            if qq[1] == '1':
                s = s + qq[2]
            else:
                s = qq[2] + s
        else:
            if qq[1] == '1':
                s = qq[2] + s
            else:
                s = s + qq[2]

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

s = input().rstrip()
q = int(input())

flag = False
for i in range(q):
    qq = input().rstrip().split()
    if qq[0] == '1':
        flag = not(flag)
    else:
        if flag:
            if qq[1] == '1':
                s = s + qq[2]
            else:
                s = qq[2] + s
        else:
            if qq[1] == '1':
                s = qq[2] + s
            else:
                s = s + qq[2]

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input("")
q = int(input())
cnt = 1
for _ in range(q):
    list_q = list(map(str, input().split()))
    if list_q[0] == "1":
        cnt *= -1
    elif list_q[0] == "2":

        if list_q[1] == "1" and cnt == 1:
            s = list_q[2] + s
        elif list_q[1] == "1" and cnt == -1:
            s = s + list_q[2]
        elif list_q[1] == "2" and cnt == 1:
            s = s + list_q[2]
        elif list_q[1] == "2" and cnt == -1:
            s = list_q[2] + s

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

Submission status: Time Limit Exceeded

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

R = 0

for i in range(Q):
    Query = str(input())
    if len(Query) == 1:
        R = (R+1)%2
    else:
        T,F,C = map(str,Query.split())

        if (R + int(F) ) % 2 == 1:
            S.insert(0,C)
        else:
            S.append(C)

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

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    Query = str(input())
    if len(Query) == 1:
        S = S[::-1]
    else:
        T,F,C = map(str,Query.split())

        if F == "1":
            S.insert(0,C)
        else:
            S.append(C)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

s, q = input()[:-1], int(input())
for _ in range(q):
    o = input()[:-1].split()
    if len(o) == 1: s = s[::-1]
    else:
         if o[1] == '1': s = o[2] + s
         else: s += o[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q =int(input())
ten = 0
for _ in range(q):
    ls = tuple(map(str, input().split()))
    if len(ls)==1:
       ten +=1
    else:
        if ls[1] =='1':
            if ten%2 ==0:
                s = ls[2] +s
            else:
                s = s + ls[2]
        else:
            if ten%2 ==1:
                s = ls[2] +s
            else:
                s = s + ls[2]
if ten%2 ==1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
class DoublyLinkedList:
    def __init__(self, num):
        self.elements = [None for _ in range(300001)]
        self.elements[0] = [None, num, None]
        self.top = 0
        self.bottom = 0
        self.cnt = 0

    def push_top(self, val):
        self.cnt += 1
        self.elements[self.top][0] = self.cnt
        self.elements[self.cnt] = [None, val, self.top]
        self.top = self.cnt

    def push_bottom(self, val):
        self.cnt += 1
        self.elements[self.bottom][2] = self.cnt
        self.elements[self.cnt] = [self.bottom, val, None]
        self.bottom = self.cnt

    @staticmethod
    def from_str(s):
        ret = DoublyLinkedList(s[0])
        for v in s[1:]:
            ret.push_bottom(v)
        return ret

    def to_str(self):
        cur = self.top
        val = self.elements[self.top][1]
        while self.elements[cur][2] is not None:
            val += self.elements[self.elements[cur][2]][1]
            cur = self.elements[cur][2]
        return val


def main():
    s = input()
    d = DoublyLinkedList.from_str(s)
    q = int(input())
    swapped = 0
    for _ in range(q):
        query = input()
        if len(query) == 1:
            swapped = 1 - swapped
            continue
        _, f, c = query.split()
        if swapped:
            if f == '1':
                d.push_bottom(c)
            else:
                d.push_top(c)
        else:
            if f == '1':
                d.push_top(c)
            else:
                d.push_bottom(c)

    if swapped:
        print(d.to_str()[::-1])
    else:
        print(d.to_str())


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
class DoblyLinkedList:
    class node:
        def __init__(self, pre, val, nex):
            self.val = val
            self.pre = pre
            self.nex = nex

    def __init__(self, num):
        self.elements = [None for _ in range(300001)]
        self.elements[0] = self.node(None, num, None)
        self.top = 0
        self.bottom = 0
        self.cnt = 0

    def push_top(self, val):
        self.cnt += 1
        self.elements[self.top].pre = self.cnt
        node = self.node(None, val, self.top)
        self.elements[self.cnt] = node
        self.top = self.cnt

    def push_bottom(self, val):
        self.cnt += 1
        self.elements[self.bottom].nex = self.cnt
        node = self.node(self.bottom, val, None)
        self.elements[self.cnt] = node
        self.bottom = self.cnt

    @staticmethod
    def from_str(s):
        ret = DoblyLinkedList(s[0])
        for v in s[1:]:
            ret.push_bottom(v)
        return ret

    def to_str(self):
        cur = self.top
        val = self.elements[self.top].val
        while self.elements[cur].nex is not None:
            val += self.elements[self.elements[cur].nex].val
            cur = self.elements[cur].nex
        return val


def main():
    s = input()
    d = DoblyLinkedList.from_str(s)
    q = int(input())
    swapped = 0
    for _ in range(q):
        query = input()
        if len(query) == 1:
            swapped = 1 - swapped
            continue
        _, f, c = query.split()
        if swapped:
            if f == '1':
                d.push_bottom(c)
            else:
                d.push_top(c)
        else:
            if f == '1':
                d.push_top(c)
            else:
                d.push_bottom(c)

    if swapped:
        print(d.to_str()[::-1])
    else:
        print(d.to_str())


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
class DoblyLinkedList:
    class node:
        def __init__(self, pre, val, nex):
            self.val = val
            self.pre = pre
            self.nex = nex

    def __init__(self, num):
        self.elements = {0: self.node(None, num, None)}
        self.top = 0
        self.bottom = 0
        self.cnt = 0

    def push_top(self, val):
        self.cnt += 1
        self.elements[self.top].pre = self.cnt
        node = self.node(None, val, self.top)
        self.elements[self.cnt] = node
        self.top = self.cnt

    def push_bottom(self, val):
        self.cnt += 1
        self.elements[self.bottom].nex = self.cnt
        node = self.node(self.bottom, val, None)
        self.elements[self.cnt] = node
        self.bottom = self.cnt

    @staticmethod
    def from_str(s):
        ret = DoblyLinkedList(s[0])
        for v in s[1:]:
            ret.push_bottom(v)
        return ret

    def to_str(self):
        cur = self.top
        val = self.elements[self.top].val
        while self.elements[cur].nex is not None:
            val += self.elements[self.elements[cur].nex].val
            cur = self.elements[cur].nex
        return val


def main():
    s = input()
    d = DoblyLinkedList.from_str(s)
    q = int(input())
    swapped = 0
    for _ in range(q):
        query = input()
        if len(query) == 1:
            swapped = 1 - swapped
            continue
        _, f, c = query.split()
        if swapped:
            if f == '1':
                d.push_bottom(c)
            else:
                d.push_top(c)
        else:
            if f == '1':
                d.push_top(c)
            else:
                d.push_bottom(c)



    if swapped:
        print(d.to_str()[::-1])
    else:
        print(d.to_str())


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(Q):
    array=input().split()
    array[0]=int(array[0])

    if array[0]==1:
        S=S[::-1]

    if array[0]==2:
        array[1]=int(array[1])
        if array[1]==1:
            S=''.join([array[2],S])
            
        else:
            S=''.join([S,array[2]])

print(S)

Submission status: Time Limit Exceeded

Solution code:

S=input()
Q=int(input())
top=0
for i in range(Q):
    li=input().split()
    if int(li[0])==1:
        if top==0:
            top=-1
        else:
            top=0
    elif int(li[0])==2:
        if int(li[1])==1:#先頭にくっつける
            if top==0:
                S=str(li[2])+S
            else:
                S=S+str(li[2])
        elif int(li[1])==2:#後ろにくっつける
            if top==0:
                S=S+str(li[2])
            else:
                S=str(li[2])+S
if top==0:
    print(S)
else:
    print(S[::top])

Submission status: Time Limit Exceeded

Solution code:

S=input()
Q=int(input())
rev_S=S[::-1]

for i in range(Q):
    li=input().split()
    if int(li[0])==1:
        new_S = rev_S
        new_rev_S = S
        #書き換え
        S=new_S
        rev_S=new_rev_S
    elif int(li[0])==2:
        if int(li[1])==1:
            S=str(li[2])+S
            rev_S=rev_S+str(li[2])
        elif int(li[1])==2:
            S=S+str(li[2])
            rev_S=str(li[2])+rev_S
print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for qq in range(q):
  l=list(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:
import sys
input = sys.stdin.readline


# 持っているビスケットを叩き、1枚増やす
# ビスケット A枚を 1円に交換する
# 1円をビスケット B枚に交換する
def main():
    s = input().strip()
    q = int(input())
    flg = True
    for i in range(q):
        t = input_list_str()
        if len(t) == 1:
            n = t[0]
            flg = False if flg is True else True
        else:
            n, f, c = t
            c = str(c).strip()
            if f == '1':
                if flg:
                    s = c + s
                else:
                    s = s + c
            else:
                if flg:
                    s = s + c
                else:
                    s = c + s
    if flg is False:
        s = s[::-1]
    print(s)


def bi(num, a, b, x):
    print((a * num) + (b * len(str(b))))
    if (a * num) + (b * len(str(b))) <= x:
        return False
    return True

def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

for _ in range(q):
    Q = list(map(str, input().split()))
    if Q[0] == "1":
        s = s[::-1]
    else:
        if Q[1] == "1":
            s = Q[2] + s
        else:
            s = s + Q[2]

print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
q = INT()
p = [input() for i in range(q)]
flag = 0

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

Submission status: Time Limit Exceeded

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
q = INT()
p = [input() for i in range(q)]

for i in range(q):
        if int(p[i][0]) == 1:
                s = s[::-1]
        else:
                t, f, c = p[i].split()
                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())

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
rev = 1
for _ in range(Q):
    query = input()
    if len(query) == 1:
        rev *= -1
    else:
        T, F, C = query.split()
        if rev == 1:
            if F == "1":
                S = C + S
            else:
                S = S + C
        else:
            if F == "1":
                S = S + C
            else:
                S = C + S

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


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

for _ in range(Q):
    Query = input().split()
    if Query[0] == "1":
        S = deque(reversed(S))
    else:
        F, C = Query[1], Query[2]
        if F == "1":
            S.appendleft(C)
        else:
            S.append(C)

print("".join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for i in range(q):
    t=list(input().split())
    t[0]=int(t[0])
    if t[0]==1:
        s="".join(list(reversed(s)))
    else:
        f=int(t[1])
        g=t[2]
        if f==1:
            s=g+s
        else:
            s=s+g
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse_index = -1

for i in range(Q):
    query = input()
    if query[0] == '1':
        reverse_index *= -1
    elif query[0] == '2':
        if query[2] == '1':
            if reverse_index == -1:
                S = query[4] + S
            else:
                S = S + query[4]
        else:
            if reverse_index == -1:
                S = S + query[4]
            else:
                S = query[4] + S

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse_index = -1

for i in range(Q):
    query = input()
    query = query.replace(' ', '')
    if query[0] == '1':
        reverse_index *= -1
    elif query[0] == '2':
        if query[1] == '1':
            if reverse_index == -1:
                S = query[2] + S
            else:
                S = S + query[2]
        else:
            if reverse_index == -1:
                S = S + query[2]
            else:
                S = query[2] + S

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