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:
from collections import deque
s = input()
q = int(input())
flag = True
d = deque(s)
for i in range(q):
    inp = input()
    if inp == '1':
        flag = not flag

    else:
        _, f, c = map(str, inp.split())
        if f == '1':
            if flag:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if flag:
                d.append(c)
            else:
                d.appendleft(c)

if flag:
    while(d):
        t = d.popleft()
        print(t, end='')
else:
    while(d):
        t = d.pop()
        print(t, end='')

Submission status: Accepted

Solution code:
s=input()
q=int(input())
from collections import deque
qq=deque(s)
cnt=0
for i in range(q):
    j=input()
    if j=="1":
        cnt+=1
        continue
    _,k,l=j.split()
    if (k=="2")^cnt%2:
        qq.append(l)
    else:
        qq.appendleft(l)

if cnt%2==1:
    print("".join(reversed(list(qq))))
else:
    print("".join(qq))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
query = []
lead = ''
end = ''
inversion_flg = False
 
for j in range(q):
    i = input().split()
    if i[0] == "1":
        if inversion_flg:
            inversion_flg = False 
        else:
            inversion_flg = True
    else:
        if inversion_flg:
            if i[1] == "1":
                i[1] = "2"
            else:
                i[1] = "1"
        if i[1] == "1":
            lead+=i[2]
        else:
            end+=i[2]
 
lead = lead[::-1]
s = lead+s+end
if inversion_flg:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
count = 0
from collections import deque
q1 = deque()
q2 = deque()
for i in range(Q):
    li = input().split()
    if len(li) == 1:
        count +=1
        q11 = q1
        q22 = q2
        q1 = q22
        q2 = q11
    if len(li) == 3:
        if li[1] == '1':
            if count % 2 == 1:
                q1.append(li[2])
            if count % 2 == 0:
                q1.appendleft(li[2])
        if li[1] == '2':
            if count % 2 == 1:
                q2.appendleft(li[2])
            if count % 2 == 0:
                q2.append(li[2])
s1 = ''.join(list(q1))
s2 = ''.join(list(q2))
if count % 2 == 1:
    K = reversed(list(S))
    S = ''.join(K)
    s1 = ''.join(reversed(list(s1)))
    s2 = ''.join(reversed(list(s2)))
print(s1+S+s2)

Submission status: Accepted

Solution code:
from collections import deque
string = input()
s = deque(string)
iteration = int(input())
reverse = 1

for c in range(0,iteration):
    sousa = input().split()
    #操作１のとき
    if sousa[0] == "1":
        reverse = reverse*(-1)
    #操作２を行うとき
    elif sousa[1] == "1":
        if reverse == 1:
            s.appendleft(sousa[2])
        else:
            s.append(sousa[2])
    else:
        if reverse == 1:
            s.append(sousa[2])
        else:
            s.appendleft(sousa[2])

result = "".join(s)
if reverse == -1:
    result = result[::-1]


print(result)

Submission status: Accepted

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


def main():
    a = input()
    d = deque(a)
    n = int(input())
    flagr = True
    for i in range(n):
        j = list(readline().split())
        if len(j) == 1:
            flagr = not flagr
        elif int(j[1] == "1") ^ flagr:
            d.append(j[2])
        else:
            d.appendleft(j[2])
    if not flagr:
        d.reverse()
    print("".join(d))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
zengo = True
mae = []
usiro = []
for i in range(q):
  qi = input()
  if qi[0] == "1":
    if zengo:
      zengo = False
    else:
      zengo = True
  else:
    t, f, c = qi.split() 
    if zengo:
      if f == "1":
        mae.append(c)
      else:
        usiro.append(c)
    else:
      if f == "1":
        usiro.append(c)
      else:
        mae.append(c)
mae = mae[::-1]
s = ''.join(mae) + s + ''.join(usiro)
if zengo:
  print(s)
else:
  print(s[::-1])

Submission status: Accepted

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

