alexjercan/bugnet huggingface dataset:
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

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

topFlg = True

leftStr = ""
rightStr = ""

for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        topFlg = not (topFlg)
    else:
        if query[1] == "1":
            if topFlg:
                leftStr = query[2] + leftStr
            else:
                rightStr += query[2]
        else:
            if topFlg:
                rightStr += query[2]
            else:
                leftStr = query[2] + leftStr

print(leftStr + S + rightStr if topFlg else rightStr[::-1] + S[::-1] + leftStr[::-1])


Error: TLE

Fixed submission:

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

topFlg = True

for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        topFlg = not (topFlg)
    else:
        if query[1] == "1":
            S = query[2] + S if topFlg else S + query[2]
        else:
            S = S + query[2] if topFlg else query[2] + S

print(S) if topFlg else print(S[::-1])
Problem p02756 (Python) — buggy submission, verdict: Runtime Error

from collections import deque

S = deque(input())
Q = int(input())
Queries = [input().split() for _ in range(Q)]

need_reverse = False

for q in Queries:
    if q[0] == "1":
        need_reverse = not need_reverse
    else:
        if q[1] == "1":
            if not need_reverse:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if not need_reverse:
                S.append(q[2])
            else:
                S.appendleft(q[2])

S = "".join(S)
print(S if not need_reverse else S[::-1])


Error: WA

Fixed submission:

from collections import deque

S = deque(input())
Q = int(input())
Queries = [input().split() for _ in range(Q)]

need_reverse = False

for q in Queries:
    if q[0] == "1":
        need_reverse = not need_reverse
    else:
        if q[1] == "1":
            if not need_reverse:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if not need_reverse:
                S.append(q[2])
            else:
                S.appendleft(q[2])

print(S if not need_reverse else S[::-1])
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

# D - String Formation

from collections import deque

S = deque([s for s in input()])
Q = int(input())
rev = False

for _ in range(Q):
    T = [t for t in input().split()]
    t = int(T[0])
    if t == 1:
        rev = not rev
    if t == 2:
        f, c = int(T[1]), T[2]
        if f == 1:
            if rev:
                S.append(c)
            else:
                S.appendleft(c)
        if f == 2:
            if rev:
                S.appendleft(c)
            else:
                S.append(c)

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


Error: TLE

Fixed submission:

# D - String Formation

S = input()
Q = int(input())
rev = False

for _ in range(Q):
    T = [t for t in input().split()]
    t = int(T[0])
    if t == 1:
        rev = not rev
    if t == 2:
        f, c = int(T[1]), T[2]
        if f == 1:
            if rev:
                S = S + c
            else:
                S = c + S
        if f == 2:
            if rev:
                S = c + S
            else:
                S = S + c

print(S if not rev else S[::-1])
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import deque

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

deq = deque(s)
rev = False

for _ in range(q):
    query = input().split()
    t = query[0]
    if t == "1":
        rev = not rev
        continue
    f = query[1]
    c = query[2]
    if not rev and f == "2" or rev and f == "1":
        deq.append(c)
    else:
        deq.appendleft(c)
if rev:
    deq = reversed(deq)
print("".join(deq))


Error: TLE

Fixed submission:

s = list(input())
q = int(input())
rev = False

for _ in range(q):
    query = input().split()
    t = query[0]
    if t == "1":
        rev = not rev
        continue
    f = query[1]
    c = query[2]
    if not rev and f == "2" or rev and f == "1":
        s.append(c)
    else:
        s.insert(0, c)
if rev:
    s = reversed(s)
print("".join(s))
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    s = input()
    q = int(input())
    flag = 0
    cnt = 0
    front = []
    back = []

    for i in range(q):
        t = input()
        if len(t) == 1:
            cnt += 1
            if flag == 0:
                flag = 1
            elif flag == 1:
                flag = 0
        else:
            _, a, b = (str(i) for i in t.split())
            if a == "1":
                if flag == 0:
                    front.append(b)
                elif flag == 1:
                    back.append(b)
            elif a == "2":
                if flag == 0:
                    back.append(b)
                elif flag == 1:
                    front.append(b)

    if cnt % 2 == 0:
        front = front[::-1]
        s = "".join(front) + s + "".join(back)
    else:
        back = back[::-1]
        s = "".join(back) + s[::-1] + "".join(front)

    print(s)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

