alexjercan/bugnet huggingface dataset:
Problem p03338 (Python) — buggy submission, verdict: Runtime Error

N = int(input())
S = list(input())

ans = 0
for i in range(1, N):
    left = set(S[:i])
    right = set(S[i:])
    both = left & right
    ans = max(len(both), ans)


print(ans)


Error: TypeError: 'str' object cannot be interpreted as an integer
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03338/Python/s796551276.py", line 5, in <module>
    for i in range(1, N):
TypeError: 'str' object cannot be interpreted as an integer


Fixed submission:

N = input()
S = list(input())

ans = 0
for i in range(1, N):
    left = set(S[:i])
    right = set(S[:i])

    both = left & right
    ans = max(len(both), ans)


print(ans)
Problem p03338 (Python) — buggy submission, verdict: Runtime Error

n = int(input())
s = input()
ans = max(len(set(s[:i]) & set(s[i:])) for i in range(1, n))
print(ans)


Error: 0

Fixed submission:

n = int(input())
s = input()
ans = max(len(set(s[:i]) & set(s[i:])) for i in range(1, n - 1))
print(ans)
Problem p03339 (Python) — buggy submission, verdict: Time Limit Exceeded

N = int(input())
S = input()
x = 0
a = 0
idx = 0

for i in range(N):
    if S[i] == "W":
        x += 1
    else:
        x -= 1

    if x < a:
        a = x
        idx = i

w = S[0:idx].count("W")
e = S[idx + 1 : N].count("E")
print(w + e)


Error: TLE

Fixed submission:

N = int(input())
S = input()

ret = N
for i in range(N):
    a = S[:i]
    b = S[i + 1 :]
    n = a.count("W") + b.count("E")
    if n == 0:
        continue
    ret = min(ret, n)
print(ret)
Problem p03339 (Python) — buggy submission, verdict: Time Limit Exceeded

# coding: utf-8

n = int(input())
s = list(input())

for (i, x) in enumerate(s):
    if x == "E":
        s[i] = 1
    elif x == "W":
        s[i] = 0

left = 0
right = sum(s[1:n])
center = s[0]
min_changed_num = right

for i in range(1, n):
    left = left + (1 - center)
    center = s[i]
    right = right - center
    changed_num = left + right
    if min_changed_num > changed_num:
        min_changed_num = changed_num

print(min_changed_num)


Error: TLE

Fixed submission:

# coding: utf-8

n = int(input())
s = list(input())

for (i, x) in enumerate(s):
    if x == "E":
        s[i] = 1
    elif x == "W":
        s[i] = 0

min_changed_num = sum(s[1:n])

for i in range(1, n):
    left = i - sum(s[0:i])
    right = sum(s[i:n])
    changed_num = left + right
    if min_changed_num > changed_num:
        min_changed_num = changed_num

print(min_changed_num)
Problem p03339 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
s = input()
wcount = 0
wecount = 0
prev = ""
for c in s:
    if c == "W":
        if prev == "E":
            wcount = 0
        wcount += 1
    if wcount and c == "E":
        wcount -= 1
        wecount += 1
print(wecount)


Error: TLE

Fixed submission:

n = int(input())
s = input()
while s.count("WE"):
    s = s.replace("WE", "")
ans = (n - len(s)) // 2
print(ans)
Problem p03339 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys
from functools import reduce
import copy
import math


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    persons = list(inputs[0])

    count = [persons[1:].count("E")]

    for i in range(1, len(persons)):
        if i == 0:
            continue

        tmp = count[-1]
        if persons[i - 1] == "W":
            tmp += 1
        if persons[i] == "E":
            tmp -= 1
        count.append(tmp)

    return min(count)


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)


Error: TLE

Fixed submission:

def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    persons = list(map(lambda x: x, inputs[0]))
    N = len(persons)
    counts = []
    for leader, _ in enumerate(persons):
        count = 0
        for p in range(0, leader):
            if persons[p] == "W":
                count += 1
        for p in range(leader + 1, N):
            if persons[p] == "E":
                count += 1
        counts.append(count)
    min = counts[0]
    for count in counts:
        if min > count:
            min = count

    return min


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)
Problem p03339 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
S = input()
s = [0 for i in range(n)]

