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()
q = int(input())

forward = ""
reverse = ""

rev = 0
for _ in range(q):
    query = list(input().split())
    t = int(query[0])
    if t == 1:
        rev = rev^1
    else:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if rev == 0:
                forward += c
            else:
                reverse += c
        else:
            if rev == 0:
                reverse += c
            else:
                forward += c

ans = ""
if rev == 0:
    for i in reversed(range(len(forward))):
        ans += forward[i]
    ans += s
    ans += reverse
else:
    for i in reversed(range(len(reverse))):
        ans += reverse[i]
    for i in reversed(range(len(s))):
        ans += s[i]
    ans += forward

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
s = list(input())
q = int(input())
d = deque(s)
rot = 0
for i in range(q):
    que = list(map(str,input().split()))
    if len(que) == 1:
        rot += 1
    else:
        num = int(que[1])
        cha = que[2]
        if num == 1 and rot % 2 == 0:
            d.appendleft(cha)
        elif num == 1 and rot % 2 == 1:
            d.append(cha)
        elif num == 2 and rot % 2 == 0:
            d.append(cha)
        elif num == 2 and rot % 2 == 1:
            d.appendleft(cha)
d = list(d)
if rot % 2 == 1:
    d.reverse()
    print("".join(d))
else:
    print("".join(d))

Submission status: Accepted

Solution code:
from collections import deque
S = list(input())
s = deque(S)
Q = int(input())
t = 0
for i in range(Q):
    qi = input()
    if qi == "1":
        t += 1
    else:
        qi = list(qi)
        if (int(qi[2])+t) % 2 ==0:
            s.append(qi[4])
        else:
            s.appendleft(qi[4])

s = list(s)
if t % 2 == 1:
    ans = [[] for j in range(len(s))]
    for i in range(len(s)):
        ans[i] = s[len(s)-i-1]
else:
    ans = s

print("".join(ans))

Submission status: Accepted

Solution code:
s = input().rstrip()
q = int(input().rstrip())
f = True

a = []
z = []
for _ in range(q):
    c = input().rstrip()
    if c == "1":
        f = not f
    else:
        _, cc, ccc = c.split()
        if (cc == "1") == f:
            a.append(ccc)
        else:
            z.append(ccc)

aa = "".join(a[::-1])
zz = "".join(z)
s = aa + s + zz

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

Submission status: Accepted

Solution code:
from collections import deque

s = input()
s = deque(s)
q = int(input())
rev = 1

for _ in range(q):
    input_ = input()
    if input_ == '1':
        rev *= -1
        continue
    _, f, c = input_.split()
    if rev == 1:
        if f == '1':
            s.appendleft(c)
        else:
            s.append(c)
    else:
        if f == '1':
            s.append(c)
        else:
            s.appendleft(c)
ans = list(s)
if rev == -1:
    ans = ans[::-1]

print(''.join(ans))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def solve(inp):
    S = inp.readline().strip()
    Q = int(inp.readline().strip())
    Queries = [inp.readline().strip() for i in range(Q)]

    l = []
    r = []
    dir = False
    for Query in Queries:
        if Query == "1":
            dir = not dir
        else:
            (T, F, C) = Query.split(' ')
            if dir == (F == "1"):
                r.append(C)
            else:
                l.append(C)

    if dir:
        return "".join(reversed(r)) + "".join(reversed(S)) + "".join(l)
    else:
        return "".join(reversed(l)) + S + "".join(r)


def main():
    import sys
    result = solve(sys.stdin)
    if result is not None and result != '':
        print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

from collections import deque
s = deque(list(input()))
q = int(input())

reverse_flag = 1

for i in range(q):
    query = input().split()
    if query[0] == "1":
        reverse_flag *= -1
    else:
        if query[1] == "1":
            if reverse_flag == 1:
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if reverse_flag == 1:
                s.append(query[2])
            else:
                s.appendleft(query[2])


if reverse_flag == -1:
    s.reverse()

print("".join(s))

Submission status: Accepted