s = input()
q = int(input())
flag = 0
cnt = 0

for i in range(q):
    t = input()
    if len(t) == 1:
        cnt += 1
        if flag == 0:
            flag = 1
        elif flag == 1:
            flag = 0
    else:
        _, a, b = (str(i) for i in t.split())
        if a == "1":
            if flag == 0:
                s = b + s
            elif flag == 1:
                s = s + b
        elif a == "2":
            if flag == 0:
                s = s + b
            elif flag == 1:
                s = b + s

if cnt % 2 == 0:
    print(s)
else:
    print(s[::-1])
Problem p02756 (Python) — buggy submission, verdict: Runtime Error

from collections import deque

s = deque(input())
len_queries = int(input())
queries = [input().split() for _ in range(len_queries)]

is_forward = True
for query in queries:
    if "1" == query[0]:
        is_forward = not is_forward
    else:
        if is_forward:
            if "1" == query[1]:
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if "1" == query[1]:
                s.append(query[2])
            else:
                s.appendleft(query[2])
if not is_forward:
    s.reverse()
print("".join(s))


Error: 0

Fixed submission:

from collections import deque

s = deque(input())
len_queries = int(input())
queries = [input().split() for _ in range(len_queries)]

is_forward = True
for query in queries:
    if "1" == query[0]:
        is_forward = not is_forward
    else:
        if is_forward:
            insert_index = 0 if "1" == query[1] else len(s)
            s.insert(insert_index, query[2])
        else:
            insert_index = len(s) if "1" == query[1] else 0
            s.insert(insert_index, query[2])
if not is_forward:
    s.reverse()
print("".join(s))
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

s = input()
Q = int(input())
flag = True  # flagがTrueのとき、偶数回数の反転が行われている
cnt = 0
query = [list(map(str, input().split())) for _ in range(Q)]
before = []  # 先頭に追加する時
after = []  # 末尾に追加する時
for i in range(Q):
    a = query[i]
    if len(a) == 1:  # 1のqueryのとき
        cnt += 1
        if flag:
            flag = False
        else:
            flag = True
    else:
        f, c = a[1], a[2]
        if f == "1":  # 先頭に追加するもの
            if flag:
                # s = c + s  # 偶数回の反転なら素直に先頭に追加する
                before.append(c)
            else:
                # s += c  # 奇数会の反転なら後ろに追加する
                after.append(c)
        else:  # 末尾に追加する
            if flag:
                # s += c  # 末尾に追加
                after.append(c)
            else:
                # s = c + s  # 先頭に追加
                before.append(c)
before.reverse()
# print(before)
# print(s)
# print(after)
s = "".join(before) + s + "".join(after)
if cnt % 2 == 1 and cnt != 0:
    s = s[::-1]
print(s)


Error: TLE

Fixed submission:

s = input()
Q = int(input())
flag = True  # flagがTrueのとき、偶数回数の反転が行われている
cnt = 0
query = [list(map(str, input().split())) for _ in range(Q)]
for i in range(Q):
    a = query[i]
    # tmp.append(query)
    if len(a) == 1:  # 1のqueryのとき
        cnt += 1
        if flag:
            flag = False
        else:
            flag = True
    else:
        f, c = a[1], a[2]
        if f == "1":  # 先頭に追加するもの
            if flag:
                s = c + s  # 偶数回の反転なら素直に先頭に追加する
            else:
                s += c  # 奇数会の反転なら後ろに追加する
        else:  # 末尾に追加する
            if flag:
                s += c
            else:
                s = c + s

if cnt % 2 == 1 and cnt != 0:
    s = s[::-1]
print(s)
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import deque

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


def getQuery():
    yield input().split()


for _ in range(q):
    for query in getQuery():
        if query[0] == "1":
            reverseFlag = not reverseFlag
        else:
            if query[1] == "1":
                if reverseFlag:
                    d.append(query[2])
                else:
                    d.appendleft(query[2])
            else:
                if reverseFlag:
                    d.appendleft(query[2])
                else:
                    d.append(query[2])

ans = "".join(d)
if reverseFlag:
    ans = ans[::-1]

print(ans)


Error: TLE

Fixed submission:

s = input()
q = int(input())
reverseFlag = False


def getQuery():
    yield input().split()


