Problem: You are given a string s. Among the **different** substrings of s, print the K-th lexicographically smallest one.  A substring of s is a string obtained by taking out a non-empty contiguous part in s. For example, if s = `ababc`, `a`, `bab` and `ababc` are substrings of s, while `ac`, `z` and an empty string are not. Also, we say that substrings are different when they are different as strings.  Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Submission attempts (in order):
from collections import Counter

s = input()
K = int(input())
c = Counter(s)

key = sorted(c.keys())
ans = []
queue = []

while (len(ans) < K):
    if queue:
        queue = sorted(list(set(queue)))
        res = queue.pop(0)
        ans.append(res)
        ans = list(set(ans))
    else:
        c = key.pop(0)
        queue.append(c)
        ind = [i for i in range(len(s)) if s[i] == c]
        for i in ind:
            for j in range(min(5, len(s) - i - 1)):
                queue.append(s[i:i+j+1])

print(ans[K-1])


from collections import Counter

s = input()
K = int(input())
c = Counter(s)

key = sorted(c.keys())
ans = []
queue = []

while (len(ans) < K):
    if queue:
        
        queue = sorted(list(set(queue)))
        res = queue.pop(0)
        ans.append(res)
        ans = list(set(ans))
    else:
        c = key.pop(0)
        #print("pop", c)
        queue.append(c)
        ind = [i for i in range(len(s)) if s[i] == c]
        for i in ind:
            for j in range(min(5, len(s) - i)):
                queue.append(s[i:i+j+1])

print(ans[K-1])


from heapq import heappush, heappop

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

min_index = [n for n, v in enumerate(s) if v == min(s)]

ans_list = [min(s)]
queue = []
i = 2

while len(ans_list) < K:
    
    for m in min_index:
        for i in range(1, 6):
            if m + i < len(s):
                heappush(queue, s[m:m+i])
    while queue:
        c = heappop(queue)
        if c not in ans_list:
            ans_list.append(c)

print(ans_list[K-1])


from collections import Counter

s = input()
K = int(input())
c = Counter(s)

key = sorted(c.keys())
ans = []
queue = []

while (len(ans) < K):
    if queue:
        queue = sorted(list(set(queue)))
        res = queue.pop(0)
        ans.append(res)
        ans = list(set(ans))
    else:
        c = key.pop(0)
        queue.append(c)
        ind = [i for i in range(len(s)) if s[i] == c]
        for i in ind:
            for j in range(len(s) - i - 1):
                queue.append(s[i:i+j+1])

print(ans[K-1])


from heapq import heappush, heappop

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

min_index = [n for n, v in enumerate(s) if v == min(s)]

ans_list = [min(s)]
queue = []
i = 2

while len(ans_list) < K:
    print("hello", i)
    
    for m in min_index:
        for i in range(1, 6):
            if m + i < len(s):
                heappush(queue, s[m:m+i])
    while queue:
        c = heappop(queue)
        if c not in ans_list:
            ans_list.append(c)

print(ans_list[K-1])


from collections import Counter

s = input()
K = int(input())
c = Counter(s)

key = sorted(c.keys())
ans = []
queue = []

while (len(ans) < K):
    if queue:
        queue = sorted(list(set(queue)))
        res = queue.pop(0)
        ans.append(res)
    else:
        c = key.pop(0)
        queue.append(c)
        ind = [i for i in range(len(s)) if s[i] == c]
        for i in ind:
            for j in range(1, len(s) - i - 1):
                queue.append(s[i:i+j+1])

print(ans[K-1])


from collections import Counter

s = input()
K = int(input())
c = Counter(s)

key = sorted(c.keys())
ans = []
queue = []

while (len(ans) < K):
    if queue:
        
        queue = sorted(list(set(queue)))
        res = queue.pop(0)
        ans.append(res)
        ans = list(set(ans))
    else:
        c = key.pop(0)
        #print("pop", c)
        queue.append(c)
        ind = [i for i in range(len(s)) if s[i] == c]
        for i in ind:
            for j in range(min(5, len(s) - i)):
                queue.append(s[i:i+j+1])

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


Verdicts: Runtime Error

Wrong Answer

Time Limit Exceeded

Runtime Error

Wrong Answer

Runtime Error

Accepted