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():

    S = input()
    Q = int(input())
    count = 0
    left = ""
    right = ""
    for _ in range(Q):
        U = input()
        if len(U) == 1:
            count += 1
            left, right = right[::-1], left[::-1]
        else:
            _, F, C = U.split()
            F = int(F)
            if F == 1:
                left = C + left
            else:
                right = right + C

    if count % 2 == 1:
        S = S[::-1]
    return left + S + right

if __name__ == '__main__':
    print(main())

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:##T_i=1
        S.reverse()
    else:#T_i == 2
        if a[1] == "1":
            S.insert(0,a[2])
        else:
            S.append(a[2])

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def inputs(): return [int(x) for x in input().split()]
s = deque(input())
q = int(input())
for i in range(q):
    q1 = input()
    if len(q1)==1:
        s = list(s)
        s = deque(s[::-1])
    else:
        l,f,c = q1.split()
        if int(f)==1:
            s.appendleft(c)
        else:
            s.append(c)
print("".join(list(s)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
p=0
for i in range(int(input())):
    a=list(map(str,input().split()))
    if len(a)==1:
        p+=1
    else:
        if p%2:
            if a[1]=="2":
                s.insert(0,a[2])
            else:
                s.append(a[2])
        else:
            if a[1]=="1":
                s.insert(0,a[2])
            else:
                s.append(a[2])
ans=""
if p%2==1:
    for i in s[::-1]:
        ans+=i
else:
    for i in s:
        ans+=i
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
cnt = 0
pre = ''
end = ''
for i in range(q):
    t, *fc = input().split()
    if t == '1':
        cnt^=1
    else:
        f, c = fc
        f = int(f)%2
        if f^cnt:
            pre+=c
        else:
            end+=c
ans = pre[::-1]+s+end
print
if cnt:
    ans = ans[::-1]
print(ans)

Submission status: Time Limit Exceeded

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

for _ in range(q):
  query = input()
  if query[0] == "1":
    s = s[::-1]
  else:
    ti, fi, ci = query.split()
    if fi == "1":
      s = ci + s  
    else:
      s = s + ci

print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
sign=1
for i in range(q):
  Q=[str(i) for i in input().split()]
  if Q[0]=="1":
    sign*=-1
  else:
    f,c=Q[1:]
    if f=="1" and sign==1:
      s=c+s
    elif f=="2" and sign==-1:
      s=c+s
    else:
      s=s+c
print(s if sign==1 else s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
f = True
q = int(input())
for i in range(q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        if f:
            f = False
        else:
            f = True
    else:
        f_i = int(q[1])
        if f_i == 1:
            if f:
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if f:
                s = s + q[2]
            else:
                s = q[2] + s
if f:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

pre = ""
suf = "" 
rev = 0
for i in range(Q):
    q = input().split()
    if len(q) == 1:
        rev = (rev + 1) % 2
    else:
        _, f, c = q
        if f == '1':
            if rev == 0:
                pre = c + pre
            else:
                suf = suf + c
        else:
            if rev == 0:
                suf = suf + c
            else:
                pre = c + pre

ans = pre + S + suf
if rev == 0:
    print(ans)
else:
    print(ans[::-1])

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=list(input())
q=int(input())
for _ in range(q):
  t=list(input().split())
  if len(t)==1:
    s=s[::-1]
  else:
    if int(t[1])==1:
      s=[t[2]]+s
    else:
      s=s+[t[2]]
print(''.join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
for i in range(q):
    qvery = input()
    if len(qvery) == 1: #t == 1
        s = list(reversed(s))
    else:
        qq, f, c = qvery.split(' ')
        f = int(f)
        if f == 1:
            s.insert(0, c)
        else:
            s.append(c)
for i in range(len(s)):
    print(s[i], end='')
print('')

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
def mi():return map(int,input().split())
def li():return list(mi())

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
s=input().rstrip()
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:
				s=c+s
			else:
				s+=c
		else:
			if rev%2==0:
				s+=c
			else:
				s=c+s
if rev%2==0:
	print(s)
else:
	print(s[::-1])

Submission status: Time Limit Exceeded

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

rev = 0
for _ in range(q):
    que = input()
    if que[0] == "1":
        rev ^= 1

    else:
        t, f, c = que.split()
        if rev:
            if f == "1":
                s = s + c
            else:
                s = c + s

        else:
            if f == "1":
                s = c + s
            else:
                s = s + c

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for _ in range(q):
    query=list(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):
    t=input()
    if t=='1':
        S=S[::-1]
    else:
        _,F,C=t.split(" ")
        if F=='1':
            S=C+S
        else:
            S=S+C

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
sl = list(input())
q = int(input())
dl = deque(sl)
for _ in range(q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        dl.reverse()
    else:
        if query[1] == "1":
            dl.appendleft(query[2])
        else:
            dl.append(query[2])
print("".join(dl))

Submission status: Time Limit Exceeded

Solution code:
a = list(input())
for i in range(int(input())):
  b = input().split()
  if b[0] == "1":
    a = a[::-1]
  else:
    if b[1] == "1":
      a.insert(0,b[2])
    else:
      a.append(b[2])
print("".join(a))

Submission status: Time Limit Exceeded

Solution code:
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = readline().decode().rstrip()
q = int(readline())

for _ in range(q):
    que = readline().decode().split()
    if len(que) == 1:
        S = S[::-1]
    else:
        if que[1] == '1':
            S = que[2] + S
        else:
            S += que[2]

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def inputs(): return [int(x) for x in input().split()]
s = deque(input())
q = int(input())
for i in range(q):
    q1 = input()
    if len(q1)==1:
        s = deque(reversed(s))
    else:
        l,f,c = q1.split()
        if int(f)==1:
            s.appendleft(c)
        else:
            s.append(c)
print("".join(list(s)))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
r = 0
ans = s
for i in range(q):
    d = input().split()
    if len(d)==1:
        r += 1
    else:
        f = int(d[1])
        c = d[2]
        if (f+r)%2 == 1:
            ans = c + ans
        else:
            ans = ans + c

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

Submission status: Time Limit Exceeded

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

now_reverse = False

for _ in range(n):
  query = input()

  if query == '1':
    now_reverse = not now_reverse
    continue

  
  _,f,c = query.split()
  if f == '1' and now_reverse:
    s = s + c
  elif f == '1' and not now_reverse:
    s = c + s
  elif f == '2' and now_reverse:
    s = c + s
  else:
    s = s + c
  
if now_reverse:
  s = reversed(s)

for c in s:
  print(c,end='')
print()

Submission status: Time Limit Exceeded

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

cur=True
for i in range(Q):
  q=list(input().split())
  if q[0]=="1":
    cur^=True
  elif q[0]=="2":
    f,c=q[1],q[2]
    if f=="1":
      if cur:
        S=c+S
      else:
        S=S+c
    elif f=="2":
      if cur:
        S=S+c
      else:
        S=c+S
if not cur:
  S=S[::-1]
  
print(S)

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    s = input()
    t = deque()
    for i in s:
        t.append(i)
    q = I()
    k = 0
    for _ in range(q):
        qu = input().split()
        if qu[0] == "1":
            k ^= 1
        else:
            f,c = qu[1:]
            key = (int(f)-1)^k
            if not key:
                t.appendleft(c)
            else:
                t.append(c)
    ans = ""
    if not k:
        while t:
            ans += t.popleft()
    else:
        while t:
            ans += t.pop()
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
	return sys.stdin.readline()[:-1]

s = input()
n = len(s)
q = int(input())
front = ""
back = ""
sgn = 0
for _ in range(q):
	query = input().split()
	if query[0] == "1":
		sgn ^= 1
	else:
		f, c = int(query[1])-1, query[2]
		if f ^ sgn == 0:
			front += c
		else:
			back += c
if not sgn:
	print(front[::-1] + s + back)
else:
	print(back[::-1] + s[::-1] + front)

Submission status: Accepted

Solution code:
S = str(input())
Q = int(input())
Qvery = [0]*Q
for i in range(Q):
    Qvery[i] = list(map(str,input().split()))
frontback = 1
tail = ''
head = ''
for i in range(Q):
    if Qvery[i][0]=='1':
        frontback *= -1
        
    else:
        if Qvery[i][1]=='2':
            if frontback == 1:
                tail +=  str(Qvery[i][2])
            else:
                head = str(Qvery[i][2]) + head
        else:
            if frontback == 1:
                head = str(Qvery[i][2])+head
            else:
                tail += str(Qvery[i][2])
S = head + S + tail
if frontback == -1:
    S = S[::-1]
    
print(S)

Submission status: Accepted

Solution code:
from collections import deque

poe=deque([])
S=input()
for i in range(len(S)):
    poe.append(S[i])
Q=int(input())
P=[]
cnt=0
for i in range(Q):
    a=list(input().split())
    P.append(a)

for i in range(Q):
    if len(P[i])==1:
        cnt+=1
    else:
        P[i][1]=int(P[i][1])+cnt
        if P[i][1]%2==1:
            poe.appendleft(P[i][2])
        else:
            poe.append(P[i][2])

poe=list(poe)
if cnt%2==1:
    poe=poe[::-1]
print(''.join(poe))

Submission status: Accepted

Solution code:
# author:  Taichicchi
# created: 21.09.2020 20:00:43

import sys

S = input()

Q = int(input())

rev = 0

front = []
end = []

for q in range(Q):
    query = input().split()
    if query[0] == "1":
        rev = 1 - rev
    else:
        if ((rev == 0) & (query[1] == "1")) | ((rev == 1) & (query[1] == "2")):
            front.append(query[2])
        else:
            end.append(query[2])

front = "".join(front)
end = "".join(end)


if rev:
    print(end[::-1] + S[::-1] + front)
else:
    print(front[::-1] + S + end)

Submission status: Accepted

Solution code:
S = input()
top = 0
end = len(S)-1
toright = True
N = int(input())
Q = [S[i] if i <= end else "" for i in range(end+1+N)]

for i in range(N):
  q = input().split()
  if q[0]=="1":
    toright = not toright
  else:
    if not(toright ^ (q[1]=="1")):
      Q[top-1]=q[2]
      top -= 1
    else:
      Q[end+1]=q[2]
      end += 1
ans = ""
#print(Q)
#print(toright)
for i in range(top, end+1):
  ans += Q[i]
if not toright:
  ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())

s=input()
q=int(input())
from collections import deque
ar=[]
for i in s:
    ar.append(i)
ar=deque(ar)
r=0
for i in range(q):
    t=[i for i in input().strip().split(" ")]
    if t[0]=="1":
        r=1-r
    else:
        if r==0:
            if t[1]=="1":
                ar.appendleft(t[2])
            else:
                ar.append(t[2])
        else:
            if t[1]=="1":
                ar.append(t[2])
            else:
                ar.appendleft(t[2])
if r==0:
    print("".join(ar))
else:
    print("".join(ar)[::-1])

Submission status: Accepted

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

lS = ''
for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        lS, S = S, lS
    else:
        if query[1] == '1':
            lS += query[2]
        else:
            S += query[2]

print(lS[::-1] + S)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
s = str(input())
q = int(input())
stat = 0
l = []
a = deque()
b = deque()
for i in range(q):
    l = list(map(str, input().split()))
    t = int(l[0])
    if t == 1:
        stat += 1
    else:
        f = int(l[1])
        c = l[2]
        if stat % 2 == 0:
            if f == 1:
                a.appendleft(c)
            else:
                b.append(c)
        else:
            if f == 1:
                b.append(c)
            else:
                a.appendleft(c)

#print(a, b, stat % 2)
ans = ''.join(a)+s+''.join(b)
if stat % 2 == 1:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
S = input()
N = int(input())
flg = 0
L = []
for i in range(N):
    e = input().split()
    if e[0] == '1':
        flg += 1
        flg = flg%2
    else:
        L.append(((flg+int(e[1]))%2, e[2]))

S = ''.join(list(reversed(S))) if flg else S
s1, s2 = '', ''
for f, v in L:
    if (flg+f)%2 == 1:
        s1 += v
    else:
        s2 += v

ans = ''.join(list(reversed(s1))) + S + s2
print(ans)

Submission status: Accepted

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

head = []
tail = []

for i in range(Q):
  line = input()
  if line == '1':
    rev *= -1
  else:
    line = line.split()
    F = int(line[1])
    C = line[2]
    if rev * F in [1, -2]:
      head.append(C)
    else:
      tail.append(C)

head = head[::-1]
S = ''.join(head + [S] + tail)

if rev == -1:
  S = S[::-1]
  
print(S)

Submission status: Accepted

Solution code:


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
from collections import deque


def main():
    mod=10**9+7
    S=input()
    
    from collections import deque
    dq=deque()
    
    for c in S:
        dq.appendleft(c)
        
    rev=0
    Q=I()
    for _ in range(Q):
        q=input().split()
        if q[0]=="1":
            rev=(rev+1)%2
        else:
            f=int(q[1])
            c=q[2]
            
            if (rev+f)%2==1:
                dq.append(c)
            else:
                dq.appendleft(c)
                
    ans=[]
    if rev:
        while dq:
            ans.append(dq.popleft())
    else:
        while dq:
            ans.append(dq.pop())
            
    print(''.join(map(str, ans)))
        

        

main()

Submission status: Accepted

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

#import
#import math
#import numpy as np

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

inv = False

fro = ""
end = ""

for _ in range(q):
    query = list(map(str, input().split()))

    if query[0] == "1":
        inv = not inv

    else:
        t = int(query[1])
        instr = query[2]
        if not inv:
            if t == 1:
                fro = instr + fro
            else:
                end = end + instr

        else:
            if t == 1:
                end = end + instr[::-1]
            else:
                fro = instr[::-1] + fro

ans = fro + s + end

if not inv:
    print(ans)

else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
import sys
S = input()
A = ''
input()
key = 1

for i in sys.stdin:
    if i[0] == '0':
        break

    if i[0] == '1':
        key = -key
    
    elif key == 1:
        if i[2] == '1':
            A = A + i[4]
        else:
            S = S + i[4]

    else:
        if i[2] == '2':
            A = A + i[4]
        else:
            S = S + i[4]

ans = A[::-1] + S
print(ans[::key])

Submission status: Accepted

Solution code:
import sys
S = input()
Q = int(input())
key = 1

for i in sys.stdin:
    if i[0] == '0':
        break

    if i[0] == '1':
        key = -key
    
    elif key == 1:
        if i[2] == '1':
            S = i[4] + S
        else:
            S = S + i[4]

    else:
        if i[2] == '2':
            S = i[4] + S
        else:
            S = S + i[4]


print(S[::key])

Submission status: Accepted

Solution code:
from collections import deque
d=deque(input())
q=int(input())
ans=1
for i in range(q):
    a=input()
    if int(a[0])==1:
        ans=ans*(-1)
    else:
        if ans==1:
            if int(a[2])==1:
                d.appendleft(a[4])
            else:
                d.append(a[4])
        else:
            if int(a[2])==1:
                d.append(a[4])
            else:
                d.appendleft(a[4])
if ans==1:
    for i in range(len(d)):
        print(d[i],end="")
else:
    d.reverse()
    for i in range(len(d)):
        print(d[i],end="")

Submission status: Accepted

Solution code:
s = input()
q = int(input())
flag = 1
f = ''
b = ''
for i in range(q):
  query = list(map(str, input().split()))
  if query[0] == '1':
    flag *= -1
  else:
    if flag == 1:
      if query[1] == '1':
        f += query[2]
      else:
        b += query[2]
    else:
      if query[1] == '1':
        b += query[2]
      else:
        f += query[2]
ans = f[::-1] + s + b
if flag == -1:
  ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
forward = 1
f = ''
b = ''
for i in range(Q):
    if query[i][0] == '1':
        forward *= -1
    else:
        if query[i][1] == '1':
            if forward == 1:
                f += query[i][2]
            else:
                b += query[i][2] 
        else:
            if forward == 1:
                b += query[i][2] 
            else:
                f += query[i][2]
S = ''.join(list(reversed(f))) + S + b

if forward == -1:
    ans = ''.join(list(reversed(S)))
else:
    ans = S
print(ans)

Submission status: Accepted

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

for q in range(Q):
  que = input().split()
  if que[0]=="1":
        S,T = T,S
  else:
    if que[1]=="1":
      T+=que[2]
    else:
      S+=que[2]

print(T[::-1]+S)

Submission status: Accepted

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

    reverse = 0
    head = ""
    tail = ""

    for i in range(q):
        v = list(input().split())
        if int(v[0]) == 1:
            reverse += 1
        elif reverse % 2 == 0:
            if int(v[1]) == 1:
                head += v[2]
            else:
                tail += v[2]
        elif reverse % 2 == 1:
            if int(v[1]) == 1:
                tail += v[2]
            else:
                head += v[2]

    s = head[::-1] + s + tail
    if reverse % 2 == 1:
        s = s[::-1]

    print(s)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def abc158d_string_formation():
    from collections import deque
    s = deque(list(input()))
    q = int(input())
    flip = 0
    for _ in range(q):
        query = list(input().split())
        if query[0] == '1':
            flip = 1-flip
        elif query[1] == '1':
            if flip == 0:
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if flip == 0:
                s.append(query[2])
            else:
                s.appendleft(query[2])
    if flip == 1:
        s = ''.join(reversed(s))
    else:
        s = ''.join(s)
    print(s)
abc158d_string_formation()

Submission status: Accepted

Solution code:
def main():
    s = str(input())
    q = int(input())
    lst = [list(map(str, input().split())) for _ in range(q)]
    switch = 0  # 0が通常　1が前

    str_lst = [s]
    front_lst = []
    for i in range(q):

        if lst[i][0] == '1':
            switch = 1 - switch

        else:
            f = lst[i][1]
            c = lst[i][2]

            if f == '1':  # 先頭に追加

                if switch == 0:
                    front_lst.append(c)

                else:
                    str_lst.append(c)

            else:

                if switch == 0:
                    str_lst.append(c)

                else:
                    front_lst.append(c)

    front = ''.join(front_lst)
    front = front[::-1]
    after = ''.join(str_lst)
    answer = front + after

    if switch == 1:
        answer = answer[::-1]

    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
    S = input().rstrip()
    Q = int(input())
    reverse_flag = False
    head = ''
    tail = ''
    for _ in range(Q):
        q = input().rstrip()
        if q == '1':
            reverse_flag = not reverse_flag
        else:
            _, F, C = q.split()
            if (F == '1' and reverse_flag is False) or (F == '2' and reverse_flag is True):
                head += C
            else:
                tail += C
    if reverse_flag is True:
        print(tail[::-1] + S[::-1] + head)
    else:
        print(head[::-1] + S + tail)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
head = ''
tail = ''
rev = 0

for _ in range(q):
    T = list(input().split())
    if T[0] == '1':
        head, tail = tail, head
        rev += 1
    else:
        if T[1] == '1':
            head += T[2]
        else:
            tail += T[2]

if rev%2 == 0:
    print(head[::-1]+s+tail)
else:
    print(head[::-1]+s[::-1]+tail)

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    S = input()
    Q = int(input())
    rev = 0  # 1のとき反転
    que = deque((s for s in S))
    for _ in range(Q):
        Cmd = [i for i in input().split()]
        if Cmd[0] == "1":
            rev ^= 1
        else:
            if rev == 0:
                if Cmd[1] == "1":
                    # 先頭に追加
                    que.appendleft(Cmd[2])
                else:
                    # 末尾に追加
                    que.append(Cmd[2])
            else:
                if Cmd[1] == "1":
                    # 先頭に追加
                    que.append(Cmd[2])
                else:
                    # 末尾に追加
                    que.appendleft(Cmd[2])
    ans = "".join(que)
    if rev == 1:
        ans = ans[::-1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
def main():
    s = list(input())
    q = int(input())
    query = []
    que = deque(s)
    f = False
    for i in range(q):
        query.append(input().split(" "))
    for i in range(q):
        if query[i][0] == '1':
            f = not f
        else:
            if f:
                if query[i][1] == '2':
                    que.appendleft(query[i][2])
                else:
                    que.append(query[i][2])
            else:
                if query[i][1] == '1':
                    que.appendleft(query[i][2])
                else:
                    que.append(query[i][2])
    if f:
        que.reverse()
    print("".join(que))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

s = list(input())
q = int(input())
t = [list(map(str,input().split())) for _ in range(q)]

d = deque(s)

cnt = 0
for i in range(q):
  if len(t[i]) == 1:
    cnt += 1
  else:
    if (int(t[i][1])+cnt) % 2 != 0:
      d.appendleft(t[i][2])
    else:
      d.append(t[i][2])
      
if cnt%2 != 0:
  d = list(d)[::-1]
print(''.join(d))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
bs = ''
fs = ''
hanten = 0
for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        hanten += 1
    else:
        if query[1] == '1':
            if hanten % 2 == 0:
                #fs += query[2]
                fs = ''.join([fs,query[2]])
            else:
                #bs += query[2]
                bs = ''.join([bs, query[2]])
        else:
            if hanten % 2 == 0:
                #bs += query[2]
                bs = ''.join([bs, query[2]])
            else:
                #fs += query[2]
                fs = ''.join([fs, query[2]])
if hanten % 2 == 0:
    #print(fs[::-1]+S+bs)
    print(''.join([fs[::-1], S, bs]))
else:
    #print(bs[::-1]+S[::-1]+fs)
    print(''.join([bs[::-1], S[::-1], fs]))