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: Accepted

Solution code:
import sys
import collections


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    s = str(input().rstrip('\n'))
    s = collections.deque(s)
    q = int(input().rstrip('\n'))
    r = False
    for _ in range(q):
        tfc = list(map(str, str(input().rstrip('\n')).split()))
        if tfc[0] == "1":
            r = not r
        else:
            if not r:
                if tfc[1] == "1":
                    s.appendleft(tfc[2])
                else:
                    s.append(tfc[2])
            else:
                if tfc[1] == "1":
                    s.append(tfc[2])
                else:
                    s.appendleft(tfc[2])
    print("".join(list(s)) if not r else "".join(reversed(s)))


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
s=str(input())

N=int(input())
cnt=0
f=''
b=''
for i in range(N):
  inp=str(input())
  if len(inp)==1:
    cnt+=1
  else:
    F,c=int(inp[2]),inp[4]
    if F==1:
      if cnt%2==0:
        f=c+f
      else:
        b+=c
    else:
      if cnt%2==0:
        b+=c
      else:
        f=c+f
if cnt%2==0:
  print(f+s+b)
else:
  ans=f+s+b
  L=len(ans)
  if L==1:
    print(ans)
  else:
    if L%2==1:
      f=ans[0:L//2]
      s=ans[L//2]
      b=ans[L//2+1:L]
      f_=''.join(list(reversed(b)))
      b_=''.join(list(reversed(f)))
      print(f_+s+b_)
    else:
      f=ans[0:L//2]
      b=ans[L//2:L]
      f_=''.join(list(reversed(b)))
      b_=''.join(list(reversed(f)))
      print(f_+b_)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
Q = [list(map(str, input().split())) for _ in range(q)]

t = ''
for qi in Q:
  if int(qi[0]) == 1:
    s, t = t, s
  else:
    if int(qi[1])  == 1:
      t += qi[2]
    else:
      s += qi[2]
      
t = t[::-1]
t += s
print(t)

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())

text = deque(s)
readleft = True


for _ in range(q):
  query = input().rstrip().split()
  if query[0] == "1":
    readleft = not(readleft)
  else:
    addtop = 2 - int(query[1])
    addleft = (addtop == readleft)
    if addleft:
      text.appendleft(query[2])
    else:
      text.append(query[2])

if readleft:
  while text != deque():
    print(text.popleft(), end="")
  print()
else:
  while text != deque():
    print(text.pop(), end="")
  print()

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
ARR = []

for i in range(Q):
    arr = input().split(" ")
    ARR.append(arr)


def calculate(s,q,arr):
    s = deque(list(s))

    kk = 0
    for ar in arr:
        if int(ar[0]) == 1:
            kk = kk + 1
        if int(ar[0]) == 2:
            if int(ar[1]) == 1:
                if kk % 2 == 0:
                    s.appendleft(ar[2])
                else:
                    s.append(ar[2])
            else:
                if kk % 2 == 0:
                    s.append(ar[2])
                else:
                    s.appendleft(ar[2])

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

    print("".join(list(s)))

calculate(S,Q,ARR)

Submission status: Accepted

Solution code:
#AC出てから！ファイル整理！
#コンテストはatcoder.pyで！
#A:
#B:
#C:
#D:
#DP
#ナップザックの残り
#56と復習

import sys
#input = sys.stdin.readline
sys.setrecursionlimit(10000000)
from collections import Counter, deque
#from collections import defaultdict
from itertools import combinations, permutations
#from itertools import accumulate, product
from bisect import bisect_left,bisect_right
from math import floor, ceil
#from operator import itemgetter

#mod = 1000000007

def main():
    from sys import stdin
    #input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

    S = list(stdin.readline().rstrip())
    Q = int(input())
    mae = []
    usiro =[]
    s = True
    for _ in range(Q):
        q = stdin.readline()
        if q[0]=='1':
            s = not s
        else:
            if q[2]=='1' and s:
                mae.append(q[4])
            elif q[2]=='2' and s:
                usiro.append(q[4])
            elif q[2]=='2' and not s:
                mae.append(q[4])
            else:
                usiro.append(q[4])
    if s:
        print(''.join(mae[::-1]+S+usiro))
    else:
        print(''.join(usiro[::-1]+S[::-1]+mae))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    from sys import stdin
    #input = sys.stdin.readline
    #sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

    S = input()
    Q = int(input())
    mae = ''
    usiro = ''
    s = True
    for _ in range(Q):
        q = stdin.readline()
        if q[0]=='1':
            s = not s
        else:
            if q[2]=='1' and s:
                mae = q[4] + mae
            elif q[2]=='2' and s:
                usiro = usiro + q[4] 
            elif q[2]=='2' and not s:
                mae = q[4] + mae
            else:
                usiro = usiro + q[4]
    if s:
        print(mae+S+usiro)
    else:
        print(usiro[::-1]+S[::-1]+mae[::-1])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
n = int(input())
f = 0
for i in range(n):
  l = list(map(str, input().split()))
  if l[0] == "2":
    if (l[1] == "1" and f == 0) or (l[1] == "2" and f == 1):
      s.appendleft(l[2])
    else:
      s.append(l[2])
  else:
    if f == 0:
      f = 1
    else:
      f = 0
if f == 1:
  s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
#s = input()
n = int(input())
f = 1
for i in range(n):
  l = list(map(str, input().split()))
  if l[0] == "2":
    tmp = int(l[1])*f
    if tmp == 1 or tmp == -2:
      s.appendleft(l[2])
    else:
      s.append(l[2])
  else:
    f *= -1

if f == -1:
  s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
Q = [list(map(str, input().split())) for _ in range(q)]

t = ''
for qi in Q:
  if int(qi[0]) == 1:
    s, t = t, s
  else:
    if int(qi[1])  == 1:
      t += qi[2]
    else:
      s += qi[2]
      
t = t[::-1]
t += s
print(t)

Submission status: Accepted

Solution code:
class Node:
    def __init__(self, v):
        self.v = v
        self.p = None
        self.n = None

class LinkedList:
    def __init__(self, v):
        self.head = Node(v)
        self.tail = self.head
        self.d = 1

    def reverse(self):
        self.d = self.d * -1

    def insertTail(self, v):
        node = Node(v)
        if self.d == 1:
            self.tail.n = node
            node.p = self.tail
            self.tail = node
        else:
            self.head.p = node
            node.n = self.head
            self.head = node

    def insertHead(self, v):
        self.reverse()
        self.insertTail(v)
        self.reverse()

    def toString(self):
        res = ""
        if self.d == 1:
            pos = self.head
            while pos != None:
                res = res + pos.v
                pos = pos.n
        else:
            pos = self.tail
            while pos != None:
                res = res + pos.v
                pos = pos.p
        return res

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

l = LinkedList(S[0])
for i in range(1,len(S)):
    l.insertTail(S[i])

for _ in range(Q):
    query = input()
    if query[0] == "1":
        l.reverse()
    else:
        T,F,C = query.split()
        if F=="1":
            l.insertHead(C)
        else:
            l.insertTail(C)
print(l.toString())

Submission status: Accepted

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

from collections import deque
s = deque(list(s))

count = 0
for i in range(q):
    t = int(Q[i][0])
    if t==1:
        count += 1
    else:
        f = int(Q[i][1])
        new = Q[i][2]
        if count%2==0 and f==1:
            s.appendleft(new)
        elif count%2==0 and f==2:
            s.append(new)
        elif count%2==1 and f==1:
            s.append(new)
        elif count%2==1 and f==2:
            s.appendleft(new)
        else:
            pass

# print(count)

if count%2==0:
    print("".join(s))
    exit()
else:
    s = list(s)[::-1]
    print("".join(s))
    exit()

Submission status: Accepted

Solution code:
import sys
from collections import deque 
d=deque(input())
#print(d)
q=int(input())
x=0 #0 means normal 1 means reverse 
for _ in range(q):
  ss=list(input().split())
  if ss[0]=='1':
    x=1-x
  else:
    f=int(ss[1])
    c=ss[2]
    if x==0:#normal
      if f==1:
        d.appendleft(c)
      else:
        d.append(c)
    else:#reverse
      if f==1:
        d.append(c)
      else:
        d.appendleft(c)
if x==0:
  print(''.join(d))
else:
  ans=[]
  for i in d:
    ans+=i
  print(''.join(ans[::-1]))

Submission status: Accepted

Solution code:
from collections import deque
s = input()
rev = s[::-1]
curs = [deque(list(s)), deque(list(rev))]
f = 0
b = 1
q = int(input())
for _ in range(q):
    stuff = input().split()
    if stuff[0] == '1':
        f, b = b, f
    else:
        op, fi, c = stuff
        fi = int(fi)
        if fi == 1:
            curs[f].appendleft(c)
            curs[b].append(c)
        else:
            curs[f].append(c)
            curs[b].appendleft(c)
print(''.join(curs[f]))

Submission status: Accepted

Solution code:
S=input()
Q=input()
#rev -1:False 0:True
L=''
R=''
rev=-1
for i in range(int(Q)):
  tmp=input().split()
  if tmp[0]=='1':
    rev = abs(rev)-1
  else:
    if rev+int(tmp[1])==1:
      R = R + tmp[2]
    else:
      L = L + tmp[2]
if rev==-1:
  print(L[::-1]+S+R)
else:
  print(R[::-1]+S[::-1]+L)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
c=1
u=''
for i in range(q):
    inp=input()
    if inp[0]=="1":
        c*=-1
    else:
        t,f,p=inp.split()
        if (f=="1" and c!=1) or (f=="2" and c==1):
            s+=p
        else:
        	u+=p
if c!=1:
    print("".join(reversed(s))+u)
else:
    print("".join(reversed(u))+"".join(s))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())

flag = 1
for i in range(q):
  st = input().split()
  if int(st[0]) == 1:
    flag *= -1
  else:
    tmp = int(st[1])*flag
    if tmp == 1 or tmp == -2:
      s.appendleft(st[2])
    else:
      s.append(st[2])

if flag == -1:
  s.reverse()
print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
n = int(input())
q = deque()
for _ in range(n):
    q.append(list(input().split()))
d = 0
for r in q:
    if r[0] == '1':
        d = d ^ 1
    else:
        if int(r[1]) & (d+1):
            s.appendleft(r[2])
        else:
            s.append(r[2])
if d == 1:
    s.reverse()
ans = "".join(list(s))
print(ans)

Submission status: Accepted

Solution code:
S=input()
q=int(input())
rev=False
pre=''

for i in range(q):
    inp=input()
    if inp[0]=="1":
        rev=not rev
    else:
        t,f,c=inp.split()
        if (f=='1' and rev) or (f=='2' and not rev):
            S+=c
        else:
            pre+=c
if not rev:
    print(''.join(list(reversed(pre)))+S)
else:
    print(''.join(list(reversed(S)))+pre)

Submission status: Accepted

Solution code:
from collections import deque

S = input()
dq = deque(S)

Q = int(input())
inverse = False
for _ in range(Q):
    query = list(input().split())
    if query[0] == '1':
        inverse = not inverse
    else:
        tmp = (int(query[1]) - 1) ^ inverse
        if tmp: dq.append(query[2])
        else: dq.appendleft(query[2])

if inverse: dq.reverse()
for c in dq:
    print(c, end='')
print('')

Submission status: Accepted

Solution code:
Query=list(input())
Q=int(input())
flag = True
strF=[]
strB=[]
for i in range(Q):
  S2=input()
  if S2=="1":
    flag = not flag
  else:
    x=S2.split()
    if flag:
      if x[1]=="1":
        strF.append(x[2])
      else:
        strB.append(x[2])
    else:
      if x[1]=="1":
        strB.append(x[2])
      else:
        strF.append(x[2])

if flag:
  strF.reverse()
  Query=strF+Query+strB
else:
  strB.reverse()
  Query.reverse()
  Query=strB+Query+strF
 
print("".join(Query))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
Flag = False
right=''
left=''

for i in range(q):
    t = input()
    if t[0] == '1':
        Flag = not Flag
    else:
        _, f, c = t.split()
        if Flag == False and f == '2' or Flag == True and f == '1':
            right += c
        else:
            left += c

ans = left[::-1] + s + right
print(ans if not Flag else ans[::-1])

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
cond = 1
pre = ''
epi = ''
for i in range(Q):
	q = list(map(str, input().split()))
	if q[0] == "1":
		cond *= -1
	else:
		if q[1] == "1":
			if cond == 1:
				pre += q[2]
			else:
				epi += q[2]
		else:
			if cond == 1:
				epi += q[2]
			else:
				pre += q[2]

if cond == 1:
	print(pre[::-1]+S+epi)
else:
	print(epi[::-1]+S[::-1]+pre)

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin

def main():
    S = deque([s for s in input()])
    Q = int(input())
    order = True
    for i in range(Q):
        query = stdin.readline()
        t = int(query[0])
        if t == 1:
            order = not order
        else:
            f = int(query[2])
            c = query[4]
            if not order:
                f = 3  - f # 2, 1 -> 1, 2
            left = True
            if f == 1:
                S.appendleft(c)
            else:
                S.append(c)
    if not order:
        S.reverse()
    print(''.join(S))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin

def main():
    S = deque([s for s in input()])
    Q = int(input())
    order = True
    for i in range(Q):
        query = stdin.readline().split()
        t = int(query[0])
        if t == 1:
            order = not order
        else:
            f = int(query[1])
            c = query[2]
            if not order:
                f = 3  - f # 2, 1 -> 1, 2
            left = True
            if f == 1:
                S.appendleft(c)
            else:
                S.append(c)
    if not order:
        S.reverse()
    print(''.join(S))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
s=[i for i in input()]
x=0 ; st=[[],[]]
for _ in range(int(input())):
    inp=list(map(str,input().split()))
    if len(inp)==1: x^=1
    else: st[(int(inp[1])-1)^x].append(inp[2])
if x==0:
    st[0].reverse()
    ans=st[0]+s+st[1]
elif x==1:
    st[1].reverse()
    s.reverse()
    ans=st[1]+s+st[0]
print(''.join(ans))

Submission status: Accepted

Solution code:
s=[i for i in input()]
x=0 ; st=[[],[]]
for _ in range(int(input())):
    inp=list(map(str,input().split()))
    if len(inp)==1: x^=1
    else: st[(int(inp[1])-1)^x].append(inp[2])
if x==0:
    st[0].reverse()
    ans=st[0]+s+st[1]
elif x==1:
    st[1].reverse()
    s.reverse()
    ans=st[1]+s+st[0]
print(''.join(ans))

Submission status: Accepted

Solution code:
S = input()
N = input()
N = int(N)
re = False
foward = ""
back = ""

for i in range(N):
    que = input().split(" ")
    if que[0] == "1":
        if re:
            re = False
        else:
            re = True
    else:
        if re:
            if que[1] == "1":
                back += que[2]
            else:
                foward += que[2]
        else:
            if que[1] == "1":
                foward += que[2]
            else:
                back += que[2]
S = foward[::-1]+S+back
if re:
    print(S[::-1])
else:
    print(S)

Submission status: Accepted

Solution code:
from collections import deque
def main():
    S = deque([s for s in input()])
    Q = int(input())
    order = True
    for i in range(Q):
        query = input().split()
        t = int(query[0])
        if t == 1:
            order = not order
        else:
            f = int(query[1])
            c = query[2]
            left = True
            if f == 1:
                if order:
                    left = True
                else:
                    left = False
            else:
                if order:
                    left = False
                else:
                    left = True
            if left:
                S.appendleft(c)
            else:
                S.append(c)
    if not order:
        S.reverse()
    print(''.join(S))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

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

c=0
S0=deque(S)

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

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

Submission status: Accepted

Solution code:
#abc 158 d
from collections import deque
x=deque()
flag=True # muki  true: migi false: hidari
s=input()
x.append(s)
q=int(input())
cnt=0
for i in range(q):
    line=list(input().split())
    if len(line)==1:
        cnt+=1
        if cnt%2==1:
            flag=False
        else:
            flag=True
    else:
        t=line[2]
        if line[1]=="1":
            if flag==True:
                x.appendleft(t)
            else:
                x.append(t)
        else:
            if flag==True:
                x.append(t)
            else:
                x.appendleft(t)
ans="".join(x)
if flag==False:
    ans=ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
rev = False
pre = ''
for i in range(q):
  inp = input()
  if int(inp[0]) == 1:
    rev = not rev
  else:
    t, f, c = inp.split()
    if (int(f) == 1 and rev) or (int(f) == 2 and not rev):
      s += c
    else:
      pre += c
print(s[::-1] + pre if rev else pre[::-1] + s)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
front = ''
end = ''
reverse = False
for i in range(Q):
    Query = input()
    if Query[0] == '1':
        reverse = not reverse 
    else:
        if (Query[2] == '1' and reverse == False) or (Query[2] == '2' and reverse == True):
            front = Query[4] + front
        else:
            end = end + Query[4]

S = front + S + end
if reverse == True:
    S = S[::-1]


print(S)
print('\n')

Submission status: Accepted

Solution code:
from collections import deque
S = str(input())
Q = int(input())
Qy = list([input().split() for _ in range(Q)])
rev = False #反転チェック
ans = deque([S])
for t in Qy:
    if t[0] == '1':
        rev = not rev
    else:
        if (t[1]=='1' and rev == False) or (t[1]=='2' and rev == True):
            ans.appendleft(t[2])
        else:
            ans.append(t[2])
if rev == False:
    print(''.join(ans))
else:
    print(''.join(ans)[::-1])

Submission status: Accepted

Solution code:
s = input()
q = int(input())
h,t = [],[]
p = 0

for i in range(q):
    c = input().split()
    if c[0] == '1':
        h,t = t,h
        p = 1-p
    else:
        if c[1] =='1':
            h.append(c[2])
        else:
            t.append(c[2])
if p ==1:
    s = s[::-1]
print("".join(h[::-1])+s+"".join(t))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 
MOD = 10**9+7

from collections import deque

S = deque(inpS())
Q = int(inp())
Query = [list(inpS().split())for _ in range(Q)]
is_revese = False

for q in Query:
  if q[0] == "1":
    is_revese = not is_revese
  else:
    if q[1] == "1": # 先頭に追加
      if is_revese: #反転状態
        S.append(q[2])
      else:
        S.appendleft(q[2])
    else:
      if is_revese: #反転状態
        S.appendleft(q[2])
      else:
        S.append(q[2])

if is_revese:
  S = reversed(S)
print("".join(S))

Submission status: Accepted

Solution code:
import collections

S = input()

Q = int(input())

list1 = collections.deque()
list2 = []
flag = 1

for _ in range(Q):
    op = input().split()
    if len(op) == 1:
        flag *= -1
    else:
        if flag == 1:
            if op[1] == '1':
                list1.appendleft(op[2])
            else:
                list2.append(op[2])
        else:
            if op[1] == '1':
                list2.append(op[2])
            else:
                list1.appendleft(op[2])

if flag == 1:
    print("".join(list1) + S + "".join(list2))
else:
    print(("".join(list1) + S + "".join(list2))[::-1])

Submission status: Accepted

Solution code:
s = input()
q = int(input())
swc = 0
top = ""
bot = ""
for i in range(q):
	a = list(map(str, input().split()))
	if a[0] == "1":
		if swc == 0:
			swc = 1
		else:
			swc = 0
	else:
		if swc == 0:
			if a[1] == "1":
				top += a[2]
			else:
				bot += a[2]
		else:
			if a[1] == "1":
				bot += a[2]
			else:
				top += a[2]
if swc == 0:
	print (top[::-1] + s + bot)
else:
	print (bot[::-1] + s[::-1] + top)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
T = 0
ins = ""
apd = ""
for i in range(Q):
    t = input()
    if t == "1":
        T = 1 - T
    else:
        _, F, C = t.split()
        F = int(F)
        if (T + F) % 2 == 0:
            apd += C
        else:
            ins += C
ans = ins[::-1] + S + apd
print(ans[::(-1)**T])

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline

S_deque = list(input())
S_deque = deque(S_deque[:-1])
Q = int(input())

top_flag = 1 # 0なら末尾、1なら最初

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

  else:
    in_flag = top_flag + int(item[1])
    if in_flag % 2 == 0:
      S_deque.appendleft(item[2])
    else:
      S_deque.append(item[2])

if top_flag == 0:
  S_deque.reverse()
else:
  pass

print(''.join(S_deque))

Submission status: Accepted

Solution code:
from collections import deque
d = deque(input())
Q = int(input())

# 逆フラグ
r = 1  # 1==正
for q in range(Q):
  s = input()
  if len(s) == 1:
    r *= -1
  else:
    s, f, c = s.split()
    if (f == '1' and r == -1) or (f == '2' and r == 1):
      d.append(c)
    else:
      d.appendleft(c)
ans = ''.join(d)
if r == -1:
  #d = d[::-1]
  print(ans[::-1])
else:
  print(ans)

Submission status: Accepted

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

s = list(input())
q = int(input())
reverse_counter = 0
left = []
for q0 in range(q):
    A = [i for i in input().split()]
    if len(A) == 1:
        reverse_counter += 1
    else:
        if A[1] == '1':
            if reverse_counter % 2 == 0:
                left.append(A[2])
            else:
                s.append(A[2])
        else:
            if reverse_counter % 2 == 0:
                s.append(A[2])
            else:
                left.append(A[2])

left.reverse()

res = left + s

if reverse_counter % 2 == 1:
    res.reverse()

print(''.join(res))

Submission status: Accepted

Solution code:
import queue
def reverse(s):
  ret = ''
  len_ = len(s) - 1
  for i in range(len(s)):
    ret += s[len_ - i]
  return ret

S = input()
N = int(input())


p_a = queue.LifoQueue()
rp_a = queue.Queue()
p_b = queue.Queue()
rp_b = queue.LifoQueue()
flag = False
for i in range(N):
  s = input()
  if s[0] == '1':
    flag = not flag   
  else:
    if (s[2] == '2' and not flag) or (s[2] == '1' and flag) :
      p_b.put(s[4])
    elif (s[2] == '2' and flag) or (s[2] == '1' and not flag) :
  	  p_a.put(s[4])
s_a = ''
s_b = ''
while not p_a.empty():
  s_a += p_a.get()
while not p_b.empty():
  s_b += p_b.get()
ret = s_a + S + s_b
if flag:
  ret = reverse(ret)

print(ret)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
list1 = []
list2 = []
flag = 1
for i in range(q):
    in_line = input().split()
    if in_line[0] == "1":
        flag *= -1
    else:
        if in_line[1] == "1":
            if flag == 1:
                list1.append(in_line[2])
            else:
                list2.append(in_line[2])
        else:
            if flag == 1:
                list2.append(in_line[2])
            else:
                list1.append(in_line[2])
if flag == 1:
    print("".join(list1[::-1]) + s + "".join(list2))
else:
    print("".join(list2[::-1]) + s[::-1] + "".join(list1))

Submission status: Accepted

Solution code:
from collections import deque

s=input()
S=[]
for i in range(len(s)):
    S.append(s[i])
S=deque(S)
q=int(input())
flag=False
for i in range(q):
    t=input()
    if t=='1':
        if flag:
            flag=False
        else:
            flag=True
    else:
        if t[2]=='1':
            if flag:
                S.append(t[4])
            else:
                S.appendleft(t[4])
        elif t[2]=='2':
            if flag:
                S.appendleft(t[4])
            else:
                S.append(t[4])

if flag:
    x=''.join(S)
    print(x[::-1])
else:
    print(''.join(S))

Submission status: Accepted

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

front = ''
back = S
reverse = False

for i in range(Q):
    query = input().split()
    if query[0] == '1':
        reverse = not reverse
    if query[0] == '2':
        if query[1] == '1':
            if not reverse:
                front = front + query[2]
            else:
                back = back + query[2]
        if query[1] == '2':
            if not reverse:
                back = back + query[2]
            else:
                front = front + query[2]

if not reverse:
    print(front[::-1] + back)
else:
    print(back[::-1] + front)

Submission status: Accepted

Solution code:
from collections import deque


def main():
    item = deque([i for i in input()])
    n = int(input())
    flag = 0
    append = item.append
    for i in range(n):
        tmp = input()
        if tmp[0] == '1':
            flag += 1
            continue

        _, q, s = tmp.split()
        if q == '1' and flag % 2 == 0:
            item.appendleft(s)
            continue
        elif q == '2' and flag % 2 == 1:
            item.appendleft(s)
            continue
        append(s)

    if flag % 2 == 1:
        item.reverse()
    print(''.join(item))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

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

flag = False
for _ in range(Q):
    q = input()
    if q[0] == '1':
        flag = not flag
    else:
        _, f, c = q.split()
        if flag:
            if f == '2':
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if f == '1':
                S.appendleft(c)
            else:
                S.append(c)

if flag:
    S.reverse()
print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque


def main():
    S = deque(list(input()))
    Q = int(input())
    count = 0
    for _ in range(Q):
        query = list(input().split())
        if query[0] == "1":
            count += 1
        if query[0] == "2":
            if count % 2 == 0:
                if query[1] == "1":
                    S.appendleft(query[2])
                elif query[1] == "2":
                    S.append(query[2])
            else:
                if query[1] == "1":
                    S.append(query[2])
                elif query[1] == "2":
                    S.appendleft(query[2])
    if count % 2 == 0:
        ans = "".join(S)
        print(ans)
    else:
        S.reverse()
        ans = "".join(S)
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# list(map(int, input().split()))

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

r = False
first = []
last = []
for i in range(q):
    ts = input().split()
    if len(ts) == 1:
        r = not r
        # print(f"reverse: {r}")
    else:
        f, c = int(ts[1]), ts[2]
        if (r and f == 1) or (not r and f == 2):
            # 最後尾
            # s += c
            last.append(c)
        else:
            # 先頭
            # s = c + s
            first.append(c)
s = "".join(first)[::-1] + s + "".join(last)
if not r:
    print(s)
else:
    print(s[::-1])

Submission status: Accepted

Solution code:
# list の append だけで実装する

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

T = 0
l = []
r = []
for _ in range(Q):
    Query = list(input().split())
    if len(Query) == 1:
        T ^= 1
    elif T == 0:
        s = Query[2]
        if Query[1] == '1':
            l.append(s)
        else:
            r.append(s)
    else:
        s = Query[2]
        if Query[1] == '1':
            r.append(s)
        else:
            l.append(s)

if T == 1:
    r.reverse()
    ans = ''.join(r) + S[::-1] + ''.join(l)
else:
    l.reverse()
    ans = ''.join(l) + S + ''.join(r)

print(ans)