Problem:


Score : 300 points

Problem Statement

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}.

Constraints

1 ≤ |s| ≤ 5000

s consists of lowercase English letters.

1 ≤ K ≤ 5

s has at least K different substrings.

Partial Score

200 points will be awarded as a partial score for passing the test set satisfying |s| ≤ 50.

Input

Input is given from Standard Input in the following format:

s
K

Output

Print the K-th lexicographically smallest substring of K.

Sample Input 1

aba
4

Sample Output 1

b

s has five substrings: a, b, ab, ba and aba.
Among them, we should print the fourth smallest one, b.
Note that we do not count a twice.

Sample Input 2

atcoderandatcodeer
5

Sample Output 2

andat

Sample Input 3

z
1

Sample Output 3

z



Sample input:
aba
4


Sample output:
b

Submission status: Time Limit Exceeded

Solution code:
import sys
import os
import math
import bisect
import itertools
import collections
import heapq
import queue
import array

# 時々使う
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
# from decimal import Decimal
# from collections import defaultdict, deque

# 再帰の制限設定
sys.setrecursionlimit(10000000)


def ii(): return int(sys.stdin.buffer.readline().rstrip())
def il(): return list(map(int, sys.stdin.buffer.readline().split()))
def fl(): return list(map(float, sys.stdin.buffer.readline().split()))
def iln(n): return [int(sys.stdin.buffer.readline().rstrip())
                    for _ in range(n)]


def iss(): return sys.stdin.buffer.readline().decode().rstrip()
def sl(): return list(map(str, sys.stdin.buffer.readline().decode().split()))
def isn(n): return [sys.stdin.buffer.readline().decode().rstrip()
                    for _ in range(n)]


