Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
S = input()
Q = int(input())
flag = True
f = ''
b = ''
for i in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag = not flag
    else:
        if flag:
            if q[1] == '1':
                f = q[2] + f
            else:
                b += q[2]
        else:
            if q[1] == '1':
                b += q[2]
            else:
                f = q[2] + f
S = f + S + b
if not flag:
    S = S[::-1]
print(S)

Submission status: Accepted

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

p = 0

for i in range(Q):
    query = input().split()
    if query[0]=='1':
        p ^= 1
    else:
        f = int(query[1])-1
        c = query[2]
        if p:
            f ^= 1
        if f:
            S.append(c)
        else:
            S.appendleft(c)

if p:
    print(*reversed(S), sep='')
else:
    print(*S, sep='')

Submission status: Accepted

Solution code:
s=input()
n=int(input())
a,b=[],[]
p=0
for i in range(n):
    l=input().split()
    if len(l)==1:
        p+=1
    else:
        k=int(l[1])
        if k%2==p%2:
            b+=[l[2]]
        else:
            a+=[l[2]]
if p%2==1:
    s=s[::-1]
    a,b=b,a
print(("".join(a)[::-1])+s+"".join(b))

Submission status: Accepted

Solution code:
#D
S = list(str(input()))
Q = int(input())
T = 0
r1 = [] #T%2 == 0
r2 = [] #T%2 == 1

for i in range(Q):
    q = list(map(str,input().split()))
    if len(q) == 1:
        T+=1
    else:
        f,c = q[1],q[2]
        if f == "1":
            if T%2 == 0:
                r2.append(c)
            else:
                r1.append(c)
            
        else:
            if T%2 == 0:
                r1.append(c)
            else:
                r2.append(c)
        
if T%2 == 0:
    r2l = len(r2)
    ans = ""
    for i in range(r2l):
        ans+=r2[r2l-i-1]
    for s in S:
        ans+=s
    for r in r1:
        ans+=r
        
else:
    ans = ""
    r1l = len(r1)
    sl = len(S)
    for i in range(r1l):
        ans+=r1[r1l-i-1]
    for i in range(sl):
        ans+=S[sl-i-1]
    for r in r2:
        ans+=r
        
print(ans)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
flag=1
a=""
b=""
for _ in range(q):
    query=input().split()
    if query[0]=="1":
        flag*=-1
    else:
        if flag==1:
            if query[1]=="1":
                a+=query[2]
            else:
                b+=query[2]
        else:
            if query[1]=="1":
                b+=query[2]
            else:
                a+=query[2]
if flag==1:
    print(a[::-1]+s+b)
else:
    print(b[::-1]+s[::-1]+a)

Submission status: Accepted

Solution code:
import sys
from collections import deque

s = sys.stdin.readline().rstrip()
Q = int(sys.stdin.readline().rstrip())
Q = [sys.stdin.readline().split() for _ in range(Q)]

def main():
    res = deque(s)
    flag = 0
    cnt = 0
    for q in Q:
        if q[0] == '1':
            flag ^= 1
            cnt += 1
        else:
            f, c = q[1:]
            f = int(f) - 1
            if flag ^ f:
                res.append(c)
            else:
                res.appendleft(c)
    
    res = ''.join(list(res))
    if cnt & 1:
        res = res[::-1]
    return res

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

Submission status: Accepted

Solution code:
import collections
s = input()
head = []
tail = []
is_reveresed = False
q = int(input())
for i in range(q):
    query = input()
    if query[0] == '1':
        is_reveresed = not is_reveresed
    else:
        f = query[2]
        c = query[4]
        if is_reveresed:
            if f == '1':
                tail.append(c)
            else:
                head.append(c)
        else:
            if f == '1':
                head.append(c)
            else:
                tail.append(c)
if is_reveresed:
    print('{}{}{}'.format(''.join(tail[::-1]), s[::-1], ''.join(head)))
else:
    print('{}{}{}'.format(''.join(head[::-1]), s, ''.join(tail)))

Submission status: Accepted

Solution code:
from collections import deque

s = input()
d = deque()
for i in s:
    d.append(i)
q = int(input())

state = 0


for i in range(q):
    In = input()
    if In == '1':
        state = (state + 1)%2
    else:
        t,f,c = In.split()
        k = (f=='2')
        if (k+state)%2==0:
            d.appendleft(c)
        else:
            d.append(c)
