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: Wrong Answer

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
query = [[1, 0, ''] for i in range(q)]
for i in range(q):
    tmp = input().split()
    if tmp[0] == '2':
        query[i][0] = int(tmp[0])
        query[i][1] = int(tmp[1])
        query[i][2] = tmp[2]

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

ans = ''.join(s)
if dire == 1:
    print(ans)
else:
    print(ans[::-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
query = [[1, 0, ''] for i in range(q)]
for i in range(q):
    tmp = input().split()
    if tmp[0] == '2':
        query[i][0] = int(tmp[0])
        query[i][1] = int(tmp[1])
        query[i][2] = tmp[2]

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

ans = ''.join(s)
if dire == 1:
    print(ans)
else:
    print(ans[::-1])

Submission status: Wrong Answer

Solution code:

S = input()
n = int(input())

flag = 0
s = []
s.append(S)
for i in range(n):
    Q = input()
    if Q[0] == "2":
        print(Q[0], Q[2], Q[4])
        if Q[2] == "1":
            if flag == 1:
                s.append(Q[4])
            else:
                s.insert(0, Q[4])
        else:
            if flag == 0:
                s.append(Q[4])
            else:
                s.insert(0, Q[4])
    else:
        flag ^= 1
    
print("".join(s) if flag == 0 else "".join(s[::-1]))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
query = [[1, 0, ''] for i in range(q)]

for i in range(q):
    tmp = input().split()
    if tmp[0] == '2':
        query[i][0] = int(tmp[0])
        query[i][1] = int(tmp[1])
        query[i][2] = tmp[2]

ins = [[], []]
dire = 1
for i in range(q):
    if query[i][0] == 1:
        dire *= -1
    else:
        ins[int(0.5+0.5*dire*(query[i][1]-1))].append(query[i][2])

if dire == 1:
    print(''.join(reversed(ins[0])) + s + ''.join(ins[1]))
else:
    print(''.join(reversed(ins[0])) + s + ''.join(reversed(ins[1])))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
query = [[1, 0, ''] for i in range(q)]

for i in range(q):
    tmp = input().split()
    if tmp[0] == '2':
        query[i][0] = int(tmp[0])
        query[i][1] = int(tmp[1])
        query[i][2] = tmp[2]

ins = [[], []]
dire = 1
for i in range(q):
    if query[i][0] == 1:
        dire *= -1
    else:
        ins[int(0.5+0.5*dire*(query[i][1]-1))].append(query[i][2])

if dire == 1:
    print(''.join(reversed(ins[0])) + s + ''.join(ins[1]))
else:
    print(''.join(reversed(ins[1])) + s + ''.join(ins[0]))

Submission status: Wrong Answer

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

query = [[x for x in input().split()] for i in range(q)]

T2query = []
cnt=0
for i in range(q-1,-1,-1):
    if(query[i][0]=='1'):
        cnt+=1
        continue
    if(cnt%2!=0):
        if(query[i][1]=='1'):
            query[i][1]='2'
        else:
            query[i][1]='1'  
    T2query.append(query[i])
print(cnt)
if(cnt%2!=0):
    s = s[::-1]


for _,i,word in reversed(T2query):
    
    if(i=='1'):
        s = word + s
    else:
        s = s + word

print(s)

Submission status: Wrong Answer

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

query = [[x for x in input().split()] for i in range(q)]
first = 0
for i in range(q):
    if(len(query[i])==1):
        first += 1
    else:
        break
T2query = []
cnt=0
for i in range(q-1,first-1,-1):
    if(len(query[i])==1):
        cnt+=1
        continue
    if(cnt%2!=0):
        if(query[i][1]=='1'):
            query[i][1]='2'
        else:
            query[i][1]='1'  
    T2query.append(query[i])

if(first%2!=0):
    s = s[::-1]


for _,i,word in reversed(T2query):
    
    if(i=='1'):
        s = word + s
    else:
        s = s + word

print(s)

Submission status: Wrong Answer

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

direction = 1
head = []
tail = []
for i in range(Q):
    T = input()
    if T == '1':
        direction = direction * -1
    else:
        T = T.split()
        if T[1] == '1':
            if direction == 1:
                head.append(T[2])
            else:
                tail.append(T[2])
        else:
            if direction == 1:
                tail.append(T[2])
            else:
                head.append(T[2])

head = head[::(direction * -1)]
head.append(S)
head.extend(tail[::direction])
S = "".join(head)[::direction]

print(S)

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
que=[]
count=0
left=[]
right=[]
for i in range(q):
  qu=list(map(str,input().split()))
  if qu[0]=="1":
    count+=1
  que.append(qu)
han=0
for i in range(q):
  if que[i][0]=="1":
    han+=1
  else:
    if que[i][1]=="1":  
      if (count-han)%2==0:
        left.append(que[i][2])
      else:
        right.append(que[i][2])
    elif que[i][1]=="2":  
      if (count-han)%2==0:
        left.append(que[i][2])
      else:
        right.append(que[i][2])
ls="".join(left[::-1])
lr="".join(right)
print(ls+s+lr)

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
que=[]
count=0
left=[]
right=[]
for i in range(q):
  qu=list(map(str,input().split()))
  if qu[0]=="1":
    count+=1
  que.append(qu)
han=0
for i in range(q):
  if que[i][0]=="1":
    han+=1
  else:
    if que[i][1]=="1":  
      if (count-han)%2==0:
        left.append(que[i][2])
      else:
        right.append(que[i][2])
    elif que[i][1]=="2":  
      if (count-han)%2==0:
        right.append(que[i][2])
      else:
        left.append(que[i][2])
ls="".join(left[::-1])
lr="".join(right)
print(ls+s+lr)

Submission status: Wrong Answer

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

cnt = [0]*(q+5)
arr=[]
l=0
r=0

for i in range(q):
    t=input().split()
    if t[0]=='1':
        r+=1
        cnt[0]+=1 
        cnt[l]-=1 
    else:
        arr.append([t[2],int(t[1])])
        l+=1 
if r%2==1:
    s=s[::-1]
sm=0
for i in range(len(arr)):
    sm+=cnt[i]
    if sm%2==1:
        if arr[i][1]==1:arr[i][1]=2
        elif arr[i][1]==2:arr[i][1]=1 
ans1=""
ans2=""
for i in range(len(arr)):
    if arr[i][1]==1:
        ans1+=arr[i][0]

    else:ans2+=arr[i][0]
print("%s%s%s"%(ans1,s,ans2))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = deque([input().strip()])
Q = int(input().strip())

inv = False
for i in range(Q):
  inp = input().strip().split()
  if len(inp)==1:
    inv = not inv
  else:
    t, f, c = inp
    if (f=="1" and not inv) or (f=="2" and inv):
      S.appendleft(c)
    elif (f=="2" and not inv) or (f=="1" and inv):
      S.append(c)
if not inv:
  print(''.join(S))
else:
  print(''.join(reversed(S)))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = deque([input().strip()])
Q = int(input().strip())

inv = False
for i in range(Q):
  inp = input().strip().split()
  if len(inp)==1:
    inv = not inv
  else:
    t, f, c = inp
    if (f=="1" and not inv) or (f=="2" and inv):
      S.appendleft(c)
    elif (f=="2" and not inv) or (f=="1" and inv):
      S.append(c)
if not inv:
  print(''.join(S))
else:
  print(''.join(list(S)[::-1]))

Submission status: Wrong Answer

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

inv = False
for i in range(Q):
  inp = input().strip().split()
  if len(inp)==1:
    inv = not inv
  else:
    t, f, c = inp
    if f==1:
      S = c + S
    else:
      S = S + c
if not inv:
  print(S[::-1])
else:
  print(S)

Submission status: Wrong Answer

Solution code:

from collections import deque

class abc_158:
    
    def __init__(self):
        pass
    
    def A(self):
        S=input()
        if "BA" in S or "AB" in S:print("Yes")
        else:print("No")
    
    def B(self):
        NAB=list(map(int,input().split()))
        
        if NAB[0]<=NAB[1]:ans=NAB[0]
        elif NAB[0]<=NAB[1]+NAB[2]:ans=NAB[1]
        else:
            tail = NAB[0]%(NAB[1]+NAB[2]) if NAB[0]%(NAB[1]+NAB[2])<NAB[1] else NAB[1]
            ans=(int(NAB[0]/(NAB[1]+NAB[2]))*NAB[1])+tail
            
        print(int(ans))
            
    def C(self):
        AB=list(map(int,input().split()))
        ans=-1
        
        #A<B<100
        for i in range(1,1001):
            if AB[0]==int(i*0.08) and AB[1]==int(i*0.1):
                ans=i
                break
            else:continue
        print(ans)
            
    def D(self):
        S=input()
        Q=int(input())
        Q_list=[list(input().split()) for i in range(Q)]
        d=deque()
        
        d.append(S)
        for i in Q_list:
            if int(i[0])==1:d.reverse()
            else:
                if int(i[1])==1:d.appendleft(i[2])
                else :d.append(i[2])
                
        ans="".join(list(d))
        print(ans)
        
    def E(self):
        pass
    
    def F(self):
        pass
    
    
if __name__=='__main__':
    obj=abc_158()
    obj.D()

Submission status: Wrong Answer

Solution code:
def solve(s, q, qs):
    reverse_cnt = 0

    for item in qs:

        if len(item) == 1:
            # T = 1 文字列を反転
            reverse_cnt += 1
        else:
            # T = 2

            if int(item[1]) == 1:
                # 先頭に追加
                s = item[2] + s

            else:
                # 末尾に追加
                s = item[2] + s

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

    return s

if __name__ == '__main__':

    #s = deque() # queue

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

    ans = solve(s, q, qs)
    print(ans)

Submission status: Wrong Answer

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

p = ""
flag = 0
for _ in range(q):
  qq = input().split()
  if qq[0] == "1":
    flag = abs(flag) -1
  else:
    if qq[1] == "1":
      if flag == 0:
        s += qq[2]
      else:
        p += qq[2]
    else:
      if flag == 0:
        p += qq[2]
      else:
        s += qq[2]
        
if flag == 0:
  s = s[::-1] + p
else:
  s = p[::-1] + s
print(s)

Submission status: Wrong Answer

Solution code:
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# from fractions import gcd
# from math import gcd

# def lcm(n, m):
#     return int(n * m / gcd(n, m))


# def coprimize(p, q):
#     common = gcd(p, q)
#     return (p // common, q // common)


# def find_gcd(list_l):
#     x = reduce(gcd, list_l)
#     return x


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def main():
    #
    s0 = input().strip()
    q = int(input().strip())

    #
    stc1 = ""
    stc2 = ""
    flag = 0

    # def f(flag1, flag2, val):
    #     if flag1 ^ flag2:
    #         stc1 += val
    #     else:
    #         stc2 += val

    #
    for i in range(q):
        t, *fc = map(lambda x: int(x) if x.isdecimal() else x, input().strip().split())
        # eprint('t,fc ', end=':\n')
        # eprint(t, fc, "\n")

        if t == 2:    # 文字列付加
            # f(fc[0], flag, fc[1])
            if (fc[0]-1)^flag:  # (fc[0]-1)は前方/後方， flagは非反転/反転，をそれぞれ表す
                stc2+=fc[1]
            else:
                stc1+=fc[1]
        else:       # 反転命令
            flag = flag ^ 1

    #
    if flag == 0:
        ans = stc1[::-1] + s0 + stc2
    else:
        ans = stc2[::-1] + s0 + stc1
    print(ans)
    # eprint(ans[::-1])
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# from fractions import gcd
# from math import gcd

# def lcm(n, m):
#     return int(n * m / gcd(n, m))


# def coprimize(p, q):
#     common = gcd(p, q)
#     return (p // common, q // common)


# def find_gcd(list_l):
#     x = reduce(gcd, list_l)
#     return x


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def main():
    #
    s0 = input().strip()
    q = int(input().strip())

    #
    stc1 = ""
    stc2 = ""
    flag = 0

    # def f(flag1, flag2, val):
    #     if flag1 ^ flag2:
    #         stc1 += val
    #     else:
    #         stc2 += val

    #
    for i in range(q):
        t, *fc = map(lambda x: int(x) if x.isdecimal() else x, input().strip().split())
        # eprint('t,fc ', end=':\n')
        # eprint(t, fc, "\n")

        if t == 2:    # 文字列付加
            # f(fc[0], flag, fc[1])
            fc[0] = fc[0] - 1
            if fc[0]^flag:  # fc[0]は前方/後方， flagは非反転/反転，をそれぞれ表す
                stc1+=fc[1]
            else:
                stc2+=fc[1]
        else:       # 反転命令
            flag = flag ^ 1

    #
    if flag == 0:
        ans = stc1[::-1] + s0 + stc2
    else:
        ans = stc2[::-1] + s0 + stc1
    print(ans)
    # eprint(ans[::-1])
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S=(input())
Q=int(input())
cnt_re=0
s=''
for i in range(Q):
    q=list(input().split())
    if q[0]=='1':
        cnt_re+=1
    else:
        if (int(q[1])+cnt_re)%2==1:
            s=s+q[2]
        else:
            S=S+q[2]
print(S,s)
if cnt_re%2==1:
    print(S[::-1]+s)
else:
    print(s[::-1]+S)

Submission status: Wrong Answer

Solution code:
S=(input())
Q=int(input())
cnt_re=0
cnt_all=0
for i in range(Q):
    q=list(input().split())
    if q[0]=='1':
        cnt_re+=1
        cnt_all+=1
    else:
        tmp=cnt_re%2
        if ((q[1]=='1')&(tmp==0)) | ((q[1]=='2')&(tmp==1)):
            S=q[2]+S
            cnt_re=0
        elif ((q[1]=='1')&(tmp==1)) | ((q[1]=='2')&(tmp==0)):
            S=S+q[2]
            cnt_re=0
S=list(S)
if cnt_all%2==1:
    S.reverse()
print(''.join(S))

Submission status: Wrong Answer

Solution code:
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# from fractions import gcd
# from math import gcd

# def lcm(n, m):
#     return int(n * m / gcd(n, m))


# def coprimize(p, q):
#     common = gcd(p, q)
#     return (p // common, q // common)


# def find_gcd(list_l):
#     x = reduce(gcd, list_l)
#     return x


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def main():
    #
    s0 = input().strip()
    q = int(input().strip())

    #
    stc1 = ""
    stc2 = ""
    flag = 0

    # def f(flag1, flag2, val):
    #     if flag1 ^ flag2:
    #         stc1 += val
    #     else:
    #         stc2 += val

    #
    for i in range(q):
        t, *fc = map(lambda x: int(x) if x.isdecimal() else x, input().strip().split())
        # eprint('t,fc ', end=':\n')
        # eprint(t, fc, "\n")

        if t == 2:    # 文字列付加
            # f(fc[0], flag, fc[1])
            if fc[0]^flag:  # fc[0]は前方/後方， flagは非反転/反転，をそれぞれ表す
                stc1+=fc[1]
            else:
                stc2+=fc[1]
        else:       # 反転命令
            flag = flag ^ 1

    #
    if flag == 0:
        ans = stc1[::-1] + s0 + stc2
    else:
        ans = stc2[::-1] + s0 + stc1
    print(ans)
    # eprint(ans[::-1])
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# from fractions import gcd
# from math import gcd

# def lcm(n, m):
#     return int(n * m / gcd(n, m))


# def coprimize(p, q):
#     common = gcd(p, q)
#     return (p // common, q // common)


# def find_gcd(list_l):
#     x = reduce(gcd, list_l)
#     return x


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def main():
    #
    s0 = input().strip()
    q = int(input().strip())

    #
    stc1 = ""
    stc2 = ""
    flag = 0

    # def f(flag1, flag2, val):
    #     if flag1 ^ flag2:
    #         stc1 += val
    #     else:
    #         stc2 += val

    #
    for i in range(q):
        t, *fc = map(lambda x: int(x) if x.isdecimal() else x, input().strip().split())
        # eprint('t,fc ', end=':\n')
        # eprint(t, fc, "\n")

        if t == 2:    # 文字列付加
            # f(fc[0], flag, fc[1])
            if fc[0]^flag:  # fc[0]は前方/後方， flagは非反転/反転，をそれぞれ表す
                stc1+=fc[1]
            else:
                stc2+=fc[1]
        else:       # 反転命令
            flag = flag ^ 1

    #
    if flag == 0:
        ans = stc1[::-1] + s0 + stc2
    else:
        ans = (stc1[::-1] + s0 + stc2)[::-1]
    print(ans)
    # eprint(ans[::-1])
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# from fractions import gcd
# from math import gcd

# def lcm(n, m):
#     return int(n * m / gcd(n, m))


# def coprimize(p, q):
#     common = gcd(p, q)
#     return (p // common, q // common)


# def find_gcd(list_l):
#     x = reduce(gcd, list_l)
#     return x


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def main():
    #
    s0 = input().strip()
    q = int(input().strip())

    #
    stc1 = ""
    stc2 = ""
    flag = 0

    # def f(flag1, flag2, val):
    #     if flag1 ^ flag2:
    #         stc1 += val
    #     else:
    #         stc2 += val

    #
    for i in range(q):
        t, *fc = map(lambda x: int(x) if x.isdecimal() else x, input().strip().split())
        eprint('t,fc ', end=':\n')
        eprint(t, fc, "\n")

        if t == 2:    # 文字列付加
            # f(fc[0], flag, fc[1])
            if fc[0]^flag:
                stc1+=fc[1]
            else:
                stc2+=fc[1]
        else:       # 反転命令
            flag = flag ^ 1

    #
    if flag == 0:
        ans = stc1[::-1] + s0 + stc2
    else:
        ans = (stc1[::-1] + s0 + stc2)[::-1]
    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

for i in range(q):
    query = input().split()
    if query[0] == "1":
        s = s[::-1]
    else:
        if query[1] == "1":
            s = query[2] + s
        else:
            s = s + query[2]
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
d=deque(map(str,input().split()))
q=int(input())
a=1
for _ in range(q):
    ss=input().split()
    if ss[0]=='1':
        a*=-1
    else:
        if ss[1]=='1':
            if a==1:
                for i11 in ss[2]:
                    d.appendleft(i11)
            else:
                for i111 in ss[2]:
                    d.append(i111)
        else:
            if a==1:
                for i21 in ss[2]:
                    d.append(i21)
            else:
                for i211 in ss[2]:
                    d.appendleft(i211)
if a==1:
    print(''.join(d))
else:
    d.reverse()
    print(''.join(d))

Submission status: Wrong Answer

Solution code:
from collections import deque
d=deque(map(str,input().split()))
q=int(input())
a=1
for _ in range(q):
    ss=input().split()
    if ss[0]=='1':
        a*=-1
    else:
        if ss[1]=='1':
            if a==1:
                for i11 in ss[2]:
                    d.appendleft(i11)
            else:
                for i111 in ss[2]:
                    d.append(i111)
        else:
            if a==1:
                for i21 in ss[2]:
                    d.append(i21)
            else:
                for i211 in ss[2]:
                    d.appendleft(i211)
if a==1:
    for j in d:
        print(j,end='')
else:
    d.reverse()
    for jj in d:
        print(jj,end='')

Submission status: Wrong Answer

Solution code:
from collections import deque
d=deque(map(str,input().split()))
q=int(input())
a=1
for _ in range(q):
    ss=input()
    if ss[0]=='1':
        a*=-1
    else:
        if ss[2]=='1':
            if a==1:
                d.appendleft(ss[4])
            else:
                d.append(ss[4])
        else:
            if a==1:
                d.append(ss[4])
            else:
                d.appendleft(ss[4])
if a==1:
    for j in d:
        print(j,end='')
else:
    d.reverse()
    for jj in d:
        print(jj,end='')

Submission status: Wrong Answer

Solution code:
from collections import deque
d=deque(map(str,input().split()))
q=int(input())
for _ in range(q):
    ss=input()
    if ss[0]==str(1):
        d.reverse()
    else:
        if ss[2]==str(1):
            d.appendleft(ss[4])
        else:
            d.append(ss[4])
for j in d:
    print(j,end='')

Submission status: Wrong Answer

Solution code:
from collections import deque
d=deque(map(str,input().split()))
q=int(input())
for _ in range(q):
    ss=input().split()
    if ss[0]==str(1):
        d.reverse()
    else:
        if ss[1]==str(1):
            d.appendleft(ss[2])
        else:
            d.append(ss[2])
for j in d:
    print(j,end='')

Submission status: Wrong Answer

Solution code:
from collections import deque
s=list(map(str,input().split()))
q=int(input())
d=deque()
for i in s:
    d.append(i)
for _ in range(q):
    ss=input().split()
    if ss[0]==str(1):
        d.reverse()
    else:
        if ss[1]==str(1):
            d.appendleft(ss[2])
        else:
            d.append(ss[2])
for j in d:
    print(j,end='')

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
status = 1
for i in range(q):
    query = [i for i in input().split()]
    if query[0] == "1":
        status *= -1
    else:
        if (query[1] == "1" and status == 1) or (query[1] == "2" and status == -1):
            s = query[2] + s
        else:
            s = s + query[2]

print(s if status else reversed(s))

Submission status: Wrong Answer

Solution code:
from collections import deque
S = list(input()) 
d = deque()
for s in S:
    d.append(s)
q = int(input())
o = 0

for _ in range(q):
    qu = list(input().split())
    if len(qu) == 1:
        o = -1 if o == 0 else 0
    if len(qu) == 3:
        if qu[1] == '1':
            d.append(qu[2]) if o == 0 else d.appendleft(qu[2])
        if qu[1] == '2':
            d.appendleft(qu[2]) if o == 0 else d.append(qu[2])

r = ''
if o == 0:
    for _ in range(len(d)):
        r += d.pop()
else:
    for _ in range(len(d)):
        r += d.popleft()

print(r)

Submission status: Wrong Answer

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

for i in range(Q):
        arr = list(input().split())
        if arr[0] == '1':
            switch_flg *= -1

        elif arr[0] == '2':
            if int(arr[1])*switch_flg == 1 or -2:
                S = arr[2]+S
            elif int(arr[1])*switch_flg == -1 or 2:
                S = S+arr[2]

if switch_flg ==1:
    print(S)
elif switch_flg == -1:
    print(S[::-1])

Submission status: Wrong Answer

Solution code:
import copy
import itertools

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

ct = 0
ls = []
rs = []
for i in range(q):
    qtmp = input().split()
    #qtmp = [2,1,'c']
    t = int(qtmp[0])
    if(t == 1):
        ct += 1
        ct = ct % 2
    else:
        f = int(qtmp[1])
        if(ct+1 == f):
            ls.append( qtmp[2])
        else:
            rs.append( qtmp[2])
if(ct == 1):
    tmp = rs
    rs = ls
    ls = tmp
for i in range(len(ls)):
    print(ls[len(ls)-i-1], end='')
print(s, end='')
for i in range(len(rs)):
    print(rs[i], end='')
print("")

Submission status: Wrong Answer

Solution code:
import copy
import itertools

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

ct = 0
ls = []
rs = []
for i in range(q):
    qtmp = input().split()
    #qtmp = [2,1,'c']
    t = int(qtmp[0])
    if(t == 1):
        ct += 1
        ct = ct % 2
    else:
        f = int(qtmp[1])
        if(ct == f):
            rs.append( qtmp[2])
        else:
            ls.append( qtmp[2])
if(ct == 1):
    tmp = rs
    rs = ls
    ls = tmp
for i in range(len(ls)):
    print(ls[len(ls)-i-1], end='')
print(s, end='')
for i in range(len(rs)):
    print(rs[i], end='')
print("")

Submission status: Wrong Answer

Solution code:
import collections
q = collections.deque(list(raw_input()))
operations = [raw_input().split(' ') for _ in range(int(raw_input()))]
rev = True

for op in operations:
	if len(op) == 1:
		rev = not(rev)
	elif len(op) == 3:
		_, where, char = op
		where = bool(int(where) - 1) ^ rev
		if where:			
			q.append(char)
		else:
			q.appendleft(char)

print ''.join(list(q)[:: (-1 if rev else 1)])

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

def LI(): return list(map(int, stdin.readline().split()))
def LS(): return list(stdin.readline())

s = [input()]
q = int(input())
# print(s,q)
head = 1
for i in range(q):
    query = input().split(' ')
    # print(query)
    if query[0]=='1':
        head *= -1
    else:
        if (query[1]=='1' and head==1) or (query[1]=='2' and head==-1):
            s.insert(0, query[2])
        else :
            s.append(query[2])
print(''.join(s))

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

def LI(): return list(map(int, stdin.readline().split()))
def LS(): return list(stdin.readline())

s = [input()]
q = int(input())
# print(s,q)
for i in range(q):
    query = input().split(' ')
    # print(query)
    if query[0]=='1':
        try:
            s = s[::-1]
        except:
            pass
    else:
        if query[1]=='1':
            s.insert(0, query[2])
        else:
            s.append(query[2])
print(''.join(s))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
f = False
# Falseだと左が先頭
for _ in range(q):
    t = input().split()
    if t[0]=="1":
        f = not f
    elif t[0]=="2":
        if f:
            t[2]=t[2][::-1]
        if t[1]=="1":
            s = s+t[2]
        elif t[1]=="2":
            s = s+t[2]
if f:
    print(s)
else:
    print(s[::-1])