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())
I = 0
L = ''
R = ''
for i in range(Q):
    Query = input().split()
    if Query[0] == "1":
        I = I + 1
    else:
        if (int(Query[1]) + I) % 2 == 0:
            R = R + Query[2]
        else:
            L = L + Query[2]
L = L[::-1]
S = L + S + R

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

Submission status: Accepted

Solution code:
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    S = input().strip()
    Q = int(input())

    rev = 0
    sdeq = collections.deque(list(S))

    for i in range(Q):
        A = [x for x in input().split()]
        if len(A) == 1:
            rev = (rev + 1) % 2
        else:
            f = int(A[1])
            c = A[2]
            if f == 1:
                if rev == 0:
                    sdeq.appendleft(c)
                else:
                    sdeq.append(c)
            else:
                if rev == 0:
                    sdeq.append(c)
                else:
                    sdeq.appendleft(c)

    if rev == 0:
        print("".join(list(sdeq)))
    else:
        print("".join(list(sdeq)[::-1]))
    print()



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#D
#入力
S=input()
Q=int(input())
#Query=[]
#for i in range(Q):
#    Query.append(list(input().split()))
    
    
#処理
t=0
b=""
a=""
for j in range(Q):
    i=list(input().split())
    if i[0]=="1":
        t+=1
    elif i[0]=="2":
        if t%2==0:
            if i[1]=="1":
                b=i[2]+b
            elif i[1]=="2":
                a=a+i[2]
        elif t%2==1:
            if i[1]=="1":
                a=a+i[2]
            elif i[1]=="2":
                b=i[2]+b
if t%2==0:
    print(b+S+a)
    
else:
    print(a[::-1]+S[::-1]+b[::-1])

Submission status: Accepted

Solution code:
from sys import stdin

def main():
    S = list(stdin.readline().rstrip())
    Q = int(input())
    rev = 0
    head = []
    tail = []

    for _ in range(Q):
        q = stdin.readline()
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    head.append(q[4])
                else:
                    tail.append(q[4])
            else:
                if rev ==0:
                    tail.append(q[4])
                else:
                    head.append(q[4])

    if rev == 0:
        ans = "".join(head[::-1] + S + tail)
    else:
        ans = "".join(tail[::-1] + S[::-1] + head)
    
    print(ans)
    """
    S = stdin.readline().rstrip() 
    Q = int(input())
    rev = 0
    for _ in range(Q):
        q = stdin.readline()

        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    S = "{}{}".format(q[4], S)
                else:
                    S = "{}{}".format(S, q[4])
            else:
                if rev ==0:
                    S = "{}{}".format(S, q[4])
                else:
                    S = "{}{}".format(q[4], S)
    if rev == 1:
        print(S[::-1])
    else:
        print(S)
    """
main()

Submission status: Accepted

Solution code:
from collections import deque
def main():
    S = input()
    S = deque([c for c in S])
    Q = int(input())
    rvCt = 0

    for _ in range(Q):
        query = input().split()
        if query[0] == '1':
            rvCt += 1
        else:
            doReverse = False
            if rvCt % 2 == 1:
                doReverse = True

            if query[1] == '1':
                if doReverse:
                    S.append(query[2])
                else:
                    S.appendleft(query[2])
            else:
                if doReverse:
                    S.appendleft(query[2])
                else:
                    S.append(query[2])

    if rvCt % 2 == 1:
        S.reverse()
    
    print(''.join(S))

main()

Submission status: Accepted

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

Ques = []
nReverse = 0
nAddFront= 0
nAddBack = 0
for i in range(nQ):
  line = input().split()
  if int(line[0]) == 1:
    nReverse += 1
    Ques.append([int(line[0])])
  else:
    if int(line[1]) == 1:
      if nReverse % 2 == 0:
        nAddFront += 1
      else:
        nAddBack  += 1
    else:
      if nReverse % 2 == 0:
        nAddBack += 1
      else:
        nAddFront  += 1      
    Ques.append([int(line[0]),int(line[1]),line[2]])