Solution code:
import sys
import math
import bisect
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def I(): return int(sys.stdin.readline())


def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res


def IR(n):
    return [I() for i in range(n)]


# A
def A():
    S = input()
    if S == 'AAA' or S == 'BBB':
        print('No')
    else:
        print('Yes')
    return


# B
def B():
    N, A, B = LI()
    ans = N // (A + B) * A + min(N % (A + B), A)
    print(ans)
    return


# C
def C():
    A, B = LI()
    for i in range(1, 1001):
        if i * 8 // 100 == A and i * 10 // 100 == B:
            print(i)
            return
    print(-1)
    return


# D
def D():
    s = S()
    d = deque(s)
    q = I()
    f = 1
    for i in range(q):
        query = input().split()
        if len(query) == 1:
            f *= -1
        else:
            if query[1] == '1':
                if f == 1:
                    d.appendleft(query[2])
                else:
                    d.append(query[2])
            else:
                if f == 1:
                    d.append(query[2])
                else:
                    d.appendleft(query[2])
    ans = ''.join(d)
    if f == -1:
        ans = ans[::-1]
    print(ans)
    return


# E
def E():
    return


# F
def F():
    return


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

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
hanten = 0
ans = deque(s)
for _ in range(q):
    a = list(input().split())
    if a[0] == "1":
        hanten += 1
    else:
        if hanten % 2 == 0:
            if a[1] == "1":
                ans.appendleft(a[2])
            if a[1] == "2":
                ans.append(a[2])
        else:
            if a[1] == "1":
                ans.append(a[2])
            if a[1] == "2":
                ans.appendleft(a[2])
if hanten % 2 == 0:
    print(*ans, sep="")
else:
    ans.reverse()
    print(*ans, sep="")

Submission status: Accepted

Solution code:

S=input()
Q=int(input())
qls=[list(map(str,input().split())) for _ in range(Q)]
N=len(S)
res=['']*(2*Q+N)
res[Q:Q+N]=S
ins = [Q+N, Q-1]
flg=0 #0:right, 1:left
for q in qls:
    if q[0]=='1':
        flg = (flg+1)%2
    else:
        x = (flg+int(q[1]))%2
        idx = ins[x]
        res[idx] = q[2]
        if x==0:
            ins[x]+=1
        else:
            ins[x]-=1
if flg==0:
    print(''.join(res))
else:
    print(''.join(res[::-1]))

Submission status: Accepted

Solution code:
from collections import deque

s=input()
q=int(input())
d=deque(s)

cnt=0
for _ in range(q):
    query=list(input().split())
    if query[0]=='1':
        cnt=(cnt+1)%2
    else:
        if query[1]=='1':
            if cnt==1:
                d.append(query[2])
            else:
                d.appendleft(query[2])
        else:
            if cnt==1:
                d.appendleft(query[2])
            else:
                d.append(query[2])
                
ans=list(d)
                
if cnt==1:
    print(*ans[::-1],sep='')
else:
    print(*ans,sep='')

Submission status: Accepted

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

s=input()
q=int(input())
query=[input().split() for i in range(q)]
now=0
d=deque(s)
# print(d)
for i in range(q):
    if query[i][0]=="1":
        now+=1
    else:
        if now%2==0:
            if query[i][1]=="1":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
        else:
            if query[i][1]=="1":
                d.append(query[i][2])
            else:
                d.appendleft(query[i][2])
if now%2==1:
    d.reverse()
print("".join(d))

Submission status: Accepted

Solution code:
#D
S=input()
Q=int(input())
t=1 # 1:正順,0:逆順
fro=''
bac=''
for _ in range(Q):
    q=input()
    if q[0]=='1':
        t=(t+1)%2
    else:
        __,f,c=q.split()
        if f=='1':
            if t==1:
                fro=c+fro
            else:
                bac=bac+c
        else:
            if t==1:
                bac=bac+c
            else:
                fro=c+fro
    # print(S)
ans=fro+S+bac
print(ans if t else ans[::-1])