added = ''
inv = 0
for i in range(Q):
  query = input().split()
  if query == ['1']:
    inv = (inv + 1)%2
  else:
    f, c = query[1], query[2]
    if (f=='1' and inv) or (f=='2' and not inv):
      S += c
    else:
      added += c

if inv:
  print(S[::-1] + added)
else:
  print(added[::-1] + S)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
query = [list(input().split()) for _ in range(q)]
right = []
left = []
rev_count = 0
for i in range(q):
  if query[i][0] == "1":
    rev_count += 1
    right,left = left,right
  else:
    if query[i][1] == "1":
      left.append(query[i][2])
    else:
      right.append(query[i][2])
if rev_count % 2 == 1:
  s = s[::-1]
print("".join(reversed(left))+s+"".join(right))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
t1 = 1
for i in range(q):
    t = input()
    if t == '1':
        t1 *= -1
    else:
        if t1 == 1:
            if t[2] == '1':
                s.appendleft(t[4])
            else:
                s.append(t[4])
        else:
            if t[2] == '2':
                s.appendleft(t[4])
            else:
                 s.append(t[4])

ans = ''
if t1 == -1:
    ans = ''.join(reversed(s))
else:
    ans = ''.join(s)

print(ans)

Submission status: Accepted

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

head = ""
tail = ""
main = ""

main = S

status = 0

while Q:
  Q -= 1
  l = list(map(str,input().split()))
  if l[0] == "1":
    status = 1-status
  else:
    letter = l[2]
    if l[1] == "1":
      if status == 0:
        head += letter
      else:
        tail += letter
    else:
      if status == 0:
        tail += letter
      else:
        head += letter

if status == 0:
  print(head[::-1]+main+tail)
else:
  print(tail[::-1]+main[::-1]+head)

Submission status: Accepted

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

head = ""
tail = ""
main = S

status = 0

while Q:
  Q -= 1
  l = list(map(str,input().split()))
  if l[0] == "1":
    status = (status+1)%2
  else:
    letter = l[2]
    if l[1] == "1":
      if status == 0:
        head += letter
      else:
        tail += letter
    else:
      if status == 0:
        tail += letter
      else:
        head += letter

if status == 0:
  print(head[::-1]+main+tail)
else:
  print(tail[::-1]+main[::-1]+head)

Submission status: Accepted

Solution code:
#python3
def main():
    s = input()
    addfront = ''
    addback = ''
    q = int(input())
    direction = 0
    for i in range(q):
        t = input()
        if t[0] == '1':
            direction += 1
            continue
        if t[0] == '2':
            if direction % 2 == 0:
                if t[2] == '1':
                    addfront = t[4] + addfront 
                if t[2] == '2':
                    addback = addback + t[4]
            else:
                if t[2] == '1':
                    addback = addback + t[4]
                if t[2] == '2':
                    addfront = t[4] + addfront
    ans = addfront + s + addback
    if direction%2 == 0:
        print(ans)
    else:
        ans = ''.join(reversed(ans))
        print(ans)
main()

Submission status: Accepted

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


class Node:
    def __init__(self, value=None, prev=None, next=None):
        self.value = value
        self.prev = prev
        self.next = next

    def __str__(self):
        return str(self.value)


class LinkedList:
    def __init__(self, initial_string=None):
        self.first = Node()
        self.last = Node()
        self.first.next = self.last
        self.last.prev = self.first
        self.should_reverse = False
        for s in initial_string:
            self.insert_last(s)

    def _insert_first(self, value=None):
        prev_next = self.first.next
        self.first.next = Node(value, self.first, prev_next)
        prev_next.prev = self.first.next

    def _insert_last(self, value=None):
        prev_prev = self.last.prev
        self.last.prev = Node(value, prev_prev, self.last)
        prev_prev.next = self.last.prev

    def insert_first(self, value=None):
        if self.should_reverse:
            self._insert_last(value)
        else:
            self._insert_first(value)

    def insert_last(self, value=None):
        if self.should_reverse:
            self._insert_first(value)
        else:
            self._insert_last(value)

    def set_reverse(self):
        self.should_reverse = not self.should_reverse

    def _print(self):
        node = self.first.next
        while node is not None:
            if node.value is not None:
                print(node, end="")
            node = node.next
        print("")

    def _print_reverse(self):
        node = self.last.prev
        while node is not None:
            if node.value is not None:
                print(node, end="")
            node = node.prev
        print("")

    def print_list(self):
        if self.should_reverse:
            self._print_reverse()
        else:
            self._print()