#print(nAddFront,nAddBack)
SS = [-1] * (nAddFront + len(S) + nAddBack)
for i in range(len(S)):
   SS[nAddFront + i] = S[i]

left = nAddFront -1
right= nAddFront + len(S)

nReverseLoc = 0
for iQ in Ques:
   if iQ[0] == 1:
      nReverseLoc += 1
   if iQ[0] == 2:
    if int(iQ[1]) == 1:
      if nReverseLoc % 2 == 0:
        #nAddFront += 1
        SS[left] = iQ[2]
        left -= 1
      else:
        SS[right] = iQ[2]
        right += 1
    else:
      if nReverseLoc % 2 == 0:
        SS[right] = iQ[2]
        right += 1
      else:
        SS[left] = iQ[2]
        left  -= 1 

S = "".join(SS)
if nReverse % 2 == 1:
  S = S[::-1]
  
print(S)

Submission status: Accepted

Solution code:
s = str(input())
q = int(input())
g = 0
A = ""
B = ""
for i in range(q):
  query = input()
  if (query == "1"):
    g = 1-g
  else:
    queries = query.split()
    f = int(queries[1])-1
    c = queries[2]
    if (g == f):
      B = "".join([B,c])
    else:
      A = "".join([A,c])
s = "".join(["".join(list(B)[::-1]),s,A])
if (g == 1):
  s = "".join(list(s)[::-1])
print(s)

Submission status: Accepted

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

reverse = False

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

queries = []
for i in range(q):
    queries.append(input())

prefix = []
suffix = []

for query in queries:
    qs = query.split()
    if qs[0] == '1':
        reverse = not reverse
    elif qs[0] == '2':
        if qs[1] == '1':
            if reverse:
                suffix.append(qs[2])
            else:
                prefix.append(qs[2])
        elif qs[1] == '2':
            if reverse:
                prefix.append(qs[2])
            else:
                suffix.append(qs[2])

if reverse:
    print (''.join(suffix[::-1]) + s[::-1] + ''.join(prefix))
else:
    print (''.join(prefix[::-1]) + s + ''.join(suffix))

Submission status: Accepted

Solution code:
from collections import deque
s= input()
q= int(input())
inv = False
head = deque()
tail = deque()
for i in range(q):
    p = input()
    if p[0]=='1':
        if inv:inv=False
        else:inv=True
    elif p[0]=='2':
        _,f,c= p.split()
        if f=='1':
            if inv:tail.append(c)
            else:head.appendleft(c)
        elif f=='2':
            if inv:head.appendleft(c)
            else:tail.append(c)
news = ''.join(head) + s + ''.join(tail)
if inv:print(news[::-1])
else:print(news)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
trans=0
aft=""
bef=""
for i in range(q):
  que=input()
  if que[0]=="1":
    trans=1-trans
  else:
    if (que[2]=="1" and trans==0) or (que[2]=="2" and trans==1):
      bef=bef+que[4]
    else:
      aft=aft+que[4]
if trans==0:
  print("".join(bef[::-1])+s+"".join(aft))
else:
  print("".join(aft[::-1])+s[::-1]+"".join(bef))

Submission status: Accepted

Solution code:
S = input()
N = int(input())
Q = [[] for _ in range(N)]
for i in range(N):
    line = input().split()
    Q[i] = line
reverse = 0
r_b = ""
r_f = ""
for i in range(N):
    if Q[i][0] == '1' :
        reverse = (reverse + 1) % 2
    else :
        if reverse == 0 :
            if Q[i][1] == '1' :
                r_f = Q[i][2] + r_f
            else :
                r_b = r_b +Q[i][2]
        else:
            if Q[i][1] == '1':
                r_b = r_b +Q[i][2]
            else:
                r_f = Q[i][2] + r_f
if reverse == 0 :
    print(r_f + S + r_b)
else:
    r_f = r_f[::-1]
    r_b = r_b[::-1]
    S = S[::-1]
    print(r_b + S + r_f)

Submission status: Accepted

Solution code:
s = input()