def lcm(x, y): return (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    S = iss()
    K = ii()

    substring = set()
    for i in range(1, len(S)+1):
        for j in range(i):
            if j == i:
                break
            substring.add(S[j:i])
    ret = sorted(list(substring))
    print(ret[K-1])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
kkk=int(input())
moji=[]
for i in range(len(s)):
  for j in range(len(s)-(i+1)+1):
    a=''
    for k in range(i+1):
      a+=s[j+k]
    if moji.count(a)==0:
      moji.append(a)
b = sorted(moji)
print(b[kkk-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
k=int(input())
ans=[]

for i in range(len(s)):
  for j in range(i+1,len(s)+1):
    ans.append(s[i:j])

ans=list(set(ans))
ans.sort()
print(ans[k-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
k=int(input())
l=[i for i in s]
ans=[i for i in s]
mae=[]
for i in range(len(s)-1):
  for j in range(len(l)-1):
    nlen=i+1
    lj=l[j+1]
    now=l[j]+lj[nlen-1:]
    mae.append(now)

  ans.extend(mae)
  l=mae
  mae=[]

ans=list(set(ans))
ans.sort()
print(ans[k-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
k=int(input())
l=[i for i in s]
ans=[i for i in s]
mae=[]
for i in range(len(s)-1):
  for j in range(len(l)-1):
    nlen=len(l[j])
    lj=l[j+1]
    now=l[j]+lj[nlen-1:]
    mae.append(now)
  ans.extend(mae)
  l=mae
  mae=[]

ans=list(set(ans))
ans.sort()
print(ans[k-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
k=int(input())
l=[i for i in s]
ans=[i for i in s]
mae=[]
for i in range(len(s)-1):
  for j in range(len(l)-1):
    nlen=len(l[j])
    lj=l[j+1]
    now=l[j]+lj[nlen-1:]
    ans.append(now)
    mae.append(now)
  l=mae
  mae=[]

ans=list(set(ans))
ans.sort()
print(ans[k-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
k=int(input())
l=[i for i in s]
ans=[i for i in s]
for i in range(len(s)-1):
  for j in range(len(l)-1):
    nlen=len(l[j])
    lj=l[j+1]
    now=l[j]+lj[nlen-1:]
    ans.append(now)
  l=ans[len(l)-i:]

ans=list(set(ans))
ans.sort()
print(ans[k-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
K = int(input())
def substring(S):
    N = len(S)
    ls = []
    for i in range(1,N+1):
        for j in range(0,N-i+1):
            ls.append(S[j:j+i])
    setls = set(ls)
    ls = list(setls)
    ls.sort()
    return ls
print(substring(s)[K-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
k = int(input())
a = []

for i in range(len(s)):
    for j in range(i+1, len(s)+1):
        a.append(s[i:j])

res = sorted(set(a))
print(res[k-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
lst = set()
l = len(s)
for i in range(l):
    for j in range(i, l):
        lst.add(s[i:j+1])
lst = list(lst)
lst.sort()
print(lst[int(input()) - 1])

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

seq_dic = defaultdict(list)
cnt_dic = defaultdict(int)
l = len(s)

for i, x in enumerate(s):
    target = s[i:]

    cnt = 0
    if x not in seq_dic:
        cnt += len(target)
    else:
        for r in seq_dic[x]:
            for j in range(min(len(target), len(r))):
                if target[j] != r[j]:
                    cnt += len(target) - j
                    break

    seq_dic[x].append(target)
    cnt_dic[x] += cnt

cnt = 0
prev_cnt = 0
moji = ''
for i in range(26):
    moji = chr(97 + i)
    c = cnt_dic[moji]
    cnt += c

    if K <= cnt:
        break

    prev_cnt = cnt


seq = set()
for i, n in enumerate(s):
    if n == moji:
        for j in range(i + 1, l + 1):
            seq.add(s[i:j])

seq = list(seq)
seq.sort()

print(seq[K - prev_cnt - 1])

Submission status: Time Limit Exceeded

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

l = len(s)

seq = set()
for i in range(l):
    for j in range(i + 1, l + 1):
        seq.add(s[i:j])

seq = list(seq)
seq.sort()

print(seq[K - 1])

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
K=int(input())
N=len(S)
l=[]
for k in range(1,N+1):
    for n in range(N):
        tmp=S[n:n+k]
        l.append("".join(tmp))
l.sort()

n_l=[l[0]]
for n in range(1,len(l)):
    if l[n]!=l[n-1]:
        n_l.append(l[n])

print(n_l[K-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
k = int(input())
l = []
for i in range(len(s)):
    for j in range(i+1,len(s)+1):
        l.append(s[i:j])
print(sorted(list(set(l)))[k-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
k = int(input())
li = []
for i in range(len(s)):
    for j in range(i+1,len(s)+1):
        li += [s[i:j]]
li2 = set(li)
li3 = sorted(li2)
print(li3[k-1])

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
k = int(input())
m = 1
w = []
for h in range(len(s)):
    r = ""
    for j in range(m,len(s)+1):
        r += s[h:j]
        w.append(s[h:j])
    m += 1
w = list(set(w))
w.sort()
print(w[k-1])

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
s = input()
n = len(s)
k = int(input())
ij = list(combinations(range(n+1), 2))
#print(ij)
substr = []
for i, j in ij:
    if j > i+k:
        continue
    sub = s[i:j]
    if sub not in substr:
        substr.append(s[i:j])
substr.sort()
print(substr[k-1])

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
s = input()
n = len(s)
k = int(input())
ij = list(combinations(range(n+1), 2))
#print(ij)
substr = []
for i, j in ij:
    sub = s[i:j]
    if sub not in substr:
        substr.append(s[i:j])
substr.sort()
print(substr[k-1])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

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

sets1 = set([])
for i in range(0, len(s)):
    sets1.add(s[i])

sorted_sets1 = sorted(sets1)

for moji in sorted_sets1:
    index = []
    max_len = 0
    for i in range(len(s)):
        if s[i] == moji:
            index.append(i)
            max_len = max(max_len, n-i)

    # print(index)
    setsn = set([])
    for i in range(0, max_len):
        for index_tmp in index:
            setsn.add(s[index_tmp:index_tmp+i+1])
    # print(setsn)
    if len(setsn) >= k:
        print(sorted(setsn)[k-1])
        break
    else:
        k -= len(setsn)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

s = str(input())
k = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

s = str(input())
k = int(input())

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

Submission status: Time Limit Exceeded

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    S = readline().strip()
    K = int(readline())

    subs = set()

    S_sorted = sorted(set(S))
    for c_min in S_sorted:
        indices = [i for i, c in enumerate(S) if c == c_min]
        for i in indices:
            for j in range(i + 1, len(S) + 1):
                subs.add(S[i:j])

        if len(subs) >= K:
            break

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

    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=input()
k=int(input())
right=0
left=0
r=deque([])
while right<len(s):
  r.append(s[right:left+1])
  left+=1
  if left==len(s):
    right+=1
    left=right
print(sorted(set(r))[k-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
k=int(input())
right=0
left=0
r=[]
while right<len(s):
  r.append(s[right:left+1])
  left+=1
  if left==len(s):
    right+=1
    left=right
print(sorted(set(r))[k-1])

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
k1=int(input())
l1=set()
for i in range(len(s)):
    l1.add(s[i])
a1=[]
for i in range(len(l1)):
    a1.append(l1.pop())
a1.sort()
for i in range(len(a1)):
    str=a1[i]
    l2=set()
    for j in range(len(s)):
        if s[j]==str:
            l2.add(s[j])
            str2=s[j]
            for k in range(j+1,len(s)):
                str2+=s[k]
                l2.add(str2)
    if len(l2)>=k1:
        l3=sorted(l2)
        ans=l3[k1-1]
        break
    else:
        k1-=len(l2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
K = int(input())
StringList = []
for TSt in range(0,N):
    for TEd in range(TSt+1,N+1):
        StringList.append(S[TSt:TEd])
SetString = sorted(set(StringList))
print(SetString[K-1])

Submission status: Time Limit Exceeded

Solution code:
L=list()
s=input()
K=int(input())
if len(s)<10:
  for i in range(len(s)):
    for j in range(i+1,len(s)+1):
      L.append(str(s[i:j]))
  L=sorted(list(set(L)))
  print(L[K-1])
else:
  A=sorted(list(s))
  B=[A[0],A[1],A[2],A[3],A[4]]
  for i in range(len(s)):
    if s[i] in B:
      for j in range(i+1,len(s)+1):
        L.append(str(s[i:j]))
      L=sorted(list(set(L)))
      if len(L)>K:
        L=list(L[:K])
  print(L[K-1])

Submission status: Time Limit Exceeded

Solution code:
L=list()
s=input()
K=int(input())
if len(s)<10:
  for i in range(len(s)):
    for j in range(i+1,len(s)+1):
      L.append(str(s[i:j]))
  L=sorted(list(set(L)))
  print(L[K-1])
else:
  A=sorted(list(s))
  B=[A[0],A[1],A[2],A[3],A[4]]
  for i in range(len(s)):
    if s[i] in B:
      for j in range(i+1,len(s)+1):
        L.append(str(s[i:j]))
      L=sorted(list(set(L)))
  print(L[K-1])

Submission status: Time Limit Exceeded

Solution code:
L=list()
s=input()
K=int(input())
for i in range(len(s)):
  for j in range(i+1,len(s)+1):
    L.append(str(s[i:j]))
L=sorted(list(set(L)))
print(L[K-1])

Submission status: Time Limit Exceeded

Solution code:
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])

Submission status: Time Limit Exceeded

Solution code:
S = input()
K = int(input())
S_len = len(S)
ans = set()
for i in range(0, S_len+1):
    for j in range(0, S_len):
        if j + i <= S_len:
            t = S[j:j + i]
            if t != '':
                ans.add(t)
ans = list(ans)
ans.sort()
print(ans[K-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
S = input()
K = int(input())

d = sorted(list(S))
x = set(d)
for c in d:
    left = S.index(c)
    while left < len(S):
        if S[left] != c:
            left += 1
            continue
        j = 2
        while left + j <= len(S):
            x.add(S[left:left+j])
            j += 1
        left += 1
    if len(x) >= K:
        break
x = sorted(list(x))
# print(x)
print(x[K - 1])

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
S = input()
K = int(input())

x = set(list(S))

for w in range(2, len(S) + 1):
    for i in range(len(S)):
        if i + w > len(S):
            continue
        x.add(S[i:i+w])

x = sorted(list(x))
print(x[K - 1])

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
S = input()
K = int(input())

x = set(list(S))

for w in range(2, len(S) + 1):
    for i in range(len(S)):
        if i + w > len(S):
            continue
        x.add(S[i:i+w])


x = sorted(list(x))
print(x[K - 1])

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from collections import defaultdict
def main():
    s = list(input())
    k = int(input())
    k -= 1

    d1 = defaultdict(list)
    for i, c in enumerate(s):
        d1[c].append(i)
    d2 = sorted(d1.items())

    lens = len(s)
    for d2e in d2:
        subs = set()
        subs.add(d2e[0])
        for d2ee in d2e[1]:
            for i1 in range(d2ee+1, lens + 1):
                subs.add("".join(s[d2ee:i1]))
        if len(subs) > k:
            subsl = list(subs)
            subsl.sort()
            print(subsl[k])
            sys.exit()
        else:
            k -= len(subs)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

s = input().rstrip()
K = int(input())
ls = len(s)
cs = collections.deque()
for i in range(ls):
    for j in range(i+1,ls+1):
        cs.append(s[i:j])
os = sorted(cs)
count = 0
lis = []
for i in os:
    if i not in lis:
        lis.append(i)
        count +=1
    else:
        continue
    if count == K:
        print(i)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import sys,math,collections,itertools
input = sys.stdin.readline

s = input().rstrip()
K = int(input())
ls = len(s)
cs = collections.deque()
for i in range(ls):
    for j in range(i+1,ls+1):
        cs.append(s[i:j])
os = sorted(cs)
count = 0
lis = []
for i in os:
    if i not in lis:
        lis.append(i)
        count +=1
    else:
        continue
    if count == K:
        print(i)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import sys,math,collections,itertools
input = sys.stdin.readline

s = input().rstrip()
K = int(input())
ls = len(s)
cs = []
for i in range(ls):
    for j in range(i+1,ls+1):
        cs.append(s[i:j])
os = sorted(cs)
count = 0
lis = []
for i in os:
    if i not in lis:
        lis.append(i)
        count +=1
    else:
        continue
    if count == K:
        print(i)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    s = list(input())
    k = int(input())
    subs = set()
    lens = len(s)
    for i1 in range(lens):
        for i2 in range(lens - i1):
            subs.add("".join(s[i2:i2+i1+1]))
    subs_l = list(subs)
    subs_l.sort()
    print(subs_l[k-1])


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

l=len(s)

se = set( s[i:j+1] for i in range(l) for j in range(i,l) )
print( sorted(se )[k-1] )

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
K = int(input())

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

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

Submission status: Time Limit Exceeded

Solution code:
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)

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
K=int(input())

S_=[]
for s in range(len(S)):
    s_temp=S[s]
    S_.append(s_temp)
    for e in range(s+1,len(S)):
        s_temp+=S[e]
        S_.append(s_temp)
        
S_=list(set(S_))
S_.sort()
# print(S_)
print(S_[K-1])

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = list(input())
    k = int(input())
    lens = len(s)
    subs = []
    for i1 in range(1, lens + 1):
        for i2 in range(0, lens +1 - i1):
            subs.append(tuple(s[i2:i1+i2]))
    subs = set(subs)
    subs = list(subs)
    subs.sort()
    r = subs[k-1]
    print("".join(r))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def calc( s , k , n):

	bruh = {}
	for i  in range(1, n + 1): # length
		for j in range( n - ( i - 1)): # start 
			bruh[s[j: j + i:]] = 0
	another = bruh.keys()
	another = sorted(another)
	print( another[k - 1])

s = input()

k = int(input())

n = len(s)
 
calc( s , k , n)

Submission status: Time Limit Exceeded

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

N = len(S)

memo = set()
for i in range(N):
    for j in range(i, N):
        memo.add(S[i:j + 1])

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
K=int(input())
p=set()
for i in range(len(s)):
  for j in range(i,len(s)):
    p.add(s[i:j+1])
p=sorted(p)
print(p[K-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
from heapq import *
def input(): return sys.stdin.readline().strip()


def main():
    s = input()
    L = len(s)
    K = int(input())
    string = set([])
    for i in range(L):
        for j in range(i + 1, L + 1):
            string.add(s[i:j])

    string = list(string)
    heapify(string)
    for _ in range(K):
        ans = heappop(string)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
k=int(input())
d=[]
for p in range(len(s)):
    t=s[p:]
    d.append(t)
    for i in range(1,len(t)):
        d.append(t[:-i])
d=sorted(set(d))
print(d[k-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter

def input():
    return sys.stdin.readline()[:-1]

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

def ii():
    return int(input())

def main():
    s = input()
    N = len(s)
    K = ii()

    A = set()

    for i in range(N):
        for j in range(i, N):
            A.add(s[i:j+1])

    A = list(A)
    A.sort()

    print(A[K-1])



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
K = int(input())
val = ''
A = set()
for i,s in enumerate(S):
    val = ''
    for j in range(i,len(S)):
        val += (S[j])
        A.add(val)
A_sort = sorted(list(A))
print(A_sort[K-1])

Submission status: Time Limit Exceeded

Solution code:
#import sys
#import numpy as np
import math
#import itertools
#from fractions import Fraction
#import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
#import bisect
s=input()
k=int(input())
n=len(s)
a=[chr(i) for i in range(ord('a'),ord('z')+1)]
ans=set()
for i in a:
    if i not in s:
        continue
    if len(ans)>=5:
        break
    for c in range(n):
      if s[c]==i:
          l=c
          r=c
          while True:
            if  r>n:
                break
            ans.add(s[l:r+1])
            r+=1
ans=list(ans)
ans.sort()
print(ans[k-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
k = int(input())
l = "abcdefghijklmnopqrstuvwxyz"
d = {}

for i in range(26):
    cnt = s.count(l[i])
    pre = 0
    if cnt > 0:
        for j in range(cnt):
            pos = s.find(l[i], pre)
            pre = pos + 1
            for m in range(len(s) - pos):
                if s[pos:pos + m + 1] not in d:
                    d[s[pos:pos + m + 1]] = 1
    if len(d) >= k:
        dl = []
        for i in d.keys():
            dl.append(i)
        dl.sort()
        print(dl[k - 1])
        exit()

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 9)

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

chars = sorted(set(S))
checked = set()


def dfs(s):
    for c in chars:
        next_s = s + c
        if next_s not in checked and next_s in S:
            checked.add(next_s)
            if len(checked) == K:
                print(next_s)
                return
            elif len(next_s) <= len(S):
                dfs(next_s)


dfs('')

Submission status: Time Limit Exceeded

Solution code:
s = input()

d = ["z"*5010]*5

for i in range(len(s)):
    for j in range(i, len(s)):
        x = s[i:j+1]
        if x in d or x == '':
            continue
        k = 0
        while k < 5 and x > d[k]:
            k += 1
        # print(k, d[0:k], x, d[k:])
        if k < 5:
            d = d[0:k] + [x] + d[k:]
            d = d[0:5]
        # print(f"->{d}")
# l = list(d)
# l.sort()
# print(l)
# if l[0] == '':
    # l = l[1:]

k = int(input())
print(d[k-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()

d = ["z"*5010]*5

for i in range(len(s)):
    for j in range(i, len(s)):
        x = s[i:j+1]
        if x in d or x == '':
            continue
        k = 0
        while k < 5 and x > d[k]:
            k += 1
        # print(k, d[0:k], x, d[k:])
        d = d[0:k] + [x] + d[k:]
        d = d[0:5]
        # print(f"->{d}")
# l = list(d)
# l.sort()
# print(l)
# if l[0] == '':
    # l = l[1:]

k = int(input())
print(d[k-1])

Submission status: Time Limit Exceeded

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

d = set()

for i in range(len(s)):
    for j in range(i,len(s)):
        d.add(s[i:j + 1])
l = list(d)
l.sort()
# print(l)
if l[0] == '':
    l = l[1:]

print(l[k-1])

Submission status: Time Limit Exceeded

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

d = set()

for i in range(len(s)):
    for j in range(len(s)):
        d.add(s[i:j + 1])
l = list(d)
l.sort()
# print(l)
if l[0] == '':
    l = l[1:]

print(l[k-1])

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

# check
case = []
append = case.append
for i in range(1, K + 1):
    for c in combinations(list(s), i):
        if "".join(c) in s and "".join(c) not in case:
            append("".join(c))
case.sort(reverse=True)

print(case[-K])

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

# check
case = []
append = case.append
for i in range(1, len(s) + 1):
    for c in combinations(list(s), i):
        if "".join(c) in s and "".join(c) not in case:
            append("".join(c))
case.sort(reverse=True)

print(case[-K])

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
K = int(input())
cnt = {i: [] for i in range(N)}
for i in range(N):
    for j in range(N - i):
        s = S[j:j+i+1]
        idx = cnt[len(s)-1]
        if s not in idx:
            idx.append(s)
ttl = []
for k, v in cnt.items():
    ttl.extend(v)
ttl.sort()
print(ttl[K-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
K = int(input())
cnt = {i: [] for i in range(N)}
for i in range(N):
    for j in range(N - i):
        s = S[j:j+i+1]
        l = len(s)
        if s not in cnt[l-1]:
            cnt[l-1].append(s)
ttl = []
for k, v in cnt.items():
    ttl.extend(v)
ttl.sort()
print(ttl[K-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
k = int(input())
 
sl = [s]
for i in range(len(s)):
    for j in range(i+1, len(s)+1):
        ss = s[i:j]
        sl.append(ss)
sl = sorted(list(set(sl)))
print(sl[k-1])

Submission status: Time Limit Exceeded

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

sl = [s]
for i in range(len(s)):
    for j in range(i+1, len(s)+1):
        ss = s[i:j]
        sl.append(ss)
sl = sorted(list(set(sl)))
print(sl[k-1])

Submission status: Time Limit Exceeded

Solution code:
import re

s = input()
n = len(s)
k = int(input())
l = []
for i in range(26):
  m_list = [m.start() for m in re.finditer(chr(ord('a')+i), s)]
  #print(m_list)
  
  for start in m_list:
    c=1
    while start + c <= n:
      if not s[start:start+c] in l:
        l.append(s[start:start+c])
      c += 1
  if len(l) >= k:
    break
#print(l)
print(sorted(l)[k-1])

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = sr()
K = ir()
cand = [set() for _ in range(26)]
length = len(S)
for i in range(length):
    index = ord(S[i]) - ord('a')
    for j in range(i+1, length+1):
        cand[index].add(S[i:j])

x = 0
while len(cand[x]) < K:
    K -= len(cand[x])
    x += 1

y = sorted(list(cand[x]))
answer = y[K-1]
print(answer)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = sr()
K = ir()
cand = set()
length = len(S)
for i in range(length):
    for j in range(i+1, length+1):
        cand.add(S[i:j])

cand = sorted(list(cand))
answer = cand[K-1]
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(1000000)

def main():
    s = readline().rstrip().decode()
    K = int(readline())
    #sに含まれる文字を一文字ずつ抽出する.
    #ここで抽出した文字を組み合わせて辞書順に文字列を構成していく
    #K<=5だから高々26^5個の文字列を構成することになる
    l = sorted(list(set(s)))
    #cnd:=構成した文字列を辞書順に格納するリスト
    cnd = []
    #f(sub):=subから構成可能なK文字以下のSの部分文字列を返す関数
    def f(sub):
        if len(sub) > K: 
            return 
        #sのsubstringか確認 
        if sub in s:
            cnd.append(sub)
        #lから選んでsubに一文字追加する
        for i in l:
            nx = sub+i
            f(nx)
    f('')
    print(cnd[K])
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

x = sorted(set(s))

subs_list = []
for i in range(len(s)):
    for j in range(i+1, len(s)+1):
        subs_list.append(s[i:j])

ans = sorted(set(subs_list))
print(ans[k-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import combinations_with_replacement 
from heapq import heapify, heappop, heappush
def main():
    s = readline().rstrip().decode()
    K = int(readline())
    N = len(s)
    p = [int(i) for i in range(N)]
    hp = []
    heapify(hp)
    #K番目のsubsitringは長さがK以下のsubstrigの集合に必ず含まれる
    #K<=5であるから列挙される要素数は最大でも26^5(およそ10,000,000)
    for l, r in combinations_with_replacement(p, 2):
        sub = s[l:r+1]
        ln = len(sub)
        if ln <= K:  
            heappush(hp, sub)
    S = set()
    while len(hp)>0:
        v = heappop(hp)
        S.add(v)
        if len(S) == K:
            print(v)
            return 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import combinations_with_replacement 
from heapq import heapify, heappop, heappush
def main():
    s = readline().rstrip().decode()
    K = int(readline())
    N = len(s)
    p = [int(i) for i in range(N)]
    hp = []
    heapify(hp)
    #K番目のsubsitringは長さがK以下のsubstrigの集合に必ず含まれる
    #K<=5であるから列挙される要素数は最大でも26^5(およそ10,000,000)
    for l, r in combinations_with_replacement(p, 2):
        sub = s[l:r+1]
        ln = len(sub)
        if ln <= K:  
            heappush(hp, sub)
    S = set()
    while len(hp)>0:
        v = heappop(hp)
        S.add(v)
        if len(S) == K:
            print(v)
            return 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import combinations_with_replacement 
def main():
    s = readline().rstrip().decode()
    K = int(readline())
    N = len(s)
    p = [int(i) for i in range(N)]
    S = set()

    #K番目のsubsitringは長さがK以下のsubstrigの集合に必ず含まれる
    #K<=5であるから列挙される要素数は最大でも26^5(およそ10,000,000)
    #したがって長さK以下のsubstringを列挙してからソートすればよい
    for l, r in combinations_with_replacement(p, 2):
        sub = s[l:r+1]
        ln = len(sub)
        if ln <= K:  
            S.add(sub)
    #最大でも26^5要素のソート
    lis = list(S)
    lis.sort()
    print(lis[K-1])
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import combinations_with_replacement 
def main():
    s = readline().rstrip().decode()
    K = int(readline())
    N = len(s)
    p = [int(i) for i in range(N)]
    S = set()
    for l, r in combinations_with_replacement(p, 2):
        S.add(s[l:r+1])
    lis = list(S)
    lis.sort()
    print(lis[K-1])
if __name__ == '__main__':
    main()