for i in range(n):
    if S[i] == "E":
        s[i] = 1
min = n

count = 0
for j in range(n):
    i = 0
    if i < j and s[j] == 1:
        count += 1
    elif i > j and s[j] == 0:
        count += 1
if count < min:
    min = count

for i in range(1, n):
    if s[i - 1] == 0:
        count += 1
    if s[i] == 1:
        count -= 1
    if count < min:
        min = count
print(min)


Error: TLE

Fixed submission:

n = int(input())
S = input()
s = [0 for i in range(n)]

for i in range(n):
    if S[i] == "E":
        s[i] = 1
min = n

for i in range(n):
    count = 0
    for j in range(n):
        if i < j and s[j] == 1:
            count += 1
        elif i > j and s[j] == 0:
            count += 1
    if count < min:
        min = count
print(min)
Problem p03339 (Python) — buggy submission, verdict: Runtime Error

s = ["", ""]

s[0] = input()
s[1] = input()

WtoE = []
wtoec = 0
EtoW = []
etowc = 0

for c in s[1]:
    if c == "W":
        wtoec = wtoec + 1
    WtoE.append(wtoec)

for c in s[1][::-1]:
    if c == "E":
        etowc = etowc + 1
    EtoW.append(etowc)
EtoW.reverse()

a = []
for i in range(0, int(s[0])):
    ans = WtoE[i] + EtoW[i]
    if WtoE[i] != 0 and s[1][i] == "W" or EtoW[i] != 0 and s[1][i] == "E":
        ans = ans - 1

    a.append(ans)

print(min(a))


Error: IndexError: list index out of range
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03339/Python/s685616573.py", line 8, in <module>
    for c in s[1]:
IndexError: list index out of range


Fixed submission:

s = input().split()

WtoE = []
wtoec = 0
EtoW = []
etowc = 0

for c in s[1]:
    if c == "W":
        wtoec = wtoec + 1
    WtoE.append(wtoec)

for c in s[1][::-1]:
    if c == "E":
        etowc = etowc + 1
    EtoW.append(etowc)
EtoW.reverse()
print(WtoE)
print(EtoW)

a = []
for i in range(0, int(s[0])):
    ans = WtoE[i] + EtoW[i]
    if WtoE[i] != 0 and s[1][i] == "W" or EtoW[i] != 0 and s[1][i] == "E":
        ans = ans - 1

    a.append(ans)

print(min(a))
Problem p03339 (Python) — buggy submission, verdict: Runtime Error

N = int(input())
S = input()
E_cost = S.count("E")
W_cost = 0
ans = N
for s in S:
    if s == "E":
        E_cost -= 1
    ans = min(ans, E_cost + W_cost)
    if s == "W":
        W_cost += 1
print(ans)


Error: 0

Fixed submission:

N = int(input())
S = input()
S_list = list(S)
face = [-1, 1]
ans_index = N // 2
while ans_index != N - 1 and S[ans_index] == S[ans_index + face[S[ans_index] == "E"]]:
    ans_index += face[S[ans_index] == "E"]
# print(ans_index)
ans = 0
for i in range(len(S)):
    if i < ans_index and S[i] == "W":
        ans += 1
    elif ans_index == i:
        continue
    elif i > ans_index and S[i] == "E":
        ans += 1
print(ans)
Problem p03341 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
s = list(input())
c = s[1:].count("E")
ans = c
for i in range(1, n):
    c -= 1 if s[i] == "E" else 0
    c += 1 if s[i - 1] == "W" else 0
    ans = min(c, ans)
print(ans)


Error: TLE

Fixed submission:

n = int(input())
s = list(input())
cmin = n + 1
for i in range(n):
    c = s[:i].count("W") + s[i:].count("E")
    if cmin > c:
        cmin = c
print(cmin)
Problem p03346 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys

N = int(sys.stdin.readline())
P = {int(sys.stdin.readline()): i for i in range(N)}
Q = [P[i] for i in range(1, N + 1)]

ans = 0
prv_q = 0
now = 0
for i in range(N):
    if prv_q > Q[i]:
        now = 0
    now += 1
    prv_q = Q[i]
    ans = max(ans, now)
print(N - ans)


Error: TLE

Fixed submission:

import sys

N = int(sys.stdin.readline())
P = [int(sys.stdin.readline()) for _ in range(N)]
Q = [P.index(i) for i in range(1, N + 1)]
ans = 0
prv_q = 0
now = 0
for i in range(N):
    if prv_q > Q[i]:
        now = 0
    now += 1
    prv_q = Q[i]
    ans = max(ans, now)
print(N - ans)
Problem p03346 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
p = [0 for i in range(n)]
for i in range(n):
    p[i] = int(input())

visited = [0 for i in range(n + 1)]

"""
for i in reversed(range(n)):
    if visited[i] == 0:
        visited[i] = 1
        count = 1
        target = p[i] - 1

        for j in reversed(range(i)):
            if p[j] == target:
                count += 1
                visited[j] = count
                target -= 1
"""
for i in range(n):
    visited[p[i]] = visited[p[i] - 1] + 1
print(n - max(visited))


Error: TLE

Fixed submission:

n = int(input())
p = [0 for i in range(n)]
for i in range(n):
    p[i] = int(input())

visited = [0 for i in range(n)]
n_range = range(n)
for i in reversed(n_range):
    if visited[i] == 0:
        visited[i] = 1
        count = 1
        target = p[i] - 1
        for j in reversed(range(i)):
            if p[j] == target:
                count += 1
                visited[j] = count
                target -= 1
print(n - max(visited))
Problem p03346 (Python) — buggy submission, verdict: Runtime Error

n = int(input())
ppp = list(map(int, (input() for _ in range(n))))

tmp = [0] * (n + 1)
for p in ppp:
    tmp[p] = tmp[p - 1] + 1

print(n - max(tmp))


Error: IndexError: list assignment index out of range
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03346/Python/s389965586.py", line 6, in <module>
    tmp[p] = tmp[p - 1] + 1
IndexError: list assignment index out of range


Fixed submission:

n = int(input())
ppp = list(map(int, (input() for _ in range(n))))

tmp = [0] * n
for p in ppp:
    tmp[p] = tmp[p - 1] + 1

print(n - max(tmp))
Problem p03347 (Python) — buggy submission, verdict: Time Limit Exceeded

def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    a0 = 0

    for a1 in aaa[1:]:
        if a1 - a0 != 1:
            ans += a0
        a0 = a1

    return ans + a0


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))


Error: TLE

Fixed submission:

def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    aaa.reverse()

    while any(aaa):
        while aaa[-1] == 0:
            aaa.pop()
        aaa.reverse()
        while aaa[-1] == 0:
            aaa.pop()
        aaa.reverse()

        new = [0] * (len(aaa) - 1)
        a1 = aaa[0]
        ap = 0
        for i, a0 in enumerate(aaa[1:]):
            if a1 == 0:
                a1 = a0
                continue
            ans += 1
            if a1 - a0 < 1:
                new[i] = ap = max(a1 - 1, ap)
            else:
                new[i] = ap
            a1 = a0
            if ap:
                ap -= 1
        if a1:
            ans += 1
        aaa = new
    return ans


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))
Problem p03351 (Python) — buggy submission, verdict: Runtime Error

a, b, c, d = map(int, input().split())
x = sorted([a, b])
y = sorted([c, b])

if (max(a, c) - min(a, c)) <= d:
    print("Yes")
elif (x[-1] - x[0]) <= d and (y[-1] - y[0]) <= d:
    print("Yes")
else:
    print("No")


Error: TypeError: sorted expected 1 argument, got 2
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03351/Python/s078991385.py", line 2, in <module>
    x = sorted(a, b)
TypeError: sorted expected 1 argument, got 2


Fixed submission:

a, b, c, d = map(int, input().split())
x = sorted(a, b)
y = sorted(c, b)

if (x[-1] - x[0]) <= d and (y[-1] - y[0]) <= d:
    print("Yes")
else:
    print("No")
Problem p03352 (Python) — buggy submission, verdict: Time Limit Exceeded

x = int(input())
ans = 1
for i in range(2, 1000):
    j = 2
    while i**j <= x:
        ans = max(ans, i**j)
        j += 1
print(ans)


Error: TLE

Fixed submission:

x = int(input())
ans = 1
for i in range(2, 1000):
    j = 2
    while i**j <= x:
        ans = max(ans, i**j)
    j += 1
    if i > x:
        break
print(ans)
Problem p03352 (Python) — buggy submission, verdict: Time Limit Exceeded

x = int(input())
li = [False] * (x + 1)
li[1] = True

for i in range(x):
    for j in range(2, x):
        if i**j > x:
            break
        li[i**j] = True

for i in reversed(range(x + 1)):
    if li[i]:
        print(i)
        break


Error: TLE

Fixed submission:

def pow(x, n):
    if n == 0:
        return 1
    value = pow(x, int(n / 2))
    value *= value
    if n % 2 == 1:
        value *= x
    return value


x = int(input())
li = [False] * (x + 1)
li[1] = True

for i in range(x):
    for j in range(2, x):
        idx = pow(i, j)
        if idx < x + 1:
            li[idx] = True

for i in reversed(range(x + 1)):
    if li[i]:
        print(i)
        break
Problem p03352 (Python) — buggy submission, verdict: Time Limit Exceeded

X = int(input())

ans = 1
for i in range(1, X):
    for j in range(2, X):
        tmp = i**j
        if tmp > X:
            break
        ans = max(tmp, ans)
print(ans)


Error: TLE

Fixed submission:

X = int(input())

for i in range(X, 0, -1):
    tmp = i
    if X % tmp:
        continue
    while tmp <= X:
        tmp *= tmp
    if tmp == X:
        print(i)
Problem p03352 (Python) — buggy submission, verdict: Time Limit Exceeded

x = int(input())

if x == 1:
    print("1")
    exit()


def func(p):
    q = p
    while q < x:
        q *= p
        if q == x:
            return q
    if q / p == p:
        return 1
    else:
        return q // p


z = []
a = 2

while a <= x:
    z.append(func(a))
    a += 1

print(max(z))


Error: TLE

Fixed submission:

x = int(input())

if x == 1:
    print("1")
    exit()


def func(p):
    q = p
    while q < x:
        q *= p
        if q == x:
            return q
            break

    if q / p == p:
        return 1
    else:
        return q / p


z = []
a = 1

while a <= x:
    z.append(func(a))
    a += 1

print(max(z))
Problem p03352 (Python) — buggy submission, verdict: Time Limit Exceeded

X = int(input())

ans = []
for i in range(1, 1000):
    for j in range(2, 11):
        if i**j > X:
            continue
        ans.append(i**j)
print(max(ans))


Error: TLE

Fixed submission:

X = int(input())

ans = []
for i in range(1000):
    for j in range(1000):
        if i**j > X:
            continue
        ans.append(i**j)
print(max(ans))
Problem p03353 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    S = input()
    K = int(input())
    result = set()

    for i in range(5):
        for j in range(len(S)):
            result.add(S[j : j + i + 1])

    print(sorted(result)[K - 1])


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    S = input()
    K = int(input())
    result = set()

    for i in range(len(S) + 1):
        for j in range(i + 1, len(S) + 1):
            result.add(S[i:j])

    print(sorted(result)[K - 1])


if __name__ == "__main__":
    main()
Problem p03353 (Python) — buggy submission, verdict: Time Limit Exceeded

s = str(input())
K = int(input())
# sがabcac の場合、a,bca,ac などは部分列(substring
# abc とabの場合、abの方が辞書順で小さいと判断する。
# abc とabaの場合、abaの方が小さい

substrings = set([])
for i in range(5):
    for j in range(len(s) - i):
        # print(i, j)
        substrings.add(s[j : j + i + 1])
# print(substrings)
print(sorted(substrings)[K - 1])


Error: TLE

Fixed submission:

s = str(input())
K = int(input())
# sがabcac の場合、a,bca,ac などは部分列(substring
# abc とabの場合、abの方が辞書順で小さいと判断する。
# abc とabaの場合、abaの方が小さい

substrings = set([])
for i in range(len(s) + 1):
    for j in range(i + 1, len(s) + 1):
        # print(i, j)
        substrings.add(s[i:j])

print(sorted(substrings)[K - 1])
Problem p03353 (Python) — buggy submission, verdict: Time Limit Exceeded

str_s = input()
int_k = int(input())