if state==0:
    print(*list(d),sep='')
else:
    t = list(d)[::-1]
    print(*t, sep='')

Submission status: Accepted

Solution code:
import sys
def main():
    input = sys.stdin.readline
    S = input().rstrip()
    Q = int(input())
    prefix = []
    suffix = []
    reverse = False
    for _ in range(Q):
        q = input().rstrip().split()
        if q[0] == '1': reverse = not reverse
        else:
            is_pre = (q[1] == '1' and reverse == False) or (q[1] == '2' and reverse)
            if is_pre:
                prefix.append(q[2])
            else:
                suffix.append(q[2])

    if reverse:
        S = S[::-1]
        prefix, suffix = suffix, prefix
    prefix.reverse()
    print(''.join(prefix) + S + ''.join(suffix))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

dq = deque(list(input()))

Q = int(input())

reverse_count = 0

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse_count = 1 - reverse_count
    else:
        F = query[1]
        C = query[2]
        if F == "1":
            if reverse_count == 0:
                dq.appendleft(C)
            else:
                dq.append(C)
        else:
            if reverse_count == 0:
                dq.append(C)
            else:
                dq.appendleft(C)

if reverse_count == 0:
    print("".join(dq))
else:
    print("".join(reversed(dq)))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(20000)

def input():
    return readline().rstrip()

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

prefix = []
suffix = []
flip = 0
for i in range(Q):
    T = input()
    if T == "1":
        prefix, suffix = suffix, prefix
        flip = 1 - flip
    else: # T == 2
        _, f, c = T.split()
        if f == "1":
            # 先頭に c 追加
            prefix.append(c)
        elif f == "2":
            # 末尾に c 追加
            suffix.append(c)

if flip == 1:
    S = S[::-1]
print("".join(prefix[::-1]), end="")
print(S, end="")
print("".join(suffix))

Submission status: Accepted

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

S_tmp = str(S)
Sl = []
Sr = []
flg_inv = 0
for i, qi in enumerate(q):
    if qi[0] == '1':
        flg_inv = 1 - flg_inv
    else:
        Fi = int(qi[1])
        Ci = qi[2]
        if (Fi == 1 and flg_inv == 0) or (Fi == 2 and flg_inv == 1):
            Sl.append(Ci)
        else:
            Sr.append(Ci)

# print("#", Sl, S, Sr, flg_inv)
if flg_inv == 0:
    ans = ''.join(Sl[::-1] + S + Sr)
else:
    ans = ''.join(Sr[::-1] + S[::-1] + Sl)

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, stdout
from collections import deque

s = raw_input().strip()
q = input()
inp = stdin.readlines()

Q = deque(list(s))
rev = 0
for line in inp:
    x = line.strip().split()
    if x[0] == '1':
        rev ^= 3
    else:
        f = int(x[1])
        ch = x[2]
        f ^= rev
        if f == 1:
            Q.appendleft(ch)
        else:
            Q.append(ch)

l = []
while Q:
    if rev == 0: l.append(Q.popleft())
    else: l.append(Q.pop())

print ''.join(l)

Submission status: Accepted

Solution code:
from collections import deque
a = list(input())
a = deque(a)
re = 0
for i in range(int(input())):
  b = input().split()
  if b[0] == "1":
    re = (re+1)%2
  else:
    if (re and b[1] == "2") or (not(re) and b[1] == "1"):
      a.appendleft(b[2])
    else:
      a.append(b[2])
a = list(a)
if re:
  a = a[::-1]
print("".join(a))

Submission status: Accepted

Solution code:
from collections import deque

Ss = input()
Q = int(input())

anss = deque(Ss)
isRev = False
for _ in range(Q):
    vs = input().split()
    if vs[0] == '1':
        isRev = not isRev
    else:
        F, C = vs[1], vs[2]
        if F == '1':
            if isRev:
                anss.append(C)
            else:
                anss.appendleft(C)
        else:
            if isRev:
                anss.appendleft(C)
            else:
                anss.append(C)

#print('anss:', anss)
#print('isRev:', isRev)
anss = ''.join(anss)
if isRev:
    anss = anss[::-1]

print(anss)

Submission status: Accepted

Solution code:
from collections import deque

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

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':
      if inv:
        S.append(c)
      else:
        S.appendleft(c)
    else:
      if inv:
        S.appendleft(c)
      else:
        S.append(c)