def formation():
    global S, Q

    linked_list = LinkedList(S)

    for _ in range(Q):
        t_str = input()

        if t_str[0] == '1':
            linked_list.set_reverse()
        else:
            split = t_str.split(' ')
            f = split[1]
            c = split[2]
            if f == '1':
                linked_list.insert_first(c)
            else:
                linked_list.insert_last(c)

    linked_list.print_list()


formation()

Submission status: Accepted

Solution code:
from collections import deque
s=deque(input())
n=int(input())
r=0
for i in range(n):
    strlist=input()
    if strlist[0]=='1':
        r+=1
    elif strlist[0]=='2' and strlist[2] == '1':
        if r%2==0:
            s.appendleft(strlist[4])
        else:
            s.append(strlist[4])
    elif strlist[0]=='2' and strlist[2] =='2':
        if r%2==0:
            s.append(strlist[4])
        else:
            s.appendleft(strlist[4])
if r%2==0:
    for s_ in s:
        print(s_, end='')
else:
    s.reverse()
    for s_ in s:
        print(s_, end='')

Submission status: Accepted

Solution code:
import collections

S = collections.deque(list(input()))
Q = int(input())
rev = 0
for i in range(0, Q):
    query = list(input().split())
    if query[0] == "1":
        rev = rev+1
    else:
        if query[1] == "1":
            if rev%2 == 0:
                S.appendleft(query[2])
            else:
                S.append(query[2])
        else:
            if rev%2 == 0:
                S.append(query[2])
            else:
                S.appendleft(query[2])
if rev%2 == 0:
    for i in S:
        print (i, end = "")
else:
    for i in reversed(S):
        print (i, end = "")

Submission status: Accepted

Solution code:
from collections import deque
q = deque(input())
Q = int(input())
cnt = 0
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        cnt += 1
    else :
        if query[1] == '1':
            if cnt % 2 == 0:
                q.appendleft(query[2])
            else :
                q.append(query[2])
        else :
            if cnt % 2 == 1:
                q.appendleft(query[2])
            else :
                q.append(query[2])
if cnt % 2 == 1: q.reverse()
print(''.join(q))

Submission status: Accepted

Solution code:
from collections import deque
S = input()
q = deque()
for s in S:
    q.append(s)