Submission status: Accepted

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

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

    def appendBottom(self,v):
        self.next = v
        v.prev = self
        return v

    def printRecursively(self):
        current = self
        s = ""
        while True:
            if current == None:
                break
            s += current.value
            current = current.next
        print(s)

    def printRecursivelyInvert(self):
        current = self
        s = ""
        while True:
            if current == None:
                break
            s += current.value
            current = current.prev
        print(s)

# 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():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S=next(tokens)
    Q=int(next(tokens))
    queries = []
    invert = False
    for i in range(Q):
        t = int(next(tokens))
        if t == 1:
            queries.append((t,))
        else:
            queries.append((t,int(next(tokens)),next(tokens)))
    tail = Node(S[0])
    head = tail
    for i in range(1,len(S)):
        tail = tail.appendBottom(Node(S[i]))
    for query in queries:
        if query[0] == 1:
            invert = not invert
        else:
            if query[1] == 1:
                if invert:
                    tail = tail.appendBottom(Node(query[2]))
                else:
                    head = head.insertHead(Node(query[2]))
            else:
                if not invert:
                    tail = tail.appendBottom(Node(query[2]))
                else:
                    head = head.insertHead(Node(query[2]))
    if invert:
        tail.printRecursivelyInvert()
    else:
        head.printRecursively()
    pass

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

import collections


class Solution:
    def solve(self, s, queries):
        mQueue = collections.deque([ch for ch in s])

        revCount = 0

        for query in queries:
            if query[0] == 1:
                revCount += 1
            elif query[0] == 2:
                if revCount % 2 == 0:
                    if query[1] == '1':
                        mQueue.appendleft(query[2])
                    else:
                        mQueue.append(query[2])
                else:
                    if query[1] == '1':
                        mQueue.append(query[2])
                    else:
                        mQueue.appendleft(query[2])

        if revCount % 2 == 0:
            return "".join(mQueue)
        else:
            return "".join(reversed(mQueue))


sol = Solution()

s = input().strip()
q = int(input().strip())

queries = []

for i in range(q):
    query = list(input().strip().split())
    query[0] = int(query[0])

    queries.append(query)

print(sol.solve(s, queries))

Submission status: Accepted

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

flag = False
for _ in range(Q):
    IN = list(map(str,input().split()))

    if IN[0] == "1":
        flag = not flag
        continue

    _,F,C = IN
    if flag:
        if F == "1":
            S.append(C)
        else:
            S.appendleft(C)
    else:
        if F == "1":
            S.appendleft(C)
        else:
            S.append(C)

if flag:
    print(*reversed(S),sep="")
else:
    print(*S,sep="")

Submission status: Accepted

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

import sys
input = sys.stdin.readline

qs = [list(input().split()) for _ in range(q)]

left = ''
right = ''


front = False
for i in qs:
    if(i[0] == '1'):
        front = not front
        continue

    f = int(i[1])
    c = i[2]
    if(front + f == 2):
        right = right + c
    else:
        left = left + c

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

print(ans)

Submission status: Accepted

Solution code:
S = input()
additions = [[],[]]
direction = 0
Q = int(input())
for _ in range(Q):
    query = input().split()
    T = int(query[0])
    if T == 1:
        direction ^= 1
    else:
        F = int(query[1])
        C = query[2]
        additions[(F+direction+1) % 2].append(C)
if direction == 0:
    print(''.join(additions[0][::-1]), end='')
    print(S, end='')
    print(''.join(additions[1]))
else:
    print(''.join(additions[1][::-1]), end='')
    print(S[::-1], end='')
    print(''.join(additions[0]))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
cnt = 0
mae = []
usiro = []
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        cnt += 1
    else:
        if cnt % 2 == 0:
            if int(q[1]) == 1:
                mae.append(q[2])
            else:
                usiro.append(q[2])
        else:
            if int(q[1]) == 1:
                usiro.append(q[2])
            else:
                mae.append(q[2])