for _ in range(q):
    for query in getQuery():
        if query[0] == "1":
            reverseFlag = not reverseFlag
        else:
            if query[1] == "1":
                s = (s + query[2]) if reverseFlag else (query[2] + s)
            else:
                s = (query[2] + s) if reverseFlag else (s + query[2])

if reverseFlag:
    s = s[::-1]

print(s)
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import deque

s = deque(list(input()))
n = int(input())
q = [list(input().split()) for _ in range(n)]
rev = False

for tfc in q:
    if tfc[0] == "1" and rev is False:
        rev = True
    elif tfc[0] == "1" and rev is True:
        rev = False
    elif rev is False:
        if tfc[1] == "1":
            s.appendleft(tfc[2])
        else:
            s.append(tfc[2])
    elif rev is True:
        if tfc[1] == "1":
            s.append(tfc[2])
        else:
            s.appendleft(tfc[2])

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


Error: TLE

Fixed submission:

from collections import deque

s = deque(list(input()))
n = int(input())
q = [list(input().split()) for _ in range(n)]
for tfc in q:
    if tfc[0] == "1":
        s.reverse()
    else:
        if tfc[1] == "1":
            s.appendleft(tfc[2])
        else:
            s.append(tfc[2])

print("".join(s))
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

S = input()
Q = int(input())
reverseCount = 0
pre = ""
pro = ""
for _ in range(Q):
    query = input()
    if len(query) == 1:
        reverseCount += 1
    else:
        _, F, C = query.split(" ")
        if (int(F) + reverseCount) % 2 == 1:
            pre = C + pre
        else:
            pro = pro + C
S = pre + S + pro
if reverseCount % 2 == 0:
    print(S)
else:
    print(S[::-1])


Error: TLE

Fixed submission:

S = input()
Q = int(input())
reverseCount = 0
for _ in range(Q):
    query = input()
    if len(query) == 1:
        reverseCount += 1
    else:
        _, F, C = query.split(" ")
        if (int(F) + reverseCount) % 2 == 1:
            S = C + S
        else:
            S = S + C
if reverseCount % 2 == 0:
    print(S)
else:
    print(S[::-1])
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

s = input()
q = int(input())
rev = False
L = ""
R = ""
for _ in range(q):
    query = input()
    if query == "1":
        rev = not rev
    else:
        _, f, c = query.split(" ")
        if f == "1":
            if rev:
                R = R + c
            else:
                L = c + L
        else:
            if rev:
                L = c + L
            else:
                R = R + c

if rev:
    s = s[::-1]
    L = L[::-1]
    R = R[::-1]
    print(R + s + L)
else:
    print(L + s + R)


Error: TLE

Fixed submission:

s = input()
q = int(input())
rev = False
for _ in range(q):
    query = input()
    if query == "1":
        rev = not rev
    else:
        _, f, c = query.split(" ")
        if f == "1":
            if rev:
                s = s + c
            else:
                s = c + s
        else:
            if rev:
                s = c + s
            else:
                s = s + c

if rev:
    s = s[::-1]
print(s)
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

import collections


class Formatter:
    def __init__(self, s):
        self._d = collections.deque(s)
        self._d_reverse = collections.deque(reversed(s))

    def reverse(self):
        self._d, self._d_reverse = self._d_reverse, self._d

    def append(self, s):
        self._d.append(s)
        self._d_reverse.appendleft(s)

    def appendleft(self, s):
        self._d.appendleft(s)
        self._d_reverse.append(s)

    def __iter__(self):
        return iter(self._d)


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

d = Formatter(S)

for _ in range(Q):
    Qi = input()
    if Qi == "1":
        d.reverse()
    else:
        T, F, C = Qi.split()
        if F == "1":
            d.appendleft(C)
        else:
            d.append(C)

print("".join(d))


Error: TLE

Fixed submission:

import collections

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

d = collections.deque(S)

for _ in range(Q):
    Qi = input()
    if Qi == "1":
        d.reverse()
    else:
        T, F, C = Qi.split()
        if F == "1":
            d.appendleft(C)
        else:
            d.append(C)

print("".join(d))
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

s = str(input())
n = int(input())

stat = 0
front = ""
back = ""

