Problem: 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.

Submission attempts (in order):
import math
import random
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from heapq import heapify, heappop, heappush
from itertools import (chain, combinations, combinations_with_replacement,
                       permutations)

MAX_INT = 2**62-1
sys.setrecursionlimit(10**7)

input = sys.stdin.readline


def read_n_int():
    return map(int, input().split())


def main():
    s = input()
    q = int(input())
    queue = deque(s)
    is_reversed = 0

    for _ in range(q):
        t, *x = input().split()
        if t == '1':
            is_reversed ^= 1
        else:
            where, c = x
            where = int(where)
            if where ^ is_reversed:
                queue.appendleft(c)
            else:
                queue.append(c)
    if is_reversed:
        queue.reverse()
    print(''.join(queue))


if __name__ == "__main__":
    main()


import math
import random
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from heapq import heapify, heappop, heappush
from itertools import (chain, combinations, combinations_with_replacement,
                       permutations)

MAX_INT = 2**62-1
sys.setrecursionlimit(10**7)

input = sys.stdin.readline


def read_n_int():
    return map(int, input().split())


def main():
    s = input()
    q = int(input())
    queue = deque(s)
    is_reversed = 0

    for _ in range(q):
        t, *x = input().split()
        if t == '1':
            is_reversed ^= 1
        else:
            where, c = x
            where = int(where) & 1
            if where ^ is_reversed:
                queue.appendleft(c)
            else:
                queue.append(c)
    if is_reversed:
        queue.reverse()
    print(''.join(queue))


if __name__ == "__main__":
    main()


import math
import random
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from heapq import heapify, heappop, heappush
from itertools import (chain, combinations, combinations_with_replacement,
                       permutations)

MAX_INT = 2**62-1
sys.setrecursionlimit(10**7)

input = sys.stdin.readline


def read_n_int():
    return map(int, input().split())


def main():
    s = input().strip()
    q = int(input())
    queue = deque(s)
    is_reversed = 0

    for _ in range(q):
        t, *x = input().strip().split()
        
        if t == '1':
            is_reversed ^= 1
        else:
            where, c = x
            where = int(where) & 1
            if where ^ is_reversed:
                queue.appendleft(c)
            else:
                queue.append(c)
    if is_reversed:
        queue.reverse()

    print(''.join(queue))


if __name__ == "__main__":
    main()


Verdicts: Wrong Answer

Wrong Answer

Accepted