q = int(input())
a = [input().split() for l in range(q)]
flag =0
c=0
start=""
finish=""
for i in range(0,q):
    if a[i][0]=='1':
        flag +=1
        c=flag%2
    else:
        if a[i][1]=='1':
            if c==0:
                start = a[i][2] + start
            else:
                finish = finish + a[i][2]
        else:
            if c==0:
                finish = finish +a[i][2]
            else:
                start = a[i][2] + start
ans = start + s + finish
if c==0:
    print(ans)
else:
    ans=ans[::-1]
    print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
s = deque(readline().strip())
n = int(readline())
q = sys.stdin.readlines()
reverse = False
for qi in q:
    if qi[0] == '1':
        reverse = not reverse
    else:
        c = qi[4]
        if qi[2] == '2':
            if reverse:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if reverse:
                s.append(c)
            else:
                s.appendleft(c)
if reverse:
    s = reversed(list(s))
print(''.join(s))

Submission status: Accepted

Solution code:
import sys
from collections import deque

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())


def main():
    S = deque(INPUT())
    Q = INT()

    flag_order = True
    D_MAP = lambda: map(str, query.split())
    for i in range(Q):
        query = INPUT()
        if len(query.split()) == 1:
            T = 1
        else:
            T = 2
            _, F, C = D_MAP()
            F = int(F)

        if T == 1:
            if flag_order: flag_order = False
            else: flag_order = True
        if T == 2:
            if flag_order:
                if F == 1: S.appendleft(C)
                else: S.append(C)
            else:
                if F == 1: S.append(C)
                else: S.appendleft(C)

    print(''.join(S) if flag_order else ''.join(reversed(S)))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
 
alpha_map = {chr(i):i for i in range(97,123)}
alpha_map_inv = {i:chr(i) for i in range(97,123)}
a = deque()
for s in S:
  a.append(s)
reversed = False
 
for _ in range(Q):
  query = list(map(str, input().split()))
  if query[0] == '1':
    if reversed:
      reversed = False
    else:
      reversed = True
  else:
    if (query[1] == '1')&(not reversed):
      a.appendleft(query[2])
    elif (query[1] == '2')&(reversed):
      a.appendleft(query[2])
    else:
      a.append(query[2])
if reversed:
  a.reverse()
print(''.join(list(a)))

Submission status: Accepted

Solution code:
S = input() 
Q = int(input())
rlist, llist = [], []
flag, cnt = 0, 0

def trans(f):
    if f == 1:
        f = 0
    elif f == 0:
        f = 1
    return f

def pros(f, r, add):
    if f == 0:
        if r == "1":
            llist.append(lists[2])
        elif r == "2":
            rlist.append(lists[2])
    elif f == 1:
        if r == "2":
            llist.append(lists[2])
        elif r == "1":
            rlist.append(lists[2])

for i in range(Q):
    lists = input().split()
    if lists[0] == "1":
        flag = trans(flag)
        cnt += 1
    else:
        pros(flag, lists[1], lists[2])
        
llist.reverse()
        
S = "".join(llist) + S + "".join(rlist)

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

Submission status: Accepted

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


def solve(S, Q, Qvery):
    pool = []
    fw = True
    for qvery in reversed(Qvery):
        if qvery[0] == 1:
            if fw:
                fw = False
            else:
                fw = True
        else:
            if qvery[1] == 1 and fw or qvery[1] == 2 and not fw:  # 先頭に描く
                print(qvery[2], end='')
            else:
                pool.append(qvery[2])

    if fw:
        print(S, end='')
    else:
        rev_S = S[::-1]
        print(rev_S, end='')
    for p in reversed(pool):
        print(p, end='')
    print()

    # fw = True
    # ret = list(S)
    # # ret.append(S)
    # for qvery in Qvery:
    #     if qvery[0] == 1:
    #         # fw = not fw
    #         if fw:
    #             fw = False
    #         else:
    #             fw = True
    #     else:
    #         if qvery[1] == 1 and fw or qvery[1] == 2 and not fw:  # 先頭に追加
    #             ret.insert(0, qvery[2])
    #             # S = qvery[2] + S
    #         else:  # 末尾に追加
    #             ret.append(qvery[2])
    #             # S = S + qvery[2]

    # if fw:
    #     print(''.join(ret))
    # else:
    #     rev_ret = ret[::-1]
    #     print(''.join(rev_ret))