S = list(S)
if inv:
  print(''.join(S[::-1]))
else:
  print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
def main():
    s = list(input().rstrip())
    s = deque(s)
    q = int(input())
    f = True
    for _ in range(q):
        a = list(map(str, input().rstrip().split()))
        if a[0] == "1":
            if f:
                f = False
            else:
                f = True
        else:
            if a[1] == "1":
                if f:
                    s.appendleft(a[2])
                else:
                    s.append(a[2])
            else:
                if f:
                    s.append(a[2])
                else:
                    s.appendleft(a[2])
    if f:
        for c in s:
            print(c, end="")
    else:
        for c in reversed(s):
            print(c, end="")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
s=input()
q=int(input())
flg=True
s1=deque()
s2=deque()
for _ in range(q):
    t=list(input().split())
    if t[0]=='1':
        flg=not flg
    else:
        if t[1]=='1':
            if flg:
                s1.appendleft(t[2])
            else:
                s2.append(t[2])
        else:
            if flg:
                s2.append(t[2])
            else:
                s1.appendleft(t[2])
if flg:
    print(''.join(s1)+s+''.join(s2))
else:
    print(''.join(s2)[::-1]+s[::-1]+''.join(s1)[::-1])

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque

if __name__ == "__main__":
    S = input()
    Q = int(input())
    mode = 0
    d = deque()
    for s in S:
        d.append(s)
    
    for i in range(Q):
        arr = list(input().split())
        if len(arr) == 1:
            mode ^= 1
        else:
            if arr[1] == '1':
                if mode == 0:
                    # print(i)
                    d.appendleft(arr[2])
                else:
                    d.append(arr[2])
            else:
                if mode == 0:
                    d.append(arr[2])
                else:
                    d.appendleft(arr[2])
        # print(mode, d);
    ans = ""
    while len(d):
        if mode == 0:
            ans += d.popleft()
        else:
            ans += d.pop()
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from bisect import bisect_left
import sys,math,itertools,pprint,fractions,time
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

s = input()
n = len(s)
Q = inp()
q = deque()
for i in s:
    q.append(i)
rev = 0
for _ in range(Q):
    tmp = input().split()
    if tmp[0] == '1':
        rev ^= 1
    else:
        if tmp[1] == '1':
            if rev==0: q.appendleft(tmp[2])
            else: q.append(tmp[2])
        else:
            if rev==0: q.append(tmp[2])
            else: q.appendleft(tmp[2])
    # print(_,q)
# print(q)
res = list(q)
if rev: res = res[::-1]
# print(res)
print(''.join(res))

Submission status: Accepted

Solution code:
from collections import deque

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

inv_cnt = 0


li = []
r = []

for i in range(q):
    d = [x for x in input().split()]
    if d[0] == '1':
        inv_cnt += 1
        inv_cnt %= 2
    else:
        if inv_cnt == 0:
            if d[1] == '1':
                li.append(d[2])
            else:
                r.append(d[2])
        else:
            if d[1] == '1':
                r.append(d[2])
            else:
                li.append(d[2])

if inv_cnt == 0:
    print(''.join(li[::-1]) + s + ''.join(r))
else:
    print(''.join(r[::-1]) + s[::-1] + ''.join(li))

Submission status: Accepted

Solution code:
s=input()
a=[]
b=[]
d=0
for i in range(int(input())):
  q=input()
  if q=="1":d^=1
  else:
    _,f,c=q.split()
    f=int(f)-1
    if d==0:
      if f==0:a.append(c)
      else:b.append(c)
    else:
      if f==1:a.append(c)
      else:b.append(c)
if d==0:print("".join(a)[::-1]+s+"".join(b))
else:print("".join(b)[::-1]+s[::-1]+"".join(a))

Submission status: Accepted

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

Submission status: Accepted

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

state = 0
start = []
end = []

for i in range(Q):
    x = list(input().split())
    if x[0] == "1":
        state = (state+1)%2
    else:
        x[1] = int(x[1])
        if (state + x[1]) % 2 == 1:
            start.append(x[2])
        else:
            end.append(x[2])

if state == 0:
    start.reverse()
    ans = start + S + end
    
else:
    end.reverse()
    S.reverse()
    ans = end + S + start

print("".join(ans))