result = list()
for f in range(len(str_s)):
    pattern = set()
    for t in range(f + 1, f + 1 + int_k):
        pattern.add(str_s[f:t])
        result = sorted(set(result + list(pattern)))[:int_k]

print(result[-1])


Error: TLE

Fixed submission:

str_s = input()
int_k = int(input())

result = list()
for f in range(len(str_s)):
    pattern = set()
    if len(result) == int_k and str_s[f] > result[-1][0]:
        continue
    for t in range(f + 1, len(str_s) + 1):
        pattern.add(str_s[f:t])
        result = sorted(set(result + sorted(list(pattern))[:int_k]))[:int_k]

print(result[-1])
Problem p03353 (Python) — buggy submission, verdict: Time Limit Exceeded

# https://atcoder.jp/contests/abc097/tasks/arc097_a


# 辞書順に置いては文字の先頭が重要。
# 一文字目が速いやつから注目していく。
# 一文字目が同じだったらさらにその次の文字を注目する。

# atcoderandatcodeer
# a      a  a       # 1種
# at     an at      # 1種 #tは以下のすべてよりも辞書順があと
#        and        # 1種
#        anda       # 1種
#        andat      #

S = input()
K = int(input())

# 一番速い文字を検索してからそこからK文字見るのが一番早そう。
# ただし、K文字ない場合は辻褄合わせが必要
# てか計算量的に、こんなことしなくてもK文字目まで全列挙すればよくね？

# mi = min(S)
candi = []
for s in range(len(S)):
    for add in range(1, K + 1):
        candi.append(S[s : s + add])

candi = list(set(candi))
candi.sort()
print(candi[K - 1])


Error: TLE

Fixed submission:

# https://atcoder.jp/contests/abc097/tasks/arc097_a
# まずは愚直に実装してみる

S = input()
K = int(input())
candi = []
for s in range(len(S)):
    for e in range(s + 1, len(S) + 1):
        candi.append(S[s:e])
candi = list(set(candi))
candi.sort()
print(candi[K - 1])
Problem p03353 (Python) — buggy submission, verdict: Runtime Error

s = input()
K = int(input())
subs = []
for i in range(0, len(s)):
    for j in range(i + 1, K + i + 1 if i + K <= len(s) else len(s) + 1):
        subs.append(s[i:j])


subs = list(set(subs))

subs.sort()
print(subs[K - 1])


Error: 0

Fixed submission:

s = input()
K = int(input())
result = []
alphabet = list("abcdefghijklmnopqrstuvwxyz")

for u in alphabet:
    for i in range(len(s)):
        if s[i] == u:
            for j in range(i + 1, min(i + K + 1, len(s) + 1)):
                result.append(s[i:j])
    if len(result) > K:
        break

result = list(set(result))
result.sort()
# print(result)
# print(len(result))
print(result[K - 1])
Problem p03353 (Python) — buggy submission, verdict: Runtime Error

s = input()
k = int(input())
n = len(list(s))
kouho = set()
for i in range(n):
    for d in range(1, k + 1):
        if i + d <= n:
            kouho.add(s[i : i + d])
ans = list(kouho)
ans.sort()
# print(ans)
print(ans[k - 1])


Error: IndexError: list index out of range
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03353/Python/s674016265.py", line 11, in <module>
    print(ans[k - 1])
IndexError: list index out of range


Fixed submission:

s = input()
k = int(input())
n = len(list(s))
kouho = set()
for i in range(n):
    for d in range(1, k + 1):
        if i + d < n:
            kouho.add(s[i : i + d])
ans = list(kouho)
ans.sort()
print(ans[k - 1])
Problem p03353 (Python) — buggy submission, verdict: Time Limit Exceeded

s = input()
K = int(input())

S = set([])
for i in range(len(s)):
    for j in range(i + 1, i + 1 + K):
        # print(f'{s[i:j]=}')
        S.add(s[i:j])
# print(f'{S=}')
# print(f'{sorted(S)=}')
ans = sorted(S)[K - 1]
print(ans)


Error: TLE

Fixed submission:

s = input()
K = int(input())

S = set([])
for i in range(len(s)):
    for j in range(i + 1, len(s) + 1):
        # print(f'{s[i:j]=}')
        S.add(s[i:j])