# 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 = str(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    Qvery = []
    for i in range(Q):
        T = int(next(tokens))
        if T == 1:
            Qvery.append([1])
        elif T == 2:
            Qvery.append([2, int(next(tokens)), str(next(tokens))])
    solve(S, Q, Qvery)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
reverse_flag = False
sento = deque([])
matubi = deque([])

def add_s(f, c):
  if f == '1':
    # 先頭
    sento.appendleft(c)
  else:
    # 末尾
    matubi.append(c)

for _ in range(Q):
  q = input()
  if q == '1':
    reverse_flag = not reverse_flag
    continue
  _, f, c = q.split()
  #print(reverse_flag, f, c)
  if reverse_flag:
    add_s('1' if f == '2' else '2', c)
  else:
    add_s(f, c)
  #print('---', S)

_s = ''.join(sento)
_m = ''.join(matubi)
ans = ''.join([_s, S, _m])
if reverse_flag:
  ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

s = list(input())
s2 = deque(list(reversed(s)))
s = deque(s)
Q = int(input())
q = []
f = []
c = []
count = 0

for _ in range(Q):
    tmp = list(map(str,input().split()))
    if len(tmp) == 1:
        count += 1
        q.append(int(tmp[0]))
        f.append(0)
        c.append('')
    else:
        q.append(int(tmp[0]))
        f.append(int(tmp[1]))
        c.append(tmp[2])


for i in range(Q):
    if q[i] == 1:
        tmp = s
        s = s2
        s2 = tmp
    else:
        if f[i] == 1:
            s.appendleft(c[i])
            s2.append(c[i])
        else:
            s.append(c[i])
            s2.appendleft(c[i])

for i in range(len(s)):
    print(s.popleft(),end="")
print("")

Submission status: Accepted

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

flg = "moto"

atama = list()
shippo = list()

for _ in range(Q):
    tmp = list(map(str, input().split()))
    if flg == "moto":
        if int(tmp[0]) == 1:
            flg = "hanten"
        else:
            if int(tmp[1]) == 1:
                atama.append(tmp[2])
            else:
                shippo.append(tmp[2])
    
    else:
        if int(tmp[0]) == 1:
            flg = "moto"
        else:
            if int(tmp[1]) == 1:
                shippo.append(tmp[2])
            else:
                atama.append(tmp[2])

if flg == "moto":
    atama = atama[::-1]
    print("".join(atama) + S + "".join(shippo))

else:
    shippo = shippo[::-1]
    print("".join(shippo) + S[::-1] + "".join(atama))

Submission status: Accepted

Solution code:
from collections import deque


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

for _ in range(Q):
    q = input().split()
    if q[0] == "1":
        flag *= -1
    elif q[1] == "1":
        if flag == 1:
            S.appendleft(q[2])
        else:
            S.append(q[2])
    else:
        if flag == 1:
            S.append(q[2])
        else:
            S.appendleft(q[2])
s = list(S)
if flag == 1:
    print("".join(s))
else:
    for i in reversed(s):
        print(i, end="")

Submission status: Accepted

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

top = []
end = []


order = []
state = True

for n in range(N):
    order = input().split()
    if order[0] == '2':
        if (order[1] == '1') == state:
            top.append(order[2])
        else:
            end.append(order[2])
    elif state:
        state = False
    else:
        state = True


if state:
    S = ''.join(S)
    top = ''.join(top[::-1])
    end = ''.join(end)
    out = top + S + end
    
else:
    S = ''.join(S[::-1])
    top = ''.join(top)
    end = ''.join(end[::-1])
    out = end + S + top

print(out)

Submission status: Accepted

Solution code:
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

S= one_str()

Q = one_int()

Qs = [ input() for i in range(Q)]
def ss(S, Qs):
    is_not_ac=False
    head = ""
    tail = ""
    for temp in Qs:
#         temp = input()
        if len(temp)>=2:
            T, F, C = temp.split()
            if F=="1":
                if is_not_ac:
                    tail += C
                else:
                    head += C
            else:
                if is_not_ac:
                    head += C
                else:
                    tail += C
        else:        
            is_not_ac = not is_not_ac
    if is_not_ac:
        return tail[::-1] + S[::-1] + head
    else:
        return head[::-1] + S + tail


print(ss(S,Qs))

Submission status: Accepted

Solution code:
from collections import deque
import sys
input=sys.stdin.readline
s=deque([input().rstrip()])
q=int(input())
rev=0
for _ in range(q):
	qw=input().split()
	if qw[0]=="1":
		rev+=1
	else:
		f=qw[1]
		c=qw[2]
		if f=="1":
			if rev%2==0:
				s.appendleft(c)
			else:
				s.append(c)
		else:
			if rev%2==0:
				s.append(c)
			else:
				s.appendleft(c)
ans=""
for x in s:
	ans+=x
if rev%2==0:
	print(ans)
else:
	print(ans[::-1])

Submission status: Accepted

Solution code:
from collections import deque

myqueue = deque(input())
#print("queue is {}".format(myqueue))

normal = True

num = int(input())

for i in range(num):
    query = input().split()
#    print(query)
    if len(query) == 1:
        normal = not normal
    else:
        if query[1] == "1":
            if normal:
                myqueue.appendleft(query[2])
            else:
                myqueue.append(query[2])
        else:
            if normal:
                myqueue.append(query[2])
            else:
                myqueue.appendleft(query[2])
#    print("queue is {}".format(myqueue))

counter = len(myqueue)

if normal:
    for i in range(counter):
        print(myqueue.popleft(), end="")
else:
    for i in range(counter):
        print(myqueue.pop(), end="")

Submission status: Accepted

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

rev = False
forw = ""
back = ""
for i in range(Q):
  query = input().split()
  if query[0] == "1":
    rev = not rev
  else:
    if (not rev and query[1] == "1") or (rev and query[1] == "2"):
      forw = query[2] + forw
    else:
      back += query[2]
#S = forw + S + back
if rev:
  S = back[::-1] + S[::-1] + forw[::-1]
else:
  S = forw + S + back
print(S)

Submission status: Accepted

Solution code:
from collections import deque

S = [input()]
d = deque(S)
#print(d)
Q = int(input())

pos = 0

for q in range(Q):
  myinput = list(input().split())
  #print(myinput)
  if len(myinput) == 1:
      pos += 1
  else:
      if myinput[1] == '1':
          if pos% 2 == 0:
               d.appendleft(myinput[2])   
          elif pos%2 !=0:
              #逆方向、先頭
              d.append(myinput[2])   
      elif myinput[1] == '2':
          if pos% 2 == 0:
              #正方向、末尾
              d.append(myinput[2])   
          elif pos%2 !=0:
              #逆方向、末尾
              d.appendleft(myinput[2])   

if pos%2 == 0:                 
    print(''.join(d))
else:
    print((''.join(d))[::-1])

Submission status: Accepted

Solution code:
s=input()
q = int(input())
toggle = "ok"
top = ""
last = ""

for i in range(q):
    si = input().split()
    if si[0] == "1":
        if toggle == "ok":
            toggle = "re"
        else:
            toggle = "ok"
    else:
        if si[1] == "1" and toggle == "ok":
            top += si[2] 
        elif si[1] == "2" and toggle == "re":
            top += si[2] 
        elif si[1] == "1" and toggle == "re":
            last += si[2]
        elif si[1] == "2" and toggle == "ok":
            last += si[2]
if toggle == "ok":
    retop = ''.join(list(reversed(top)))
    print(retop, end="")
    print(s,end="")
    print(last,end="")
else:
    relast = ''.join(list(reversed(last)))
    print(relast,end="")
    res = ''.join(list(reversed(s)))
    print(res,end="")
    print(top,end="")

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