for _ in range(n):
    q = input()

    # 反転の場合
    if q[0] == "1":
        stat = (stat + 1) % 2
        continue

    judge = int(q[2])
    if stat == 1:
        if judge == 1:
            judge = 2
        elif judge == 2:
            judge = 1

    if judge == 1:
        front = q[4] + front

    if judge == 2:
        back = back + q[4]

s = front + s + back
if stat == 1:
    s = s[::-1]

print(s)


Error: TLE

Fixed submission:

s = str(input())
n = int(input())

for _ in range(n):
    q = input()

    # 反転の場合
    if q[0] == "1":
        s = s[::-1]
        continue

    # q = q.split()

    if q[2] == "1":
        s = q[4] + s

    if q[2] == "2":
        s = s + q[4]

print(s)
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

# 先頭と末尾を保ちつつ
from collections import deque

s = input()
q = int(input())
x, y = deque(), deque()
r = 0
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        r += 1
        x, y = y, x
    elif query[0] == "2":
        f, c = query[1], query[2]
        if f == "1":
            if r % 2 == 0:
                x.appendleft(c)
            else:
                x.append(c)
        elif f == "2":
            if r % 2 == 1:
                y.appendleft(c)
            else:
                y.append(c)

if r % 2 == 1:
    s = s[::-1]
    x = reversed(x)
    y = reversed(y)
print("".join(x) + s + "".join(y))


Error: TLE

Fixed submission:

# 先頭と末尾を保ちつつ
s = input()
q = int(input())
x, y = [], []
r = 0
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        r += 1
        # これ遅いか？
        x, y = y[::-1], x[::-1]
    elif query[0] == "2":
        f, c = query[1], query[2]
        if f == "1":
            # x.append(c)
            x = x[::-1]
            x.append(c)
            x = x[::-1]
        elif f == "2":
            y.append(c)
if r % 2 == 1:
    s = s[::-1]
print("".join(x) + s + "".join(y))
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

s = input()
num_query = int(input())

former = ""
latter = ""
is_rev = False

for _ in range(0, num_query):
    query = input()
    if query[0] == "1":
        is_rev = not is_rev
    else:
        pos = query[2]
        character = query[4]
        if pos == "1":
            if is_rev:
                latter += character
            else:
                former = character + former
        else:
            if is_rev:
                former = character + former
            else:
                latter += character

if is_rev:
    ans = latter[::-1] + s[::-1] + former[::-1]
else:
    ans = former + s + latter
print(ans)


Error: TLE

Fixed submission:

s = input()
num_query = int(input())

for _ in range(0, num_query):
    query = input()
    if query[0] == "1":
        s = s[::-1]
    else:
        _, pos, character = query.split()
        if pos == "1":
            s = character + s
        else:
            s = s + character
print(s)
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

# coding: utf-8

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


heads = {}
tails = {}
h = 0
t = 0
rev = False
for _ in range(Q):
    line = input()

    if line[0] == "1":
        rev = not rev

    else:
        if line[2] == "1":
            if rev:
                # S = "".join([S, line[4]])
                tails[t] = line[4]
                t += 1
            else:
                # S = "".join([line[4], S])
                heads[h] = line[4]
                h += 1
        else:
            if rev:
                # S = "".join([line[4], S])
                heads[h] = line[4]
                h += 1
            else:
                # S = "".join([S, line[4]])
                tails[t] = line[4]
                t += 1

head = [None] * h
tail = [None] * t

for i in range(h):
    head[i] = heads[i]
head = head[::-1]

for i in range(t):
    tail[i] = tails[i]

head_joint = "".join(head)
tail_joint = "".join(tail)
joint = "".join([head_joint, S, tail_joint])

if rev:
    print(joint[::-1])
else:
    print(joint)


Error: TLE

Fixed submission:

# coding: utf-8

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


rev = False
for _ in range(Q):
    line = input()

    if line[0] == "1":
        rev = not rev

    else:
        if line[2] == "1":
            if rev:
                S = "".join([S, line[4]])
            else:
                S = "".join([line[4], S])
        else:
            if rev:
                S = "".join([line[4], S])
            else:
                S = "".join([S, line[4]])

if rev:
    print(S[::-1])
else:
    print(S)
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

s = input()
q = int(input())
status = 1
top = []
bottom = []
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):
            top.append(query[2])
        else:
            bottom.append(query[2])