mae = mae[::-1]
S = "".join(mae) + S + "".join(usiro)

if cnt % 2 == 1:
    S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
rev = False
for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        rev = not rev
    else:
        if q[1] == '1':
            if rev:
                S.append(q[2])
            else:
                S.appendleft(q[2])
        else:
            if rev:
                S.appendleft(q[2])
            else:
                S.append(q[2])
if rev:
    S.reverse()

print(''.join(S))

Submission status: Accepted

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
from inspect import currentframe
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def MI() : return map(int, input().split())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def chkprint(*args) : names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}; print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

S = input()
Q = I()

d = collections.deque()
d.append(S)

rev = 0
for i in range(Q):
    line = input().split()

    if int(line[0]) == 1:
        rev += 1
    else:
        F = int(line[1])
        C = line[2]

        if F == 1:
            if rev % 2 == 1:
                d.append(C)
            else:
                d.appendleft(C)
        else:
            if rev % 2 == 1:
                d.appendleft(C)
            else:
                d.append(C)

ans = "".join(d)

if rev % 2 == 1:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s = input()
pre = []
post = []
rev_it = 0#反転の回数
q = int(input())
for i in range(q):
    query = input()
    if query[0] == "1": rev_it += 1
    elif query[2] == "1":
        if rev_it % 2 == 0:
            pre.append(query[4])
        else:
            post.append(query[4])
    else:
        if rev_it % 2 == 0:
            post.append(query[4])
        else:
            pre.append(query[4])
ans = "".join(pre)[::-1]+s+"".join(post)
if rev_it % 2 == 0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
from collections import deque

S = deque(list(input()))
Q = int(input())
r = False
left = deque()
right = deque()
for i in range(Q):
    q = input().split()
    if q[0] == "1":
        r = not r
    else:
        f, c = q[1:]
        if f == "1":
            if r:
                right.append(c)
            else:
                left.appendleft(c)
        else:
            if r:
                left.appendleft(c)
            else:
                right.append(c)

S, left, right = map(list, (S, left, right))
left.extend(S)
left.extend(right)
print("".join(left[::-1] if r else left))

Submission status: Accepted

Solution code:
s=input()
q=int(input())
mae=''
ato=''
ten=0

for i in range(q):
  ss=input()
  if ss[0]=='1':
    ten+=1
    ten%=2
  else:
    t,f,c=ss.split()
    if f=='1':
      if ten==0:
        mae+=c
      else:
        ato+=c        
    else:
      if ten==0:
        ato+=c
      else:
        mae+=c
mae=mae[::-1]
s=mae+s+ato
if ten==1:
  s=s[::-1]
print(s)

Submission status: Accepted

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
MOD = 10**9+7
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def ifn(): return float(input()) #浮動小数点読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return map(int, input().split()) #整数map取得
def fmn(): return map(float, input().split()) #浮動小数点map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def fln(): return list(map(float, input().split())) # 浮動小数点リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離
def m_add(a,b): return (a+b) % MOD

