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:
S=input()
b=1
x=''
y=''
for i in range(int(input())):
    q=input()
    if q=='1':b^=1
    else:
        a,f,c=q.split()
        if (int(f)-1)^b:x=c+x
        else:y=y+c
S=x+S+y
print(S if b else S[::-1])

Submission status: Accepted

Solution code:
s = input()
q = int(input())
hanten = False
sentou = ""
matubi = ""
query = []
for i in range(q):
    query.append(list(input().split()))

for i in range(q):
    if hanten == False:
        if len(query[i]) == 1:
            hanten = True
        else:
            if int(query[i][1]) == 1:
                #先頭にquery[i][2]を追加
                sentou = query[i][2] + sentou
            else:
                #末尾にquery[i][2]を追加
                matubi = matubi + query[i][2]
        #print(s)
    else:
        if len(query[i]) == 1:
            hanten = False
        else:
            if int(query[i][1]) == 1:
                #末尾にquery[i][2]を追加
                matubi = matubi + query[i][2]
            else:
                #先頭にquery[i][2]を追加
                sentou = query[i][2] + sentou
        #print(s)

s = sentou + s + matubi
if hanten == False:
    print(s)
else:
    print(s[::-1])

Submission status: Accepted

Solution code:
from collections import deque

s = input()
a = deque()
for i in s:
  a.append(i)
q = int(input())
rev = 0
for i in range(q):
  s = input()
  if s[0] == '1':
    rev ^= 1
  else:
    b = (int(s[2]) - 1) ^ rev
    if b:
      a.append(s[4])
    else:
      a.appendleft(s[4])
if rev:
  a.reverse()
for i in a:
  print(i, end="")

Submission status: Accepted