tmp = "".join(top[::-1]) + s + "".join(bottom)
print(tmp if status == 1 else tmp[::-1])


Error: TLE

Fixed submission:

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 == 1 else s[::-1])
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import deque

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

S = deque(S)
rev = 1
for _ in range(Q):
    query = input()
    if len(query) == 1:
        rev *= -1
    else:
        T, F, C = query.split()
        if rev == 1:
            if F == "1":
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if F == "1":
                S.append(C)
            else:
                S.appendleft(C)

if rev == -1:
    S = reversed(S)

print("".join(S))


Error: TLE

Fixed submission:

S = input()
Q = int(input())
rev = 1
for _ in range(Q):
    query = input()
    if len(query) == 1:
        rev *= -1
    else:
        T, F, C = query.split()
        if rev == 1:
            if F == "1":
                S = C + S
            else:
                S = S + C
        else:
            if F == "1":
                S = S + C
            else:
                S = C + S

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

print(S)
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

s = input()
N = int(input())
Q = [input().split() for _ in range(N)]

head = ""
tail = ""
reverse = 0

for q in Q:
    if int(q[0]) == 1:
        reverse = 1 - reverse
    elif int(q[1]) == 1:
        if reverse:
            tail += q[2]
        else:
            head += q[2]
    else:
        if reverse:
            head += q[2]
        else:
            tail += q[2]

if reverse:
    print(tail[::-1] + s[::-1] + head)
else:
    print(head[::-1] + s + tail)


Error: TLE

Fixed submission:

s = input()
N = int(input())
Q = [input().split() for _ in range(N)]

for q in Q:
    if int(q[0]) == 1:
        s = s[::-1]
    elif int(q[1]) == 1:
        s = q[2] + s
    else:
        s = s + q[2]

print(s)
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

import collections

s = collections.deque(list(input()))
q = int(input())
rev = 0  # 反転するなら1そうでないなら0
for i in range(q):
    tfc = list(input().split())
    if tfc[0] == "1":
        rev = abs(rev - 1)  # revが入力されるたびに1か0に変更される
    else:
        t, f, c = tfc
        if rev:
            if f == "1":
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if f == "1":
                s.appendleft(c)
            else:
                s.append(c)
if rev:  # 反転される場合逆向きに出力されなければいけない
    t = []
    for i in range(len(s)):
        t.append(s.pop())
    print("".join(t))
else:
    print("".join(s))


Error: TLE

Fixed submission:

s = input()
n = int(input())
Q = [list(map(str, input().split())) for i in range(n)]

for i in range(n):
    op = Q[i]
    if len(op) == 1:
        s = s[::-1]
    elif op[1] == "1":
        s = op[2] + s
    else:
        s = s + op[2]
print(s)
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import deque

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

d = deque()
for v in s:
    d.append(v)
r = False
for _ in range(q):
    xs = input().split()
    if xs[0] == "1":
        r = not r
    else:
        if (not r and xs[1] == "1") or (r and xs[1] == "2"):
            d.appendleft(xs[2])
        else:
            d.append(xs[2])
s = "".join(d)
print(s[::-1] if r else s)


Error: TLE

Fixed submission:

s = input()
q = int(input())
r = False
for _ in range(q):
    xs = input().split()
    if xs[0] == "1":
        r = not r
    else:
        if (not r and xs[1] == "1") or (r and xs[1] == "2"):
            s = "".join([xs[2], s])
        else:
            s = "".join([s, xs[2]])
print(s[::-1] if r else s)
Problem p02756 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import deque

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

flag = 0
for _ in range(Q):
    q = input()
    if q == "1":
        flag += 1
    else:
        _, t, c = q.split()
        if flag % 2 == 0:
            if t == "1":
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if t == "2":
                d.appendleft(c)
            else:
                d.append(c)

if flag % 2 == 1:
    d.reverse()
print("".join(list(d)))


Error: TLE

Fixed submission:

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

flag = 0
for _ in range(Q):
    q = input()
    if q == "1":
        flag += 1
    else:
        _, t, c = q.split()
        if flag % 2 == 0:
            if t == "1":
                S.insert(0, c)
            else:
                S.append(c)
        else:
            if t == "2":
                S.insert(0, c)
            else:
                S.append(c)

if flag % 2 == 1:
    S.reverse()
print("".join(S))