def sieves_of_e(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

S = list(input())
d = deque(S)
Q = iin()
Query = [input().split() for _ in range(Q)]
isReversed = False

for i in range(Q):
    if Query[i][0] == '1':
        if isReversed:
            isReversed = False
        else:
            isReversed = True
    else:
        fl = 0

        if Query[i][1] == '1':
            fl = 0
            if isReversed:
                fl = 1
        else:
            fl = 1
            if isReversed:
                fl = 0
        if fl == 0:
            d.appendleft(Query[i][2])
        else:
            d.append(Query[i][2])
if isReversed:
    d.reverse()    
print(''.join(d))

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
q = int(input())
flag = 0
for i in range(q):
    query = list(input().split())
    if query[0] == "1":
        flag = (flag+1)%2
    else:
        if flag == 0:
            if query[1] == "1":
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if query[1] == "2":
                s.appendleft(query[2])
            else:
                s.append(query[2])

s = list(s)
if flag == 0:
    for i in s:
        print(i, end="")
if flag == 1:
    l = len(s)
    for i in range(l):
        print(s[l-i-1], end="")

print()

Submission status: Accepted

Solution code:
from _collections import deque
S = deque(input())
Q = int(input())
dir = 1

for i in range(Q):
    query = list(input().split())
    if len(query) == 1:
       dir *= -1
    else:
        T, f, c = query
        if int(f) == 1:
            if dir == 1:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if dir == 1:
                S.append(c)
            else:
                S.appendleft(c)
if dir == -1:
    S.reverse()
print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
S = list(input())
q = int(input())
Q = [list(input().split()) for _ in range(q)]

ds = deque(S)

flg = True
for i in Q:
  if i[0] == "1":
    flg = not flg
  else:
    if flg == True:
      if i[1] == "1":
        ds.appendleft(i[2])
      else:
        ds.append(i[2])
    else:
      if i[1] == "2":
        ds.appendleft(i[2])
      else:
        ds.append(i[2])
if flg:
  print("".join(ds))
else:
  print("".join(ds)[::-1])

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
reverse = 0
q = int(input())

for _ in range(q):
  query = input()
  if query == '1':
    reverse = 1 - reverse
  else:
    _,f,c = query.split()
    if f == '1':
      if reverse:
        s.append(c)
      else:
        s.appendleft(c)
    else:
      if reverse:
        s.appendleft(c)
      else:
        s.append(c)
        
  
if reverse:
  s.reverse()

print(''.join(s))

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

S = input().strip()
Q = int(input())
Query = []
for _ in range(Q):
    Query.append(input().strip())

rev = S[::-1]
left = ""
right = ""
count = 0
flag = 'Normal'
for value in Query:
    if value == '1':
        count += 1
    if count % 2 == 1:
        flag = 'Reverse'
    else:
        flag = 'Normal'
    if value != "1":
        if flag == 'Normal':
            if value[2] == '1':
                left += value[4]
            elif value[2] == '2':
                right += value[4]
        elif flag == 'Reverse':
            if value[2] == '1':
                right += value[4]
            elif value[2] == '2':
                left += value[4]

if flag == 'Normal':
    ans = left[::-1] + S + right
elif flag == 'Reverse':
    ans = right[::-1] + rev + left
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque

SL = list(input())
Qn = int(input())


S = deque()
for s in SL:
    S.append(s)
    
even = 1
for i in range(Qn):
    t = list(map(str,input().split()))
    if len(t)==1:
        even = even^1
    else:
        if t[1]=='1':
            if even:
                S.appendleft(t[2])
            else:
                S.append(t[2])
        else:
            if even:
                S.append(t[2])
            else:
                S.appendleft(t[2])

if even == 0:
    S = reversed(S)

print(''.join(S))

Submission status: Accepted

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

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

A = []
B = []
revFlag = False;

for _ in range(Q):
    q = input().split()

    if q[0] == '1':
        revFlag = not revFlag
        A,B = B,A
    else:
        if q[1] == '1':
            A.append(q[2])
        else:
            B.append(q[2])


if revFlag:
    A,B = B,A
    


S2 = ''.join(reversed(A)) + S + ''.join(B)

if revFlag:
    S2 = ''.join(reversed(S2))

print(S2)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q_num = int(input())
d = 0
for _ in range(q_num):
    q = input().split()
    if q[0] == '1':
        d = 1 - d
    else:
        if d == 0 and q[1] == '1' or d == 1 and q[1] == '2':
            s.appendleft(q[2])
        else:
            s.append(q[2])

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

Submission status: Accepted

Solution code:
s = input()
q =int(input())  #数値入力 「N」だけの入力のとき

count=0
add_s=[]
add_e=[]
for i in range(q):
    qi= input()
    if qi=='1':
        add_s,add_e=add_e,add_s
        count+=1
    else:
        _t,f,c=qi.split()
        if f =='1':
            add_s.append(c)
        else:
            add_e.append(c)

if count%2==0:
    print(''.join(reversed(add_s))+s+''.join(add_e))
else:
    print(''.join(reversed(add_s))+''.join(reversed(list(s)))+''.join(add_e))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverse = False
pref, suf = "", ""

for i in range(Q):
    query = list(input().split())
    if query[0] == "1":
        reverse = not reverse
    else:
        if reverse == False:
            if query[1] == "1":
                pref = query[2] + pref
            else:
                suf = suf + query[2]
        else:
            if query[1] == "1":
                suf = suf + query[2]
            else:
                pref = query[2] + pref

if reverse == False:
    ans = pref + S + suf
else:
    ans = suf[::-1] + S[::-1] + pref[::-1]

print(ans)

Submission status: Accepted

Solution code:
S=list(input())

D={}
MIN=0
MAX=len(S)-1
for i in range(len(S)):
    D[i]=S[i]
F=1
Q=int(input())
for i in range(Q):
    q=list(input().split())
    if F==1:
        if q[0]=="1":
            F*=-1
        else:
            if q[1]=="1":
                D[MIN-1]=q[2]
                MIN-=1
            else:
                D[MAX+1]=q[2]
                MAX+=1
    else:
        if q[0]=="1":
            F*=-1
        else:
            if q[1]=="2":
                D[MIN-1]=q[2]
                MIN-=1
            else:
                D[MAX+1]=q[2]
                MAX+=1
ans=""

for i in range(MIN,MAX+1):
    ans+=D[i]
if F==1:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:

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

front = []
back = []
cnt = 0
for i in range(q):
    t = input()
    if t[0] == '1':
        cnt ^= 1
    else:
        f = 1 if int(t[2]) == 1 else 0
        c = t[4]
        if cnt ^ f:
            front.append(c)
        else:
            back.append(c)

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

Submission status: Accepted

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

s=input().rstrip('\n')
n=int(input())

s_f=[]
s_b=[]
muki=True

for i in range(n):
    line=input().rstrip('\n')
    #print(line)
    if line == "1":
        muki=not muki
        continue

    q,f,c=map(str, line.split())
    q,f=int(q),int(f)
    if (f == 1 and muki) or (f == 2 and not muki):
        s_f.append(c)
    else:
        s_b.append(c)

ans = "".join(s_f[::-1]) + s + "".join(s_b)

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

Submission status: Accepted

Solution code:
from collections import deque
S = list(input())
Q = int(input())
ans = deque(S)
a = 1
for i in range(Q):
    q = input()
    if q == '1':
        a *= -1
    else:
        d, f, c = q.split()
        if f == '1':
            if a == 1:
                ans.appendleft(c)
            else:
                ans.append(c)
        else:
            if a == 1:
                ans.append(c)
            else:
                ans.appendleft(c)

ans = ''.join(list(ans))
if a == -1:
    ans = ans[::-1] 

print(ans)

Submission status: Accepted

Solution code:


s = input()
queNum = int(input())


reverse_cnt = 0
begin_lst = []
end_lst = []
for _ in range(queNum):
    query = input()

    if query == '1':
        reverse_cnt += 1
        begin_lst, end_lst = end_lst, begin_lst
    else:
        num, fi, ci = query.split()
        if fi == '1':
            begin_lst.append(ci)
        else:
            end_lst.append(ci)
    
if reverse_cnt % 2 == 0:
    pass
else:
    s = s[::-1]

print(''.join(reversed(begin_lst)) + s + ''.join(end_lst))

Submission status: Accepted

Solution code:
from collections import deque


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


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import collections
s = collections.deque(list(input()))
q = int(input())
cnt = 0
for i in range(q):
    fc = input().split()
    if fc[0] == '1':
        cnt += 1
    else:
        if cnt == 1:
            fc[1] = '1' if fc[1] == '2' else '2'
        if fc[1] == '1':
            s.appendleft(fc[2])
        else:
            s.append(fc[2])
    cnt %= 2
s = list(s)
if cnt == 1:
    s.reverse()
print(*s, sep='')