Submission status: Accepted

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    s=SI()
    q=II()
    aa=deque(ord(c) for c in s)
    rev=0
    for _ in range(q):
        t=SI().split()
        if t[0]=="1":rev=1-rev
        else:
            f=int(t[1])-1
            if f^rev:aa.append(ord(t[2]))
            else:aa.appendleft(ord(t[2]))
    if rev:aa.reverse()
    print("".join(chr(a) for a in aa))

main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    # 10**6
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     # 10**6
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):   
    return fs[n] * invs[n-k] * invs[k] % modp

#

class UnionFindEx:
    def __init__(self, size):
        #正なら根の番号、負ならグループサイズ
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   #負なら根
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    #サイズ更新
        self.roots[r2] = r1
        return True

def main():
    S = deque(input())
    right = True
    for _ in range(ii()):
        q = input()
        if len(q) == 1:
            right ^= 1
        else:
            t, f, c = q.split()
            if right ^ (f=='1'):
                S.append(c)
            else:
                S.appendleft(c)
    if not right:
        S.reverse()
    print(*S, sep='')


main()

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    S = input()
    Q = int(input())
    S = deque(S)
    flag = False
    for query in range(Q):
        a, *bc = (i for i in input().split())
        # print(a, bc)
        if a == "1":
            flag = not(flag)
        else:
            if flag:
                if bc[0] == "1":
                    S.append(bc[1])
                elif bc[0] == "2":
                    S.appendleft(bc[1])
            else:
                if bc[0] == "1":
                    S.appendleft(bc[1])
                elif bc[0] == "2":
                    S.append(bc[1])
    S = "".join(S)
    if flag:
        S = S[::-1]
    print(S)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

import collections


def prepare_str(s, queries):
    flip = False
    dq = collections.deque(s)
    for qid, fc in queries:
        if qid == 1:
            flip = not flip
        else:
            f = int(fc[0])
            c = fc[1]
            if flip:
                f = 3 - f
            if f == 1:
                dq.appendleft(c)
            else:
                dq.append(c)
    if flip:
        res = "".join(reversed(dq))
    else:
        res = "".join(dq)
    return res


def main():
    s = input()
    q = int(input())
    queries = []
    for _ in range(q):
        ps = input().split()
        qid = int(ps[0])
        fc = tuple(ps[1:])
        queries.append((qid, fc))
    res = prepare_str(s, queries)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

from collections import deque
s = deque(S)

mode = True
for query in queries:
    if len(query) == 1:
        mode = False if mode else True
        continue
    f = int(query[1])
    c = query[2]
    if mode:
        if f == 1:
            s.appendleft(c)
        else:
            s.append(c)
    else:
        if f == 2:
            s.appendleft(c)
        else:
            s.append(c)

if mode:
    print(*s, sep='')
else:
    print(*list(s)[::-1], sep='')

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    q = deque()
    s = S()
    for c in s:
        q.append(c)
    Q = I()
    backward = False
    for _ in range(Q):
        query = LS()
        if int(query[0]) == 1:
            backward = not backward
            continue
        _, f, c = query
        f = int(f)
        if backward and f == 1:
            # ushiro ni tsuika
            q.append(c)
        if backward and f == 2:
            # mae ni tsuika
            q.appendleft(c)
        if not backward and f == 1:
            # mae
            q.appendleft(c)
        if not backward and f == 2:
            # ushiro
            q.append(c)
    if not backward:
        ans = ''.join(q)
    else:
        q.reverse()
        ans = ''.join(q)
    print(ans)
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

rev = 0
s = input()
queue = deque()
for c in s:
    queue.append(c)

q = int(input())
for i in range(q):
    cs = input().split()
    if cs[0] == '1':
        rev = 1 - rev
    else:
        if ((cs[1] == '1' and rev == 0) or (cs[1] == '2' and rev == 1)):
            queue.appendleft(cs[2])
        else:
            queue.append(cs[2])
r = ''.join(queue)
if rev == 1:
    print(r[::-1])
else:
    print(r)

Submission status: Accepted

Solution code:
from collections import deque

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

asc = True
for _ in range(q):
    qs = input()
    if qs[0]=="1":
        asc = not asc
        continue

    _,f,c = qs.split()
    if f=="1":
        if asc:
            s.appendleft(c)
        else:
            s.append(c)
    else:
        if asc:
            s.append(c)
        else:
            s.appendleft(c)

if not asc:
    s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
import sys, math
sys.setrecursionlimit(10**5 + 10)

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