Solution code:
from collections import deque
s = list(input())
q = int(input())
rev = 0
revcnt = 0
s_q = deque(s)
for _ in range(q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        revcnt += 1
        if rev == 0:
            rev = 1
        elif rev == 1:
            rev = 0

    elif query[0] == "2":
        if rev == 0:
            if query[1] == "1":
                s_q.appendleft(query[2])
            elif query[1] == "2":
                s_q.append(query[2])
        else:
            if query[1] == "1":
                s_q.append(query[2])
            elif query[1] == "2":
                s_q.appendleft(query[2])
s = list(s_q)
if revcnt%2 == 0:
    print("".join(s))
else:
    print("".join(s[::-1]))

Submission status: Accepted

Solution code:
S = input()
len_s = len(S)
rev = False
max_ = 2*10**5
index = max_ + len_s
rev_index = max_ - 1
s = ["." for _ in range(max_*2 + len_s)]
for i in range(len_s):
    s[max_+i] = S[i]
for _ in range(int(input())):
  t = input()
  if t == "1":
    rev = not rev
  else:
    t = t.split()
    if (t[1] == "1" and not rev) or (t[1] == "2" and rev):
      s[rev_index] = t[2]
      rev_index -= 1
    else:
      s[index] = t[2]
      index += 1
if rev:
  for i in range((max_ + max_ + len_s - 1), 0, -1):
    if s[i] != ".":
      print(s[i], end="")
else:
  for i in range((max_ + max_ + len_s)):
    if s[i] != ".":
      print(s[i], end="")

Submission status: Accepted

Solution code:
from collections import deque

s = input()
d = deque(s)
n = int(input())
flag = 0
for _ in range(n):
    q = input().split()
    if (len(q)==1):
        if(flag == 0):
            flag = 1
        else:
            flag = 0
    else:
        a = int(q[1])
        b = q[2]
        if (a == 1):
            if flag==0:
                d.appendleft(b)
            else:
                d.append(b)
        else:
            if flag==0:
                d.append(b)
            else:
                d.appendleft(b)

if flag==1:
    d.reverse()
ans = list(d)
xxx = "".join(ans)
print(xxx)

Submission status: Accepted

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

S=list(input()[:-1])
Q=int(input()[:-1])
f=0
back=[]
forward=[]

for _ in range(Q):
    q=input().split()
    if int(q[0])==1:
        f = (f+1)%2
    if int(q[0])==2:
        if int(q[1])==1:
            if f==0:
                forward.append(q[2])
            else:
                back.append(q[2])
        else:
            if f==0:
                back.append(q[2])
            else:
                forward.append(q[2])

if f==0:
    print("".join(forward[::-1]+S+back))
else:
    print("".join((forward[::-1]+S+back)[::-1]))

Submission status: Accepted

Solution code:
from collections import deque

S = deque(list(str(input())))
Q = int(input())
flag = False

for i in range(Q):
    Query =  [str(p) for p in input().split()]
    if(int(Query[0]) == 1):
        flag = not(flag)
    else:
        if(int(Query[1]) == 1):
            if(flag):
                S.append(Query[2])
            else:
                S.appendleft(Query[2])
        else:
            if(flag):
                S.appendleft(Query[2])
            else:
                S.append(Query[2])

if(flag):
    S.reverse()

print("".join(list(S)))

Submission status: Accepted

Solution code:
s=input()
n=int(input())
q=[list(map(str,input().split())) for i in range(n)]
atama=0
x=0
for i in range(n):
  if q[i][0]=='2':
    x+=1

oshiri=len(s)+x-1
ans=[]
for i in range(len(s)+x):
    ans.append('x')
x=0
for i in range(n)[::-1]:
  if q[i][0]=='1':
    x+=1
  else:
    if (int(q[i][1])+x)%2==0:
      ans[oshiri]=q[i][2]
      oshiri-=1
    else:
      ans[atama]=q[i][2]
      atama+=1

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

for i in range(len(s)):
  ans[i+atama]=s[i]

answer=ans[0]
for i in range(1,len(ans)):
    answer+=ans[i]
    
print(answer)

Submission status: Accepted

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

def make_reverse(S):

    if len(S) == 1:
        return S

    reversedS = ""
    for i in range(len(S)-1, -1, -1):
        reversedS += S[i]
    else: return reversedS

def main(S, Q, lst_query):

    cnt_reverse = 0
    leftS  = ""
    rightS = ""

    for i in range(Q-1, -1, -1):
        
        query = lst_query[i]

        if int(query[0]) == 1:
            cnt_reverse += 1
        else:
            if cnt_reverse % 2 == 0:
                if int(query[1]) == 1: leftS  = leftS + query[2]
                else                 : rightS = query[2] + rightS
            else:
                if int(query[1]) == 1: rightS = query[2] + rightS
                else                 : leftS  = leftS + query[2]

    if cnt_reverse % 2 == 1: S = make_reverse(S)
    return leftS + S + rightS


if __name__ == "__main__":

    answer = main(S, Q, lst_query)
    print(answer)

Submission status: Accepted

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

for _ in range(q):
    Q = list(map(str,input().split()))
    if int(Q[0]) == 1:
        if k == 0:
            k = 1
        else:
            k = 0
    else:
        if k == 0:
            if int(Q[1]) == 1:
                s.appendleft(Q[2])
            else:
                s.append(Q[2])
        else:
            if int(Q[1]) == 1:
                s.append(Q[2])
            else:
                s.appendleft(Q[2])


if k == 1:
    s = list(s)[::-1]
    
print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
tmp = deque()
for i in range(len(s)):
    tmp.append(s[i])
flag = 0
for _ in range(q):
    _s = input()
    if _s == "1":
        flag += 1
    else:
        t, f, c = _s.split(" ")
        r = flag % 2
        if f == "1" and r == 0:
            #tmp = c + tmp
            tmp.appendleft(c)
        elif f == "1" and r == 1:
            #tmp = tmp + c
            tmp.append(c)
        elif f == "2" and r == 0:
            #tmp = tmp + c
            tmp.append(c)
        elif f == "2" and r == 1:
            #tmp = c + tmp
            tmp.appendleft(c)
if flag % 2 == 0:
    for i in range(len(tmp)):
        print(tmp.popleft(), end="")
else:
    for i in range(len(tmp)):
        print(tmp.pop(), end="")

Submission status: Accepted

Solution code:
s = input()
n = int(input())
re = 1
head = ''
tail = ''

for i in range(n):
    q = input().split()
    if q[0] == '1':
        re = re * -1
    else:
        if re == 1:
            if q[1] == '1':
                head = q[2] + head
            else:
                tail = tail + q[2]
        else:
            if q[1] == '1':
                tail = tail + q[2]
            else:
                head = q[2] + head

print(head+s+tail) if re == 1 else print((head+s+tail)[::-1])

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    S = input()
    Sd = deque(list(S))
    Q = int(input())
    Query = [input().split() for i in range(Q)]
    flag = 0
    for q in Query:
        if q[0] == "1":
            flag += 1
        elif q[0] == "2":
            if q[1] == "1":
                if flag % 2 == 1:
                    Sd.append(q[2])
                elif flag % 2 == 0:
                    Sd.appendleft(q[2])
            elif q[1] == "2":
                if flag % 2 == 1:
                    Sd.appendleft(q[2])
                elif flag % 2 == 0:
                    Sd.append(q[2])

    if flag % 2 == 1:
        Sd.reverse()
        print("".join(Sd))
    elif flag % 2 == 0:
        print("".join(Sd))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S = list(str(input()))
Q = int(input())
s1 = []#先頭
s2 = []#後方
b = []
num_rev = 0
for i in range(Q):
  a = list(map(str, input().split()))
  if len(a) == 1:
    num_rev += 1
  else:
    if (num_rev + int(a[1]))%2==0:
      s2.append(a[2])
    else:
      s1.append(a[2])
if num_rev % 2 == 0:
  s1 = list(reversed(s1))
  s1.extend(S)
  s1.extend(s2)
  print(''.join(s1))
else:
  s2 = list(reversed(s2))
  s2.extend(list(reversed(S)))
  s2.extend(s1)
  print(''.join(s2))

Submission status: Accepted

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

start = ''
end = ''

rvs = False

for i in range(q):
  query = input()
  if query[0] == '1':
    if rvs == False:
      rvs = True
    else:
      rvs = False
  else:
    t, f, c = query.split()
    if f == '1':
      if rvs == False:
        start = c + start
      else:
        end = end + c
    else:
      if rvs == False:
        end = end + c
      else:
        start = c + start

if rvs == False:
  print(start + s + end)
else:
  ans = start + s + end
  print(ans[::-1])

Submission status: Accepted

Solution code:
from collections import deque
s = deque([i for i in input()])
q = int(input())
cnt = 0
for _ in range(q):
    s_ = input()
    if len(s_) == 1: cnt += 1
    else:
        if (int(s_[2]) + cnt % 2) % 2 == 0: s.append(s_[4])
        else: s.appendleft(s_[4])
if cnt % 2 == 1: print("".join(list(s)[::-1]))
else: print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque

def T2F1(org_S,rev_S, C, jdg):
    if jdg%2 == 0:
        org_S.appendleft(C)
        rev_S.append(C)
        return org_S, rev_S
    else:
        org_S.append(C)
        rev_S.appendleft(C)
        return org_S, rev_S
def T2F2(org_S, rev_S, C,jdg):
    if jdg%2 == 0:
        org_S.append(C)
        rev_S.appendleft(C)
        return org_S, rev_S
    else:
        org_S.appendleft(C)
        rev_S.append(C)
        return org_S, rev_S
    
org_S = deque(list(input()))
rev_S = deque(list(reversed(org_S)))
Q = int(input())
jdg = 0

for i in range(Q):
    A = list(input().split())
    if A[0] == "1":
        jdg += 1
    elif A[1] == "1":
        T2F1(org_S, rev_S, A[2],jdg)
    else:
        T2F2(org_S, rev_S, A[2],jdg)
        
if jdg%2 ==0:
    org_S = "".join(org_S)
    print(org_S)
    
else:
    rev_S = "".join(rev_S)
    print(rev_S)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
Query = []
kaisu = 0
sento = ""
for i in range(Q):
    Query.append(input().split())

for i in Query:
    if int(i[0]) == 1:
        kaisu = kaisu + 1
    elif int(i[0]) == 2:
        if kaisu % 2 == 0:
            if int(i[1]) == 1:
                sento = sento + i[2]
            else:
                S = S + i[2]
        else:
            if int(i[1]) == 2:
                sento = sento + i[2]
            else:
                S = S + i[2]
S = ''.join(list(reversed(sento))) + S
if kaisu % 2 == 0:
    print(S)
else:
    print(''.join(list(reversed(S))))

Submission status: Accepted

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

mode = True  # True: Normal False: Reverse
pre = []
aft = []
for query in Query:
    if query[0] == '1':
        # モードを反転
        mode = not mode
    else:
        if (mode and (query[1] == '1')) or (not mode and (query[1] == '2')):
            pre.append(query[2])
        else:
            aft.append(query[2])
    
ans = ''.join(pre[::-1]) +  S + ''.join(aft)
    
if not mode:
    ans = ans[::-1]
 
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

flag = 0

for i in range(Q):
  A = input().split()
  if A[0] == "1":
    if flag == 0:
      flag = 1
    else:
      flag = 0
  else:
    if A[1] == "1" and flag == 0 or A[1] == "2" and flag == 1:      
      a.appendleft(A[2])
    else:
      a.append(A[2])

if flag == 1:
  a.reverse()
print("".join(a))

Submission status: Accepted

Solution code:


import glob


# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = 'ABC\\158\\D'

# テスト用ファイル置き場のトップ
TOP_PATH = 'C:\\AtCoder'

class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class Solver(Common):

    def resolve(self):

        S = self.input_data().split()[0]
        Q = int(self.input_data())
        query = []

        zengo = True
        add_first = ""
        add_last = ""

        for i in range(Q):
            tmp = [j for j in self.input_data().split()]
            if tmp[0] == "1":
                query.append([1])
            else:
                query.append([2, int(tmp[1]), tmp[2]])

        for q in query:
            if q[0] == 1:
                zengo = not zengo
            else:
                if zengo:
                    if q[1] == 1:
                        add_first = q[2] + add_first
                    else:
                        add_last = add_last + q[2]
                else:
                    if q[1] == 2:
                        add_first = q[2] + add_first
                    else:
                        add_last = add_last + q[2]

        result = add_first + S + add_last
        if zengo:
            print(result)
        else:
            print(result[::-1])




solver = Solver(REL_PATH)
solver.exec_resolve()

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
ans = S
## vector が1なら正方向、-1なら逆方向
vector = 1
before = ""
after = ""
for i in range(Q):
    Query = input()
    if Query[0] == "1":
        vector *= -1
    elif vector == 1 and Query[0:3] == "2 1":
        before = Query[4] + before
    elif vector == 1 and Query[0:3] == "2 2":
        after = after + Query[4]
    elif vector == -1 and Query[0:3] == "2 1":
        after = after + Query[4]
    elif vector == -1 and Query[0:3] == "2 2":
        before = Query[4] + before
#     print(before)
#     print(after)
if vector==1:
    print(before + S + after)
else:
    print(after[::-1]+S[::-1]+before[::-1])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import deque


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    a = input()
    Q = int(input())
    qs = [input().split() for _ in range(Q)]
    ans = deque(a)
    revcount = 0
    for q in qs:
        if len(q) == 1:
            revcount += 1
        else:
            if int(q[1]) == 1:
                if revcount % 2 == 0:
                    ans.appendleft(q[2])
                else:
                    ans.append(q[2])
            else:
                if revcount % 2 == 0:
                    ans.append(q[2])
                else:
                    ans.appendleft(q[2])
    if revcount % 2 == 1:
        ans.reverse()
    print(''.join(list(ans)))
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

is_reverse = False
s = deque(input())
q = int(input())
for _ in range(q):
    query = input().split()
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if (not is_reverse and query[1] == '1') or (is_reverse and query[1] == '2'):
            s.appendleft(query[2])
        else:
            s.append(query[2])
print(''.join(s)[::-1]) if is_reverse else print(''.join(s))

Submission status: Accepted

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

from collections import deque
s_list = [s for s in S]
que = deque(s_list)

mode = 1
for _ in range(Q):
  q_in = str(input())

  if q_in == '1':
    mode *= -1
    continue

  else:
    t, f_in, c = map(str, q_in.split())
    f = int(f_in)
    if mode == -1:
      if f_in == '1':
        f = 2
      else:
        f = 1
    if f == 1:
      que.appendleft(c)
    else:
      que.append(c)

if mode == -1:
  que = reversed(que)

ans = ''.join(que)
print(ans)

Submission status: Accepted

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

add_to_last = []
add_to_beginning = []

queries = [input() for _ in range(Q)]

mode = 1
for i in range(Q):
  query = queries[i]
  if query=='1':
    mode *= -1
  else:
    _, F, C = query.split()
    if F=='1':
      if mode==1:
        add_to_beginning.append(C)
      else:
        add_to_last.append(C)
    else:
      if mode==1:
        add_to_last.append(C)
      else:
        add_to_beginning.append(C)   

if mode==1:
  add_to_beginning.reverse()
  print("".join(add_to_beginning), end="")
  print(S, end="")
  print("".join(add_to_last))
else:
  add_to_last.reverse()
  print("".join(add_to_last), end="")
  print(S[::-1], end="")
  print("".join(add_to_beginning))

Submission status: Accepted

Solution code:
import sys
from collections import deque

V = list(input())
S = deque()
S.extend(V)
Q = int(input())

memo = []
r = 0
for i in range(Q):
    c = sys.stdin.readline().split()
    #print(c)
    if c[0] == '1':
        r += 1
        continue
    else:
        if c[1] == '1':
            memo.append((r, c[2]))
        else:
            memo.append((r+1, c[2]))
        
#print(memo)        
if r % 2 == 1:
    S.reverse()

for m in memo:
    s, l = m
    s = r - s
    if s % 2 == 0:
        S.appendleft(l)
    else:
        S.append(l)    

print(''.join(S))

Submission status: Accepted

Solution code:
def main():
    s = input()
    q = int(input())
    jun = True
    mae = ""
    ushiro = ""
    for _ in range(q):
        qu = input()
        if qu == '1':
            jun = not jun
        else:
            if qu[2] == '1':
                if jun:
                    mae = qu[4] + mae
                else:
                    ushiro = ushiro + qu[4]
            else:
                if jun:
                    ushiro = ushiro + qu[4]
                else:
                    mae = qu[4] + mae
    s = mae + s + ushiro
    if not jun:
        s = s[::-1]
    print(s)
main()

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
ans = deque()
ans.append(S)
turncnt = 0
for i in range(Q):
    query = input().split()
    if (query[0] == "1"):
        turncnt = turncnt + 1
    if (query[0] == "2"):
        if (query[1] == "1"):
            if (turncnt % 2 == 0):
                ans.appendleft(query[2])
            else:
                ans.append(query[2])
        if (query[1] == "2"):
            if (turncnt % 2 == 0):
                ans.append(query[2])
            else:
                ans.appendleft(query[2])

anstr = "".join(ans)
if (turncnt % 2 == 1):
    anstr = anstr[::-1]
print(anstr)

Submission status: Accepted

Solution code:
from collections import deque

s=input()
query=int(input())

li_q = []
for _ in range(query):
    li_q.append(input().split())

rev = False
q_str = deque([s])
for q in li_q:
    if q[0] == '1':
        rev = not rev
    else:
        if q[1] == '1':
            if rev:
                q_str.append(q[2])
            else:
                q_str.appendleft(q[2])
        else:
            if rev:
                q_str.appendleft(q[2])
            else:
                q_str.append(q[2])

li_str = list(q_str)
ans = ''.join(li_str)
if rev:
    ans = ans[::-1]
    
print(ans)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        if n:
            f = '1' if f != '1' else '2'
        if f == '1':
            left=c+left
        elif f == '2':
            right+=c
s = left+s+right
print(s[::-1] if n else s)

Submission status: Accepted

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

f = True
l, r = [], []
for i in range(q):
    if query[i][0] == "1":
        f = not f
    else:
        if f:
            if query[i][1] == "1":
                l.append(query[i][2])
            else:
                r.append(query[i][2])
        else:
            if query[i][1] == "1":
                r.append(query[i][2])
            else:
                l.append(query[i][2])

if f:
    print("".join(l[::-1]) + s + "".join(r))
else:
    print("".join(r[::-1]) + s[::-1] + "".join(l))

Submission status: Accepted

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

count = 0
for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        count += 1
    elif a[0] == '2':
        if count % 2 == 0 and a[1] == '1':
            S.appendleft(a[2])
        elif a[1] == '1':
            S.append(a[2])
        elif count % 2 == 0:
            S.append(a[2])
        else:
            S.appendleft(a[2])


if count % 2 == 0:
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverseFlag = False
forwardList = []
backwardList = []
for i in range(Q):
    tmp = input()
    if tmp[0] == '1':
        reverseFlag = not reverseFlag
    else:
        tmpt, tmpf, tmpc = tmp.split(' ')
        if tmpf == '1' and reverseFlag == False or tmpf == '2' and reverseFlag == True:
            forwardList.append(tmpc) 
        else:
            backwardList.append(tmpc)

if reverseFlag == True:
    S = ''.join(reversed(backwardList)) + S[::-1] + ''.join(forwardList)
else:
    S = ''.join(reversed(forwardList)) + S + ''.join(backwardList)
print(S)

Submission status: Accepted

Solution code:




s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

Submission status: Accepted

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    s = input()
    q = int(input())
    ans = deque()
    swap = False
    for i in range(len(s)):
        ans.append(s[i])

    for i in range(q):
        #print(ans, swap)
        inp = input()
        if len(inp) == 1:
            hoge = int(inp)
            if swap:
                swap = False
            else:
                swap = True
        else:
            # print(tuple(inp))
            fi, ci = tuple(inp)[2], tuple(inp)[4]
                       
            if int(fi) == 1:
                if not swap:
                    ans.appendleft(ci)
                else:
                    ans.append(ci)
            if int(fi) == 2:
                if not swap:
                    ans.append(ci)
                else:
                    ans.appendleft(ci)
    s_ans = str()
    ans = list(ans)
    if swap:
        for i in range(len(ans)-1,-1, -1):
            s_ans += ans[i]
    else:
        for i in range(len(ans)):
            s_ans += ans[i]
    
    print(s_ans)
                
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
checker=-1#-1で正向き、1で反転
BUF1,BUF2="",""
for _ in range(Q):
  B=input().split()
  if len(B)==3:
    T,F,C=B
    if all([F=="1",checker==-1]):
      BUF1+=C
    elif all([F=="1",checker==1]):
      BUF2+=C
    elif all([F=="2",checker==-1]):
      BUF2+=C
    elif all([F=="2",checker==1]):
      BUF1+=C
  else:
    checker*=-1
S=BUF1[::-1]+S+BUF2
print(S if checker==-1 else S[::-1])

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
import bisect as bi
from operator import mul
from functools import reduce
import math

def fast_comb(a,b,mod):
    up = reduce(lambda x,y: x*y%mod, range(a,a-b,-1))
    down = reduce(lambda x,y: x*y%mod, range(1,b+1))
    return (up * pow(down, mod-2, mod)) % mod

def prime(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]:
            continue
        for j in range(i*2, n+1, i):
            is_prime[j] = False
    return is_prime

def readlines(n):
    for _ in range(n):
        query = input().split()
        yield iter(query)

def main():
    a = input().strip()
    q = int(input())

    deq = deque(a)
    first = True
    initial = True
    for query in readlines(q):
        t = next(query)
        if t == '1':
            if first:
                continue
            initial = not initial
            continue

        f, c = query
        if initial:
            if f == '1':
                deq.appendleft(c)
            else:
                deq.append(c)
        else:
            if f == '1':
                deq.append(c)
            else:
                deq.appendleft(c)

        first = False

    print("".join(deq) if initial else "".join(reversed(deq)))



main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=list(input())
q=int(input())
start=[]
owari=[]
r=1
for i in range(q):
    data=input().split()
    if(len(data)==1):
        r*=-1
    elif(data[1]=="1"):
        if(r==1):
            start.append(data[2])
        else:
            owari.append(data[2])
    else:
        if(r==1):
            owari.append(data[2])
        else:
            start.append(data[2])

if(r==1):
    start.reverse()
    print(''.join(start),end='')
    print(''.join(s),end='')
    print(''.join(owari),end='')
else:
    owari.reverse()
    s.reverse()
    print(''.join(owari),end='')
    print(''.join(s),end='')
    print(''.join(start),end='')

Submission status: Accepted

Solution code:
from collections import deque


def main():
    S = list(input())
    D = deque(S)
    Q = int(input())
    T = [tuple(map(str, input().split())) for _ in range(Q)]
    r = False

    for t in T:
        if t[0] == "1" and r == False:
            r = True
        elif t[0] == "1" and r == True:
            r = False
        else:
            if t[1] == "1" and r == False:
                D.appendleft(t[2])
            elif t[1] == "1" and r == True:
                D.append(t[2])
            elif t[1] == "2" and r == False:
                D.append(t[2])
            elif t[1] == "2" and r == True:
                D.appendleft(t[2])

    if r == False:
        print("".join(D))
    else:
        D.reverse()
        print("".join(D))


if __name__ == "__main__":
    main()