# print(f'{S=}')
# print(f'{sorted(S)=}')
ans = sorted(S)[K - 1]
print(ans)
Problem p03353 (Python) — buggy submission, verdict: Runtime Error

s = input()
K = int(input())
alphabet = list("abcdefghijklmnopqrstuvwxyz")
result = []

for u in alphabet:
    for i in range(len(s)):
        if s[i] == u:
            for j in range(i + 1, min(i + K + 1, len(s) + 1)):
                result.append(s[i:j])
    result = list(set(result))
    if len(result) > K:
        break

result.sort()
print(result[K - 1])


Error: 0

Fixed submission:

s = input()
K = int(input())
alphabet = list("abcdefghijklmnopqrstuvwxyz")
result = []

for u in alphabet:
    for i in range(len(s)):
        if s[i] == u:
            for j in range(i + 1, min(i + K + 1, len(s) + 1)):
                result.append(s[i:j])
    if len(result) > K:
        break

result = list(set(result))
result.sort()
# print(result)
# print(len(result))
print(result[K - 1])
Problem p03353 (Python) — buggy submission, verdict: Time Limit Exceeded

s = input()
K = int(input())

s_set = set()
for i in range(len(s)):
    for j in range(i + 1, i + K + 1):
        if j > len(s) + 1:
            break
        s_set.add(s[i:j])
s_list = list(s_set)
s_list.sort()
print(s_list[K - 1])


Error: TLE

Fixed submission:

s = input()
K = int(input())

s_set = set()
for i in range(len(s) + 1):
    for j in range(i + 1, len(s) + 1):
        s_set.add(s[i:j])
s_list = list(s_set)
s_list.sort()
print(s_list[K - 1])
Problem p03353 (Python) — buggy submission, verdict: Time Limit Exceeded

s = input()
k = int(input())
a = set()
for i in range(len(s) + 1):
    for j in range(1, 6):
        a.add(s[i : i + j])
print(sorted(list(a))[k])


Error: TLE

Fixed submission:

s = input()
k = int(input())
a = set()
for i in range(len(s)):
    for j in range(i + 1, len(s) + 1):
        a.add(s[i:j])
print(sorted(list(a))[k - 1])
Problem p03353 (Python) — buggy submission, verdict: Time Limit Exceeded

from heapq import nsmallest

s = input()
K = int(input())
subs = set()
for i in range(len(s)):
    for j in range(i + 1, min(i + 6, len(s) + 1)):
        subs.add(s[i:j])
arr = list(subs)
print(nsmallest(K, arr)[-1])


Error: TLE

Fixed submission:

from heapq import nsmallest

s = input()
K = int(input())
subs = set()
for i in range(len(s)):
    for j in range(i + 1, len(s) + 1):
        subs.add(s[i:j])
arr = list(subs)
print(nsmallest(K, arr)[-1])
Problem p03353 (Python) — buggy submission, verdict: Time Limit Exceeded

S = input()
K = int(input())
N = len(S)
substrs = set()

for i in range(N):
    cur = S[i]
    if cur not in substrs:
        substrs.add(cur)
    for j in range(i + 1, min(N, i + 6)):
        cur += S[j]
        if cur not in substrs:
            substrs.add(cur)

substrs = sorted(substrs)
print(substrs[K - 1])


Error: TLE

Fixed submission:

S = input()
K = int(input())
N = len(S)
substrs = set()

for i in range(N):
    cur = S[i]
    if cur not in substrs:
        substrs.add(cur)
    for j in range(i + 1, N):
        cur += S[j]
        if cur not in substrs:
            substrs.add(cur)

substrs = sorted(substrs)
print(substrs[K - 1])
Problem p03353 (Python) — buggy submission, verdict: Time Limit Exceeded

s = str(input())
k = int(input())
subs = set()
for i in range(len(s)):
    for j in range(i + 1, i + k + 1):
        subs.add(s[i:j])
print(sorted(list(subs))[k - 1])


Error: TLE

Fixed submission:

s = str(input())
k = int(input())
subs = set()
for i in range(len(s)):
    for j in range(i + 1, len(s) + 1):
        subs.add(s[i:j])
print(sorted(list(subs))[k - 1])