Q = int(input())
flag = True
cnt = 0
for _ in range(Q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        # 反転するクエリ
        cnt += 1
        if flag:
            flag = False
        else:
            flag = True
    else:
        f, c = query[1], query[2]
        if f == "1":  # 先頭に追加する命令
            if flag:  # 反転していない状態であれば前に追加する
                q.appendleft(c)
            else:  # 反転している状態であれば、後ろに追加する
                q.append(c)
        elif f == "2":  # 末尾に追加する命令
            if flag:  # 反転していない状態であれば末尾に追加する
                q.append(c)
            else:
                q.appendleft(c)
ans = "".join(list(q))
if cnt % 2 == 1:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

def string_formation(s, q_list):
    ans = deque()
    for x in s:
        ans.append(x)
    count = 0
    for query in q_list:
        if query[0] == '1':
            count += 1
        else:
            if count % 2 == 0:
                if query[1] == '1':
                    ans.appendleft(query[2])
                else:
                    ans.append(query[2])
            else:
                if query[1] == '1':
                    ans.append(query[2])
                else:
                    ans.appendleft(query[2])
    if count % 2 == 1:
        ans.reverse()
    print(''.join(ans))

s = input()
q = int(input())
q_list = []
for _ in range(q):
    tmp = input().split()
    q_list.append(tmp)

string_formation(s, q_list)

Submission status: Accepted

Solution code:
# D - String Formation
s = input()
q = int(input())
f = b = ''
odd = 0
for i in range(q):
    t,*fc = input().split()
    if t=='1': odd ^= 1 #t1
    else: #t2
        if odd ^ (int(fc[0])-1): b += fc[1]
        else: f += fc[1]
s = b[::-1] + s[::-1] + f if odd else f[::-1] + s + b
print(s)

Submission status: Accepted

Solution code:
from collections import deque

N = deque(input())
Q = int(input())
Qs = []
for _ in range(Q):
    Qs.append(list(map(str, input().split())) ) 
k=0
for q in Qs:
    if q[0] == '1':
        k+=1
    else:
        if k%2 ==0:
            if q[1]=='1':
                N.appendleft(q[2])
            else:
                N.append(q[2])
        else:
            if q[1]=='2':
                N.appendleft(q[2])
            else:
                N.append(q[2])
if k%2==1:
    ans=''
    for x in reversed(N):
        ans += x
else:
    ans=''
    for x in N:
        ans += x

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
q = int(input())
 
r = 0
for i in range(q):
  tfc = input().split()
  if tfc[0] == '1':
    r += 1
  else:
    if tfc[1] == '1':
      if r % 2 == 0:
        s.appendleft(tfc[2])
        #s.insert(0, tfc[2])
      else:
        s.append(tfc[2])
    if tfc[1] == '2':
     if r % 2 == 1:
        s.appendleft(tfc[2])
     else:
        s.append(tfc[2])          
if r % 2 == 0:
  for i in s:
      print(i, end='')
else:
  for i in (reversed(s)):
    print(i, end='')

Submission status: Accepted

Solution code:
# D - String Formation
from collections import deque
s = deque(input())
q = int(input())
odd = 0
for i in range(q):
    t,*fc = input().split()
    if t=='1': odd ^= 1 #t1
    else: #t2
        if odd ^ (int(fc[0])-1): s.append(fc[1])
        else: s.appendleft(fc[1])
if odd: deque.reverse(s)
print(''.join(s))

Submission status: Accepted

Solution code:
s = list(input())
q = int(input()) #int(input())
inv = 1
query = [input() for _ in range(q)]
bef = []
aft = []


for t in query:
  if t == '1':
    inv *= -1 #s = s[::-1]
  else:
    _, f, c = map(str, t.split())
    if (f =='1' and inv == 1) or (f =='2' and inv == -1):
      # s = c+s
      bef.append(c)
    else:
      # s = s+c
      aft.append(c)

bef.reverse()
bef.extend(s)
bef.extend(aft)

print(''.join(bef[::inv]))

Submission status: Accepted

Solution code:
from collections import deque
S=input()
N=int(input())
Q=[input() for i in range(N)]
T=deque(S)
cnt=0
for i in range(N):
    if Q[i][0]=="1":
        cnt+=1
    if cnt%2==0:
        if Q[i][0]=="2":
            if Q[i][2]=="1":
                T.appendleft(Q[i][4])
            if Q[i][2]=="2":
                T.append(Q[i][4])
    if cnt%2==1:
        if Q[i][0]=="2":
            if Q[i][2]=="1":
                T.append(Q[i][4])
            if Q[i][2]=="2":
                T.appendleft(Q[i][4])
if cnt%2==1:
    T.reverse()
print(''.join(T))

Submission status: Accepted

Solution code:
from collections import deque

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

R=0
S=deque(S)

for i in range(Q):
    temp=list(input().split())
    if int(temp[0])==1:
        R+=1
    elif int(temp[0])==2:
        F=int(temp[1])
        c=temp[2]
        if F==1:
            if R%2==0:
                S.appendleft(c)
            else:
                S.append(c)
        elif F==2:
            if R%2==0:
                S.append(c)
            else:
                S.appendleft(c)

if R%2==1:
    S=list(S)
    ans=S[-1::-1]
    ans=map(str,ans)
else:
    ans=map(str,S)

print("".join(ans))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
revFlag = False
head, tail = [], []

for i in range(q):
  query = list(input().split())
  if int(query[0]) == 1:
    revFlag = not revFlag
  else:
    if int(query[1]) == 1:
      if not revFlag:
        head.append(query[2])
      else:
        tail.append(query[2])
    else:
      if not revFlag:
        tail.append(query[2])
      else:
        head.append(query[2])

if not revFlag:
  h = "".join(reversed(head))
  t = "".join(tail)
  print("".join([h, s, t]))
else:
  h = "".join(head)
  t = "".join(reversed(tail))
  print("".join([t, s[::-1], h]))

Submission status: Accepted

Solution code:
# D - String Formation
from collections import deque
s = deque(input())
q = int(input())
odd = 0
for i in range(q):
    t,*fc = input().split()
    if t=='1': odd ^= 1 #t1
    else: #t2
        if odd ^ (int(fc[0])-1): s.append(fc[1])
        else: s.appendleft(fc[1])
if odd: deque.reverse(s)
print(*s, sep='')

Submission status: Accepted

Solution code:
from collections import deque

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

d = deque([S[i] for i in range(len(S))])
#print(d)

flg = 0

for i in range(Q):
    query = input()
    if query[0] == '1':
        flg = flg%2 + 1
    else:
        t, f, c = query.split()
        if f == '1':
            if flg % 2 == 0:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if flg % 2 == 0:
                d.append(c)
            else:
                d.appendleft(c)
if flg % 2 == 0:
    print(''.join(list(d)))
else:
    tmp = ''.join(list(d))
    print(tmp[::-1])

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
flag = True  # 今の状態, 反転していない状態がTrue, 反転している状態がFalse
cnt = 0  # この値が最後に奇数であればもう一度反転する必要がある
before = []
after = []
for _ in range(Q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        # 反転するクエリ
        cnt += 1
        if flag:
            flag = False
        else:
            flag = True
    else:
        f, c = query[1], query[2]
        if f == "1":  # 先頭に追加する命令
            if flag:  # 反転していない状態であれば前に追加する
                before.append(c)
            else:  # 反転している状態であれば、後ろに追加する
                after.append(c)
        elif f == "2":  # 末尾に追加する命令
            if flag:  # 反転していない状態であれば末尾に追加する
                after.append(c)
            else:
                before.append(c)
before.reverse()
ans = "".join(before) + S + "".join(after)
if cnt % 2 == 1:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

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

s = input().strip()
q = int(input())
front = []
back = []
flip = False

for i in range(q):
    query = input()
    if query[0] == '1':
        flip = (not flip)
    else:
        _, f, c = query.split()
        if flip:
            if f == '1':
                back.append(c)
            else:
                front.append(c)
        else:
            if f == '1':
                front.append(c)
            else:
                back.append(c)

if flip:
    print(''.join(back[::-1]) + s[::-1] + ''.join(front))
else:
    print(''.join(front[::-1]) + s + ''.join(back))

Submission status: Accepted

Solution code:
import collections
 
s = collections.deque(list(input()))
q = int(input())
rev = 0
for i in range(q):
    tfc = list(input().split())
    if tfc[0]=='1':
        rev = abs(rev-1)
    else:
        t,f,c = tfc
        if rev:
            if f=='1':
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if f=='1':
                s.appendleft(c)
            else:
                s.append(c)
if rev:
    t = []
    for i in range(len(s)):
        t.append(s.pop())
    print(''.join(t))
else:
    print(''.join(s))

Submission status: Accepted

Solution code:
s=input()
n=int(input())
rev=1
pre=''
suf=''

for i in range(n):
  q=input().split()
  if q[0]=='1':
    rev+=1
  elif (rev+int(q[1]))%2==0:
    pre=q[2]+pre
  else:
    suf=suf+q[2]
    
ans=pre+s+suf
  
if rev%2==0:
  print(ans[::-1])
else:
  print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

S = deque(S)
rev = False

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S.append(TFC[2])
            else:
                S.appendleft(TFC[2])
        else:
            if rev:
                S.appendleft(TFC[2])
            else:
                S.append(TFC[2])
if rev:
    S.reverse()
print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque

s = deque(str(input()))
q = int(input())
hanten = 0

for i in range(q):
    x = input().split()
    if len(x) == 1:
        t = x[0]
        if t == "1" and hanten == 0:
            hanten = 1
        elif t == "1" and hanten == 1:
            hanten = 0

    else:
        f = int(x[1])
        c = str(x[2])
        if f == 1 and hanten == 0:
            s.appendleft(c)

        elif f == 1 and hanten == 1:
            s.append(c)

        elif f == 2 and hanten == 0:
            s.append(c)

        elif f == 2 and hanten == 1:
            s.appendleft(c)

else:
    if hanten == 1:
        s.reverse()

print("".join(s))

Submission status: Accepted

Solution code:
S = input().rstrip()
Q = int(input().rstrip())
reversed = False
from collections import deque
q = deque()
q.extend(list(S))
for _ in range(Q):
    l = input().rstrip()
    if l.startswith('1'):
        reversed = False if reversed else True
    else:
        _, F, C = l.split()
        if F == '1':
            if reversed:
                q.append(C)
            else:
                q.appendleft(C)
        elif F == '2':
            if reversed:
                q.appendleft(C)
            else:
                q.append(C)
ret = ''.join(list(q)[::-1]) if reversed else ''.join(list(q))
print(ret)

Submission status: Accepted

Solution code:
def main():
    import collections
    S = input()
    q = collections.deque(S)
    Q = int(input())
    isrev = False
    for _ in range(Q):
        query = input()
        if query == "1":
            isrev = isrev ^ True
        else:
            _, f, c = query.split()
            ishead = (f=="1")
            ishead = ishead ^ isrev
            if ishead:
                q.appendleft(c)
            else:
                q.append(c)
    if isrev:
        q = reversed(q)
    print("".join(q))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

ans = deque()
center_ans_1 = input()
center_ans = list(center_ans_1)
for i in range(len(center_ans)):
    a = center_ans[i]
    ans.append(a)

q = int(input())
list_data = [ list(map(str,input().split(" "))) for i in range(q)]
#print(list_data)
re = 0
for i in range(q):
    list_a = list_data[i]
    #print(list_a)
    if list_a[0] == "1":
        re += 1
    elif list_a[0] == "2":
        if list_a[1] == "1":
            if re % 2 == 0:
                ans.appendleft(list_a[2])
            elif re % 2 == 1:
                ans.append(list_a[2])
        elif list_a[1] == "2":
            if re % 2 == 0:
                ans.append(list_a[2])
            elif re % 2 == 1:
                ans.appendleft(list_a[2])

if re % 2 == 1:
    ans.reverse()

print("".join(ans))

Submission status: Accepted

Solution code:
from sys import stdin

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

reverse_flg = False

former_list = []
latter_list = []

for q in range(Q):
    T = stdin.readline()[:-1]

    if T == '1':
        if reverse_flg:
            reverse_flg = False
        else:
            reverse_flg = True
    
    else:
        t, f, c = T.split()
        if (f == '1' and reverse_flg == False) or (f == '2' and reverse_flg == True):
            former_list.append(c)
        else:
            latter_list.append(c)


if reverse_flg:
    latter_list = ''.join(reversed(latter_list))
    former_list = ''.join(former_list)
    S = ''.join(list(reversed(S)))
    S = ''.join([latter_list, S, former_list])

else:
    former_list = ''.join(reversed(former_list))
    latter_list = ''.join(latter_list)
    S = ''.join([former_list, S, latter_list])

print(S)

Submission status: Accepted

Solution code:
# D - String Formation
s = input()
q = int(input())

f = ''
b = ''
odd = 0
for i in range(q):
    t,*fc = input().split()
    if t=='1': odd ^= 1 #t1
    else: #t2
        if odd ^ (int(fc[0])-1): b += fc[1]
        else: f += fc[1]

if odd: print(b[::-1] + s[::-1] + f)
else: print(f[::-1] + s + b)

Submission status: Accepted

Solution code:
import sys
S =input()
Q=int(input())
orders=[]
for line in sys.stdin.readlines():
    orders.append(line.rstrip().split(" "))
dire=1
right=""
left=""
center=""
for order in orders:
    if order[0]=="1":
        dire=dire*(-1)
        center=right
        right=left
        left=center
    elif order[0]=="2" and order[1]=="1":
        if dire==1:
            left=order[2]+left
        if dire==-1:
            left=left+order[2]
    elif order[0]=="2" and order[1]=="2":
        if dire==1:
            right=right+order[2]
        if dire==-1:
            right=order[2]+right
if dire==1:
    S=S
    right=right
    left=left
elif dire==-1:
    S=S[::-1]
    right=right[::-1]
    left=left[::-1]
print(left+S+right)

Submission status: Accepted

Solution code:
s = list(input())
n = int(input())
head = []
tail = []

cnt = 0
for _ in range(n):
    li = list(input().split())
    if li[0] == '1':
        head, tail = tail, head
        cnt += 1
    else:
        if li[1] == '1':
            head.append(li[2])
        else:
            tail.append(li[2])

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

ans = head[::-1] + s + tail
print(*ans , sep='')

Submission status: Accepted

Solution code:
s = list(input())
n = int(input())
head = []
tail = []

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

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

ans = head[::-1] + s + tail
print(*ans , sep='')

Submission status: Accepted

Solution code:
s = list(input())
n = int(input())
head = []
tail = []

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

if cnt % 2 != 0:
    s = s[::-1]
    
ans = head[::-1] + s + tail
print(*ans , sep='')

Submission status: Accepted

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

rev = False
S1 = ''
S2 = ''

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S2 += TFC[2]
            else:
                S1 = TFC[2] + S1
        else:
            if rev:
                S1 = TFC[2] + S1
            else:
                S2 += TFC[2]

if rev:
    print(S2[::-1] + S[::-1] + S1[::-1])
else:
    print(S1 + S + S2)

Submission status: Accepted

Solution code:
import sys
from collections import deque

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7


q = deque()
for c in input():
    q.append(c)

rev = False
for _ in range(I()):
    query = input()
    if query.startswith('1'):
        rev = not rev
        continue
    _, f, c = query.split()
    right = f == '2'
    right = not right if rev else right
    if right:
        q.append(c)
    else:
        q.appendleft(c)

if rev:
    print(''.join(reversed(q)))
else:
    print(''.join(q))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque
def main():
    S=input()
    Q=int(input())
    query=[list(input().split()) for _ in range(Q)]
    
    d=deque(S)
    c=0

    for q in query:
        if q[0]=="1":
            c+=1
        elif q[0]=="2":
            if (c%2==0 and q[1]=="1") or (c%2==1 and q[1]=="2"):
                d.appendleft(q[2])
            else:
                d.append(q[2])
    if(c%2!=0):
        d.reverse()
    print("".join(d))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
from collections import deque

s= input()
n=int(input())
q= [list(input().split()) for i in range(n)]

s=deque(s)
flag=True
for i in range(n):
    if q[i][0]==str(1):
        if flag:
            flag=False
        else:
            flag=True
    else:
        if q[i][1]==str(1):
            if flag:
                s.appendleft(q[i][2])
            else:
                s.append(q[i][2])
        if q[i][1]==str(2):
            if flag:
                s.append(q[i][2])
            else:
                s.appendleft(q[i][2])

if not flag:
    s.reverse()
print(''.join(s))