s = input()
n = int(input())
front = ''
end = ''
front_flag = True # Trueの場合frontがfront Falseの場合endがfront
for i in range(n):
    q = input()
    if len(q) == 1:
        front_flag = not front_flag
    else:
        _, f, c = q.split()
        if f == '1':
            if front_flag:
                front += c
            else:
                end += c
        else:
            if not front_flag:
                front += c
            else:
                end += c

if front_flag:
    s = front[::-1] + s + end
else:
    s = end[::-1] + s[::-1] + front

print(s)

Submission status: Accepted

Solution code:
s = list(input())
q = int(input())
ss = list()
flip = False
for i in range(q):
    tfc = input()
    if tfc[0] == '1':
        flip = not flip
    else:
        if flip:
            if tfc[2] == '1':
                s.append(tfc[4])
            else:
                ss.append(tfc[4])
        else:
            if tfc[2] == '1':
                ss.append(tfc[4])
            else:
                s.append(tfc[4])
ss.reverse()
s = ss + s
if flip:
    s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
a = input()
l = list(a)
list_ins = [0] * 200000
ins_cnt = 0
list_app = [0] * 200000
app_cnt = 0

q = int(input())

default_order = True
for i in range(q):
  x = input().split()
  if x[0] == "1":
    default_order = default_order ^ True
  elif x[0] == "2":
    insert_type = default_order ^ False
    if x[1] == "2":
      insert_type = not(insert_type)
    if insert_type:
      list_ins[ins_cnt] = x[2]
      ins_cnt += 1
    else:
      list_app[app_cnt] = x[2]
      app_cnt += 1
ret = ""
if ins_cnt > 0:
  list_ins = list_ins[0:ins_cnt]
else:
  list_ins = []
if app_cnt > 0:
  list_app = list_app[0:app_cnt]
else:
  list_app = []
if default_order:
  list_ins.reverse()
  ret = ''.join(list_ins) + ''.join(l) + ''.join(list_app)
else:
  list_app.reverse()
  l.reverse()
  ret = ''.join(list_app) + ''.join(l) + ''.join(list_ins)

print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
n = int(input())
a = "*"
spin = 1

for i in range(n):
    q = input()
    if q[0] == "1":
        spin *= -1
        a = a[::-1]
    else:
        if q[2] == "1":
            a = q[4] + a
        else:
            a = a + q[4]
            
    if len(a) == 100:
        s = a[:a.index("*")] + s[::spin] + a[a.index("*")+1:]
        a = "*"
        spin = 1
            
print(a[:a.index("*")] + s[::spin] + a[a.index("*")+1:])

Submission status: Accepted

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

c = 0
R = ""
L = ""
for _ in range(Q):
    T = input().split()
    if len(T) == 1:
        c +=1
    else:
        if T[1] == "1":
            if c % 2 == 0:
                L = T[2] + L
            else:
                R = R + T[2]
        else:
            if c % 2 == 0:
                R = R + T[2]
            else:
                L = T[2] + L
if c % 2 == 0:
    print(L + S + R)
else:
    print("".join(list(reversed(L+S+R))))

Submission status: Accepted

Solution code:
# D
from collections import deque
s = deque(str(input()))
q_count = int(input())
rev = False
for i in range(q_count):
    q = list(input().split())
    if q[0] == '1':
        rev = not(rev)
    else:
        if q[1] == '1':
            if rev:
                # 末尾にq[2]追加
                s.append(q[2])
            else:
                # 先頭にq[2]追加
                s.appendleft(q[2])
        else:
            if rev:
                # 先頭にq[2]追加
                s.appendleft(q[2])
            else:
                # 末尾にq[2]追加
                s.append(q[2])
if rev:
    s.reverse()
print(*s, sep='')

Submission status: Accepted

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




