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:
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors

#maincode-------------------------------------------------
s = input()
q = ini()
cnt_one = 0
mae = ''
bac = ''
for i in range(q):
    a = input()
    if a[0] == '1':
        cnt_one += 1
        temp = mae
        mae = bac[::-1]
        bac = temp[::-1]
    else:
        if a[2] == '1':
            mae = a[4] + mae
        else:
            bac = bac + a[4]
if cnt_one%2:
    s = s[::-1]
print(mae + s + bac)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
cnt=0
for i in range(q):
    query=list(input().split())
    if query[0]=="1":
        cnt+=1
    else:
        if (cnt+int(query[1]))%2==1:
            s=query[2]+s
        else:
            s=s+query[2]
if cnt%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=list(input().split())
    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=input()
n=int(input())
for i in range(n):
    l=list(input().split())
    if l[0]=="1":
        s=s[::-1]
    if l[0]=="2":
        if l[1]=="1":
            s=l[2]+s
        if l[1]=="2":
            s=s+l[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque as dq
L=dq(list(input()))
for _ in range(int(input())):
    S=input().split()
    if S == ['1']:
        L.reverse()
    else:
        if S[1]=='1':
        	L.appendleft(S[2])
        else:
        	L.append(S[2])
print(''.join(L))

Submission status: Time Limit Exceeded

Solution code:
str1 = input()
num = int(input())
for i in range(num):
    flag1 = input().split()
    if int(flag1[0]) == 1:
        str1 = str1[::-1]
    else:
        if int(flag1[1]) == 2:
            str1 = str1 + flag1[2]
        else:
            str1 = flag1[2] + str1
print(str1)

Submission status: Time Limit Exceeded

Solution code:
str1 = input()
num = int(input())
i = 0
while i < num:
    flag1 = input()
    if len(flag1) == 1:
        str1 = str1[::-1]
    else:
        trash, flag2, char = map(str, flag1.split())
        if int(flag2) == 2:
            str1 = str1 + char
        else:
            str1 = char + str1
    i += 1
print(str1)

Submission status: Time Limit Exceeded

Solution code:
str1 = input()
num = int(input())
for i in range(num):
    flag1 = input()
    if len(flag1) == 1:
        str1 = str1[::-1]
    else:
        trash, flag2, char = map(str, flag1.split())
        if int(flag2) == 2:
            str1 = str1 + char
        else:
            str1 = char + str1
print(str1)

Submission status: Time Limit Exceeded

Solution code:
str1 = input()
num = int(input())
for i in range(num):
    flag1 = input()
    if len(flag1) == 1:
        str1 = str1[::-1]
    elif len(flag1) > 1:
        trash, flag2, char = map(str, flag1.split())
        if int(flag2) == 2:
            str1 = str1 + char
        else:
            str1 = char + str1
print(str1)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
rev = False
for i in range(q):
    inp = input()
    if inp[0] == "1":
        if rev:
            rev = False
        else:
            rev = True
    elif inp[2] == "1":
        a = inp[4]
        if rev:
            s.append(a)
        else:
            s.insert(0, a)
    else:
        a = inp[4]
        if rev:
            s.insert(0, a)
        else:
            s.append(a)
if rev:
    s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
rev = False
for i in range(q):
    inp = input().split()
    if len(inp) == 1:
        if rev:
            rev = False
        else:
            rev = True
    elif inp[1] == "1":
        a = inp[2]
        if rev:
            s.append(a)
        else:
                s.insert(0, a)
    else:
        a = inp[2]
        if rev:
            s.insert(0, a)
        else:
            s.append(a)
if rev:
    s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
c = 0
left_l = ['', '']
right_l = ['', '']
s_l = [s, s[::-1]]
for _ in range(n):
  lst = [i for i in input().split()]
  if lst[0] == '1':
    if c == 1:
      s_l[0], s_l[1] = s_l[1], s_l[0]
      #print(s)
    else:
      s_l = [right_l[1] + s_l[1] + left_l[1], left_l[0] + s_l[0] + right_l[0]]
      left_l[0], left_l[1] = '', ''
      right_l[0], right_l[1] = '', ''
      c = 1
      #print(s)
  elif lst[0] == '2':
    c = 0
    if lst[1] == '1':
      left_l[0] = lst[2] + left_l[0]
      left_l[1] = left_l[1] + lst[2]
    elif lst[1] == '2':
      right_l[0] = right_l[0] + lst[2]
      right_l[1] = lst[2] + right_l[1]
s = left_l[0] + s_l[0] + right_l[0]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
c = 0
left_l = ['', '']
right_l = ['', '']
s_l = [s, s[::-1]]
for _ in range(n):
  lst = [i for i in input().split()]
  if lst[0] == '1':
    if c == 1:
      s_l[0], s_l[1] = s_l[1], s_l[0]
      #print(s)
    else:
      s_l = [right_l[1] + s_l[1] + left_l[1], left_l[0] + s_l[0] + right_l[0]]
      left_l = ['', '']
      right_l = ['', '']
      c = 1
      #print(s)
  elif lst[0] == '2':
    c = 0
    if lst[1] == '1':
      left_l = [lst[2] + left_l[0], left_l[1] + lst[2]]
    elif lst[1] == '2':
      right_l = [right_l[0] + lst[2], lst[2] + right_l[1]]
w = left_l[0] + s_l[0] + right_l[0]
print(w)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
c = 0
left_l = ['', '']
right_l = ['', '']
s_l = [s, s[::-1]]
for _ in range(n):
  lst = [i for i in input().split()]
  if lst[0] == '1':
    if c == 1:
      s_l[0], s_l[1] = s_l[1], s_l[0]
      #print(s)
    else:
      s_l = [right_l[1] + s_l[1] + left_l[1], left_l[0] + s_l[0] + right_l[0]]
      left_l = ['', '']
      right_l = ['', '']
      c = 1
      #print(s)
  elif lst[0] == '2':
    c = 0
    if lst[1] == '1':
      left_l = [lst[2] + left_l[0], left_l[1] + lst[2]]
    elif lst[1] == '2':
      right_l = [right_l[0] + lst[2], lst[2] + right_l[1]]
s = left_l[0] + s_l[0] + right_l[0]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
c = 0
left_l = ['', '']
right_l = ['', '']
s_l = [s, s[::-1]]
for _ in range(n):
  lst = [i for i in input().split()]
  if lst[0] == '1':
    if c == 1:
      s_l[0], s_l[1] = s_l[1], s_l[0]
      s = s_l[0]
      #print(s)
    else:
      s_l = [left_l[0] + s_l[0] + right_l[0], right_l[1] + s_l[1] + left_l[1]]
      s_l[0], s_l[1] = s_l[1], s_l[0]
      s = s_l[0]
      left_l = ['', '']
      right_l = ['', '']
      c = 1
      #print(s)
  elif lst[0] == '2':
    c = 0
    if lst[1] == '1':
      left_l = [lst[2] + left_l[0], left_l[1] + lst[2]]
    elif lst[1] == '2':
      right_l = [right_l[0] + lst[2], lst[2] + right_l[1]]
s = left_l[0] + s_l[0] + right_l[0]
print(s)

Submission status: Time Limit Exceeded

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

querys = []
is_reversed = False
for _ in range(Q):
    tmp = input().split()
    if len(tmp)==1:
        is_reversed = not(is_reversed)
    else:
        if tmp[1]==str(1):
            if is_reversed:
                S = S+tmp[2]
            else:
                S = tmp[2]+S
        elif tmp[1]==str(2):
            if is_reversed:
                S = tmp[2]+S
            else:
                S = S+tmp[2]
if is_reversed:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

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

querys = list(S)
is_reversed = False
for _ in range(Q):
    tmp = input().split()
    if len(tmp)==1:
        is_reversed = not(is_reversed)
    else:
        if tmp[1]==str(1):
            if is_reversed:
                querys.append(tmp[2])
            else:
                tmp = [tmp[2]]
                tmp.extend(querys)
                querys=tmp
        elif tmp[1]==str(2):
            if is_reversed:
                tmp = [tmp[2]]
                tmp.extend(querys)
                querys=tmp
            else:
                querys.append(tmp[2])
if is_reversed:
    querys=querys[::-1]
S = ""
for q in querys:
    S+=q
print(S)

Submission status: Time Limit Exceeded

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

querys = []
for _ in range(Q):
    tmp = input().split()
    if len(tmp)==1:
        S = S[::-1]
    else:
        if tmp[1]==str(1):
            S = tmp[2]+S
        if tmp[1]==str(2):
            S = S+tmp[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
c = 0
for _ in range(n):
  lst = [i for i in input().split()]
  if lst[0] == '1':
    if c == 1:
      s, tmp = tmp, s
      #print(s)
      #print(tmp)
    else:
      tmp = s
      s = s[::-1]
      c = 1
      #print(s)
      #print(tmp)
  elif lst[0] == '2':
    c = 0
    if lst[1] == '1':
      s = lst[2] + s
    elif lst[1] == '2':
      s = s + lst[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
c = 0
for _ in range(n):
  lst = [i for i in input().split()]
  if lst[0] == '1':
    if c == 1:
      s, tmp = tmp, s
      #print(s)
      #print(tmp)
    else:
      tmp = s
      l = len(s)
      w = ''
      for i in range(l):
        w += s[l - i - 1]
      s = w
      c = 1
      #print(s)
      #print(tmp)
  elif lst[0] == '2':
    c = 0
    if lst[1] == '1':
      s = lst[2] + s
    elif lst[1] == '2':
      s = s + lst[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s= input();
q = int(input());
flag = 0;
while(q>0):
    q-=1;
    ls = list(map(str,input().split(" ")));
    if(ls[0]=='1'):
        flag +=1;
    else:
        if(flag%2 == 1):
            if(ls[1]=='2'):
                s=ls[2]+s;
            else:
                s=s+ls[2];
        else:
            
            if(ls[1]=='1'):
                s=ls[2]+s;
            else:
                s=s+ls[2];
if(flag%2==0):
    print(s);
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    S=input()
    Q=int(input())
    Left=[]
    Right=[]

    reverse_flag=0
    for i in range(Q):
        query=input().split()
        if query[0]=="1":
            if reverse_flag==1:
                reverse_flag=0
            else:
                reverse_flag=1
        else:
            if query[1]=="1":
                if reverse_flag==0:
                    Left.append(query[2])
                else:
                    Right.append(query[2])
            else:
                if reverse_flag==0:
                    Right.append(query[2])
                else:
                    Left.append(query[2])

    res=""
    if reverse_flag==0:
        for i in range(len(Left)-1,-1,-1):
            res+=Left[i]
        res+=S
        for r in Right:
            res+=r
    else:
        for i in range(len(Right)-1,-1,-1):
            res+=Right[i]
            
        for i in range(len(S)-1,-1,-1):
            res+=S[i]

        for l in Left:
            res+=l
    print(res)

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    S=input()
    Q=int(input())
    Left=[]
    Right=[]

    reverse_flag=0
    for i in range(Q):
        query=input().split()
        if query[0]=="1":
            if reverse_flag==1:
                reverse_flag=0
            else:
                reverse_flag=1
        else:
            if query[1]=="1":
                if reverse_flag==0:
                    Left.append(query[2])
                else:
                    Right.append(query[2])
            else:
                if reverse_flag==0:
                    Right.append(query[2])
                else:
                    Left.append(query[2])

    res=""
    if reverse_flag==0:
        while len(Left)>0:
            res+=Left.pop()
        res+=S
        for r in Right:
            res+=r
    else:
        while len(Right)>0:
            res+=Right.pop()
        
        for i in range(len(S)-1,-1,-1):
            res+=S[i]
        
        for l in Left:
            res+=l
    print(res)

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    S=input()
    Q=int(input())
    Left=[]
    Right=[]

    reverse_flag=0
    for i in range(Q):
        query=input().split()
        if query[0]=="1":
            if reverse_flag==1:
                reverse_flag=0
            else:
                reverse_flag=1
        else:
            if query[1]=="1":
                if reverse_flag==0:
                    Left.append(query[2])
                else:
                    Right.append(query[2])
            else:
                if reverse_flag==0:
                    Right.append(query[2])
                else:
                    Left.append(query[2])

    res=""
    if reverse_flag==0:
        while len(Left)>0:
            res+=Left.pop()
        res+=S
        for r in Right:
            res+=r
    else:
        while len(Right)>0:
            res+=Right.pop()
        S=list(S)
        while len(S)>0:
            res+=S.pop()
        for l in Left:
            res+=l
    print(res)

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

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

for i in range(q):
    qu = input().split()
    if int(qu[0]) == 1:
        s = list(s)
        s.reverse()
        s = "".join(s)
    elif int(qu[0]) == 2:
        if int(qu[1]) == 1:
            s = qu[2] + s 
        elif int(qu[1]) == 2:
            s = s + qu[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
flip = 0
for i in range(Q):
  j = input()
  if len(j) == 1:
    flip += 1
    continue
  else:
    if j[2] == '1' and flip % 2 == 0:
      S = j[4] + S
      continue
    elif j[2] == '2' and flip % 2 == 0:
      S = S + j[4]
      continue
    elif j[2] == '1' and flip % 2 ==1:
      S = S + j[4]
      continue
    elif j[2] == '2' and flip % 2 ==1:
      S = j[4] + S
      continue
if flip % 2 == 1:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
flip = 0
for i in range(Q):
  j = input()
  if len(j) == 1:
    flip += 1
    continue
  else:
    if j[2] == '1' and flip % 2 == 0:
      S = j[4] + S
    elif j[2] == '2' and flip % 2 == 0:
      S = S + j[4]
    elif j[2] == '1' and flip % 2 ==1:
      S = S + j[4]
    elif j[2] == '2' and flip % 2 ==1:
      S = j[4] + S
if flip % 2 == 1:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

reverse = False
for _ in range(q):
    query = input().split()
    if query[0] == '1':
        reverse = not reverse
    else:#2
        if query[1] == '1':
            if reverse:
                S = S + query[2]
            else:
                S = query[2] + S
        else:
            if reverse:
                S = query[2] + S
            else:
                S = S + query[2]
print(S[::-1] if reverse else S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
A=[list(input().split()) for i in range(Q)]
c=0
for i in range(Q):
    if A[i][0]=="1":
        c+=1
        continue
    if c%2==1 and A[i][0]=="2":
        if A[i][1]=="1":
            S=S+A[i][2]
        elif A[i][1]=="2":
            S=A[i][2]+S
    elif c%2==0 and A[i][0]=="2":
        if A[i][1]=="1":
            S=A[i][2]+S
        elif A[i][1]=="2":
            S=S+A[i][2]
if c%2==1:
    S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
def trans(s):
     return s[::-1]

def headto(s, c):
    return c+s

def endto(s, c):
    return s+c

for i in range(q):
    que = [i for i in input().split()]
    if que[0] == '1':
        s = trans(s)
    elif que[0] == '2':
        if que[1] == '1':
            s = headto(s,que[2])
        elif que[1] == '2':
            s = endto(s,que[2])
print(s)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
Flag=True
for i in range(Q):
    L=input().split()
    if L[0]=="1":
        Flag=not Flag
    else:
        if L[1]=="1" and Flag or L[1]=="2" and not Flag:
            S=L[2]+S
        else:
            S=S+L[2]
if not Flag:
    S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
for i 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=S+L[2]
print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
    tmp=input()
    if len(tmp)==1:
        s=s[::-1]
    else:
        if tmp[2]=="1":
            s=tmp[4]+s
        else:
            s=s+tmp[4]

print(s)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

S = input()
n = int(stdin.readline())
flipNumber = 0
for _ in range(n):
  li = list(map(str, stdin.readline().split()))
  if len(li) == 1: flipNumber += 1
  else:
    letter = li[2]
    if flipNumber%2 == 0:
      if int(li[1]) == 1: S = letter + S
      else: S = S + letter
    else:
      if int(li[1]) == 1: S = S + letter
      else: S = letter + S
  
if flipNumber%2 == 1: S = ''.join(list(reversed(S)))
print(S)

Submission status: Time Limit Exceeded

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

#flag=0 でleft, flag=1でrightが先頭
flag = 0
cnt = 0

for _ in range(q):
    lis = list(input().split())
    if lis[0] == "1":
        if flag:
            flag = 0
        else:
            flag = 1
        cnt += 1
    else:
        if lis[1] == '1':
            if flag:
                s = s + lis[2]
            else:
                s = lis[2] + s
        else:
            if flag:
                s = lis[2] + s
            else:
                s = s + lis[2]

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


print(s)

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
l = []
r = []
flg = False
for i in range(q):
    try:
        t,f,c = input().split()
        if flg:
            if f == '1':
                r.append(c)
            else:
                l.insert(0,c)
        else:
            if f == '1':
                l.insert(0,c)
            else:
                r.append(c)
    except ValueError:
        if flg:
            flg = False
        else:
            flg = True
if flg:
    print((''.join(r[::-1]))+(''.join(s[::-1]))+(''.join(l[::-1])))
else:
    print((''.join(l))+(''.join(s))+(''.join(r)))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
flg = False
for i in range(q):
    try:
        t,f,c = input().split()
        if flg:
            if f == '1':
                s.append(c)
            else:
                s.insert(0,c)
        else:
            if f == '1':
                s.insert(0,c)
            else:
                s.append(c)
    except ValueError:
        if flg:
            flg = False
        else:
            flg = True
if flg:
    print(''.join(s[::-1]))
else:
    print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
def solve():
    S = input()
    s = [c for c in S]
    Q = int(input())
    for i in range(Q):
        T = input().split()
        if T[0] == '1':
            for j in range(len(s)//2):
                tmp = s[j]
                s[j] = s[-1-j]
                s[-1-j] = tmp
        else:
            ext = [c for c in T[2]]
            if T[1] == '1':
                s = ext + s
            else:
                s = s + ext
    print(''.join(s))

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
def solve():
    S = input()
    Q = int(input())
    for i in range(Q):
        T = 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)

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
import itertools


def resolve(in_):
    one = ord(b'1')
    two = ord(b'2')
    # print(f'{one=} {two=}')

    S = in_.readline().strip()
    q = collections.deque(S)
    # print(f'{S=} {q=}')

    reverse_flag = 0
    Q = int(in_.readline())
    for query in itertools.islice(in_, Q):
        T = query[0]
        if T == one:
            reverse_flag ^= 1 
            # q.reverse()
        elif T == two:
            if reverse_flag:
                q.reverse()
                reverse_flag = 0
            F = query[2]
            C = query[4]
            if F == one:
                q.appendleft(C)
            elif F == two:
                q.append(C)
    else:
        if reverse_flag:
            q.reverse()
            reverse_flag = 0

    return bytes(q)


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer.decode('ascii'))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
import itertools


def resolve(in_):
    one = ord(b'1')
    two = ord(b'2')
    # print(f'{one=} {two=}')

    S = in_.readline().strip()
    q = collections.deque(S)
    # print(f'{S=} {q=}')

    Q = int(in_.readline())
    for query in itertools.islice(in_, Q):
        T = query[0]
        if T == one and len(q) > 1:
            q.reverse()
        elif T == two:
            F = query[2]
            C = query[4]
            if F == one:
                q.appendleft(C)
            elif F == two:
                q.append(C)

    return bytes(q)


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer.decode('ascii'))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	D_fix_2
# CreatedDate:  2020-08-09 01:27:04 +0900
# LastModified: 2020-08-09 01:48:19 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    s = input()
    q = int(input())
    for _ in range(q):
        query = list(map(str, 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)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:

s = input()
n = int(input())

hanten=0
for i in range(n):
    Q = input()
    if int(Q[0])==1:
        hanten+=1 
    elif int(Q[0])==2 :
        if hanten%2 == int(Q[2])-1 :
            s= Q[4]+s
        else:
            s= s+Q[4]

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
r = False
for _ in range(q):
  d = list(input().split())
  if len(d)==1:
    r = ~r
  else:
    if (d[1]=='1' and ~r) or (d[1]=='2' and r):
      s = d[2] + s
    else:
      s += d[2]
if r:
  s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
cou = 1
frt = ""
bck = ""
for i in range(q):
    t = list(input().split())
    #logging.debug(t)#
    if t[0] == "1":
        cou *= -1
    else:
        if (t[1] == "1" and cou == 1)or(t[1] == "2" and cou == -1):
            s = t[2] + s
        else:
            s = s + t[2]
#    logging.debug("s {},cou {}".format(s,cou))

#s = frt + s + bck
#logging.debug(s)#
if cou == 1:
    print(''.join(s))
else:
    print(''.join(s[::-1]))

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
front=True
for _ in range(n):
    q=input()
    if q=="1":front=not front;continue
    l=q.split()
    if l[1]=="1":
        if front:s=l[2]+s
        else:s=s+l[2]
    if l[1]=="2":
        if front:s=s+l[2]
        else:s=l[2]+s

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
cou = 1
for i in range(q):
    t = list(input().split())
    #logging.debug(t)#
    if t[0] == "1":
        cou *= -1
    else:
        if t[1] == "1" and cou == 1:
            s = t[2] + s
        elif t[1] == "2" and cou == -1:
            s = t[2] + s
        else:
            s = s + t[2]
#    logging.debug("s {},cou {}".format(s,cou))

if cou == 1:
    print(''.join(s))
else:
    print(''.join(s[::-1]))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
flip = 1
right_edge = s
left_edge = ''
for i in range(q):
    query = input()
    if query[0] == '1':
        flip *= -1
    else:
        if query[2] == '1':
            if flip == 1:
                left_edge += query[-1]
            else:
                right_edge += query[-1]
        else:
            if flip == 1:
                right_edge += query[-1]
            else:
                left_edge += query[-1]
left_edge = left_edge[::-1]
if flip == 1:
    print(left_edge + right_edge)
else:
    print(right_edge[::-1] + left_edge[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
flip = 1
right_edge = s
left_edge = ''
for i in range(q):
    query = input()
    if query[0] == '1':
        flip *= -1
    else:
        if query[2] == '1':
            if flip == 1:
                left_edge = query[-1] + left_edge
            else:
                right_edge += query[-1]
        else:
            if flip == 1:
                right_edge += query[-1]
            else:
                left_edge = query[-1] + left_edge
if flip == 1:
    print(left_edge + right_edge)
else:
    print(right_edge[::-1] + left_edge[::-1])

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    flag = False
    for _ in range(Q):
        A = tuple(map(str, input().split()))
        if A[0] == "1":
            flag = not(flag)
        else:
            if (A[1] == "1" and not(flag)) or (A[1] == "2" and flag):
                S = A[2] + S
            else:
                S = S + A[2]
    if flag:
        print(S[::-1])
    else:
        print(S)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = list(str(input()))
N = int(input())
for i in range(N):
    CL = list(map(str,input().split()))
    if int(CL[0]) == 1:
        S = S[::-1]
    elif int(CL[1]) == 1:
        S.insert(0,CL[2])
    else:
        S.append(CL[2])
print(''.join(S))

Submission status: Time Limit Exceeded

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