def main():
    S = input()
    Q = int(input())
    f = 1
    m = ""
    u = ""
    for k in range(Q):
        q = list(input().split())
        if q[0] == "1":
            f *= -1
        else:
            if f == 1:
                if q[1] == "1":
                    m = q[2] + m
                else:
                    u = u + q[2]
            else:
                if q[1] == "1":
                    u = u + q[2]
                else:
                    m = q[2] + m
    if f == 1:
        print(m+S+u)
    else:
        print(u[::-1]+S[::-1]+m[::-1])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return "".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

s = input().split()
q = onem()

de = cl.deque(s)

co = 0

for i in range(q):
    po = input().split()
    if len(po) == 1:
        co = co ^ 1
    else:
        f = int(po[1])
        if co == 0:
            if f == 1:
                de.appendleft(po[2])
            else:
                de.append(po[2])

        else:
            if f == 1:
                de.append(po[2])
            else:
                de.appendleft(po[2])

if co == 0:
    print(jo(de))
else:
    print(jo(de)[::-1])

Submission status: Accepted

Solution code:
S = input()
revFlg = False
Q = int(input())
first_append = ""
last_append = ""
for i in range(Q):
    query = input().split()
    if query[0] == "1":
        revFlg = not revFlg
    else:
        if query[1] == "1":
            if revFlg:
                last_append = last_append+query[2]
            else:
                first_append = query[2]+first_append

        else:
            if revFlg:
                first_append = query[2]+first_append
            else:
                last_append = last_append+query[2]

ans = first_append+S+last_append
if revFlg:
    print(ans[::-1])
else:
    print(ans)

Submission status: Accepted

Solution code:
S = input()

Q = int(input())

from collections import deque
ans = deque()

for s in S:
    ans.append(s)

rev = False
for _ in range(Q):
    q = input().split()
    if q[0] == "1":
        rev = not rev
    else:
        is_right = (q[1] =="2") ^ rev
        if is_right:
            for s in q[2]:
                ans.append(s)
        else:
            for s in q[2]:
                ans.appendleft(s)
if rev:
    ans.reverse()
print("".join(ans))

Submission status: Accepted

Solution code:
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

s = input()
q = int(input())
qqq = list(map(lambda s: s.strip(), sys.stdin.readlines()))

que = deque()

que.append(s)

rev = False

for a in qqq:
    b = a.split()

    if len(b) == 1:
        rev = not rev
    else:
        front = b[1] == "1"
        t = b[2] if not rev else "".join(reversed(b[2]))

        if front == (not rev):
            que.appendleft(t)
        else:
            que.append(t)

if rev:
    print("".join(reversed("".join(que))))
else:
    print("".join(que))

Submission status: Accepted

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

s = str(input().rstrip())
ans = deque(list(s))

f = True
q = int(input())
for i in range(q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        if f:
            f = False
        else:
            f = True
    else:
        f_i = int(q[1])
        if f_i == 1:
            if f:
                ans.appendleft(q[2])
            else:
                ans.append(q[2])
        else:
            if f:
                ans.append(q[2])
            else:
                ans.appendleft(q[2])
ans = list(ans)
if f:
    print("".join(ans))
else:
    print("".join(ans[::-1]))

Submission status: Accepted

Solution code:
from collections import deque

def main():
    S = input()
    Q = int(input())
    cnt = 0
    dS = deque(S)
    for _ in range(Q):
        query = input()
        if query[0] == '1':
            cnt += 1
        else:
            A, F, C = query.split()
            if cnt % 2 == 0:
                if F == '1':
                    dS.appendleft(C)
                else:
                    dS.append(C)
            else:
                if F == '1':
                    dS.append(C)
                else:
                    dS.appendleft(C)

    if cnt % 2 == 0:
        print(*dS, sep ="")
    else:
        dS.reverse()
        print(*dS, sep ="")





if __name__ == "__main__":
    main()

Submission status: Accepted

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

a = []
b = []
d = 1

for q in query:
    if q[0] == '1':
        d *= -1
    else:
        if q[1] == '1':
            if d == 1:
                a.append(q[2])
            else:
                b.append(q[2])
        else:
            if d == 1:
                b.append(q[2])
            else:
                a.append(q[2])

if d == 1:
    ans = list(reversed(a)) + s + b
else:
    ans = list(reversed(b)) + list(reversed(s)) + a

print(''.join(ans))