def main():
    s = input()
    Q = int(input())
    q = [list(input().split()) for i in range(Q)]

    # print(q)

    rq = [0] * (Q+1)
    for i in range(Q):
        if len(q[i]) == 1:
            rq[i+1] = rq[i] + int(q[i][0])
        else:
            rq[i+1] = rq[i]
        rq[i+1] %= 2

    b = []
    a = []

    for i in range(Q):
        if len(q[i]) != 1:
            if (q[i][1] == '1' and rq[i+1] == 0) or (q[i][1] == '2' and rq[i+1] == 1):
                b.append(q[i][2])
            else:
                a.append(q[i][2])

    b = b[::-1]

    if rq[Q]%2 == 1:
        b, a = reversed(a), reversed(b)
        s = s[::-1]


    ans = ''.join([''.join(b), s, ''.join(a)])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
import collections

s = raw_input()
qn = int(raw_input())
qs = [raw_input().split() for _ in xrange(qn)]

direction = 1
deq = collections.deque(s)
#print deq, '<_>'[direction+1]
for q in qs:
    if q[0] == '1':
        direction *= -1
    else:
        _, fi, ci = q
        fi = -1 if fi == '1' else +1
        if direction * fi == -1:
            deq.appendleft(ci)
        else:
            deq.append(ci)
    #print deq, '<_>'[direction+1]

#print deq, '<_>'[direction+1]
ans = None
ln = len(deq)
if direction == 1:
    ans = ''.join(deq.popleft() for _ in xrange(ln))
else:
    ans = ''.join(deq.pop() for _ in xrange(ln))
print ans

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
num_rev = 0
S_front = [""] * Q
S_back = [""] * Q
 
for i in range(Q):
  inp = input()
  if len(inp) == 1:
    num_rev += 1
  else:
    T, F, C = inp.split()
    if ((num_rev % 2 == 0) and int(F) == 1) or ((num_rev % 2 == 1) and int(F) == 2):
      S_front[i] = C
    else:
      S_back[i] = C

S = "".join(S_front)[::-1] + S + "".join(S_back)
if num_rev % 2 == 0:
  print(S)
else:
  print(S[::-1])

Submission status: Accepted

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

b_str = ""
f_str = ""

for q in range(Q):
  r = input().split()
  if len(r) == 1:
    reverse_flg = not reverse_flg
  else:
    # Sは最終的に加えればいいので、それを最適化する
    if int(r[1]) == 1:
      if reverse_flg:
        f_str = f_str + r[2]
      else:
        b_str = r[2] + b_str
    else:
      if reverse_flg:
        b_str = r[2] + b_str
      else:
        f_str = f_str + r[2]

S = b_str + S + f_str

if reverse_flg:
  S = S[::-1]

print(S)

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
S = deque(S)
rev = False
for i in range(Q):
    T, *FC = input().split()
    if T == "1":
        rev = not rev
    else:
        if FC[0] == "1":
            if rev:
                S.append(FC[1])
            else:
                S.appendleft(FC[1])
        else:
            if rev:
                S.appendleft(FC[1])
            else:
                S.append(FC[1])

if rev:
    S.reverse()
print("".join(S))

Submission status: Accepted

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


def main():
    from collections import deque

    s = input()
    q = int(input())
    d = deque()
    d.append(s)
    reverse_count = 0

    for i in range(q):
        qi = input().split()

        if qi[0] == '1':
            reverse_count += 1
        else:
            if reverse_count % 2 == 0:
                if qi[1] == '1':
                    d.appendleft(qi[2])
                else:
                    d.append(qi[2])

            else:
                if qi[1] == '1':
                    d.append(qi[2])
                else:
                    d.appendleft(qi[2])

    c = ''.join(map(str, d))

    if reverse_count % 2 == 1:
        c = c[::-1]

    print(c)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
t_count = 0
before = []
after = []

for i in range(Q):
  list_ = list(input().split())
  T = int(list_[0])
  if T == 1:
    t_count += 1
  else:
    F, C = int(list_[1]), list_[2]
    if F == 1:
      if t_count%2 == 0:
        before.append(C)
      else:
        after.append(C)
    else:
      if t_count%2 == 0:
        after.append(C)
      else:
        before.append(C)
before.reverse()
S.extend(after)
before.extend(S)
if t_count%2 == 1:
  before.reverse() 
ans = ''.join(before)
print(ans)