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: Accepted

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    s = input()
    K = int(input())
    dict = set()
    for i in range(len(s)):
        for j in range(K):
            if i+j+1 <= len(s):
                dict.add(s[i:i+j+1])
    di = list(dict)
    di.sort()
    print(di[K-1])
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
s=input()
k=int(input())
n=len(s)
ans=deque()
for i in range(n):
    for j in range(k):
        ans.append(s[i:i+j+1])
print(sorted(list(set(ans)))[k-1])

Submission status: Accepted

Solution code:
S = input()
K = int(input())
anslist = []
for i in range(0, len(S)):
	now = ''
	for j in range(i, len(S)):
		now = now + str(S[j])
		flag = True
		if len(anslist) != K and now not in anslist:
			flag = False
			anslist.append(now)
		elif len(anslist) == K and now < anslist[-1] and now not in anslist:
			flag = False
			anslist[-1] = now
		elif now in anslist:
			flag = False
		anslist.sort()
		if flag:
			break
print(anslist[-1])

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(len(s)):
    for j in range(k):
        if i+j+1 <= len(s):
            dic.add(s[i:i+j+1])
di = list(dic)
di.sort()
print(di[k-1])

Submission status: Accepted

Solution code:
# ABC097C - K-th Substring (ARC097C)
def substring_getter(a: str, x: int) -> set:
    length = len(a)
    ret = set()
    for i in range(length - x + 1):
        ret.add(a[i : i + x])
    return ret


def main():
    s = input()
    k = int(input())
    ans = set()
    for i in range(1, k + 1):
        ans = ans | substring_getter(s, i)
    print(sorted(ans)[k - 1])


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

letter_set = sorted(set(S))

ans_list = []

for letter in letter_set:
    letters = letter
    while letters in S:
        ans_list.append(letters)
        if len(ans_list) == K:
            print(ans_list[K - 1])
            sys.exit()
        for i in range(len(letter_set)):
            if letters + letter_set[i] in S:
                letters = letters + letter_set[i]
                break
        else:
            break
print(ans_list[K - 1])

Submission status: Accepted

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

l = "abcdefghijklmnopqrstuvwxyz"
li = []
res = []
for i in l:
    for index,j in enumerate(s):
        if j==i:
            li.append(s[index:])
    else:
        if len(li) > 5:
            break

#print(li)

li.sort()

for i in range(1,6):
    for j in li:
        try:
            #print(s[:i])
            res.append(j[:i])
            #print(res)
        except:
            pass
else:
    res = list(set(res))
    res.sort()
        
    print(res[k-1])
    # print(res)

Submission status: Accepted

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

ls = []

for i in range(min(5,len(s))):
    for j in range(0,len(s)-i):
        ls.append(s[j:j+(i+1)])

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

Submission status: Accepted

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


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


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

Submission status: Accepted

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


ans_set = set()
for i in range(len(s)):
    c = s[i]
    ans_set.add(c)
    for j in range(i + 1, len(s)):
        c += s[j]
        if len(c) > k:
            break
        ans_set.add(c)


ans_lst = list(ans_set)
ans_lst.sort()
ans = ans_lst[k-1]


print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

s = input()
k = int(input())
ans = []
for i in range(len(s)):
    for j in range(1, 6):
        ans.append(s[i:min(i+j, len(s))])
ans = list(set(ans))
ans.sort()
print(ans[k-1])

Submission status: Accepted

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

for i in range(K):
    for j in range(len(s) - i):
        str_set.add(s[j:j+i+1])

sorted_str = sorted(list(str_set))
print(sorted_str[K-1])

Submission status: Accepted

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

for i in range(len(s)):
    for j in range(1,6):
        st.add(s[i:i+j])

sl=sorted(list(st))

print(sl[K-1])

Submission status: Accepted

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

ans=[]
a=ans.append
for i in range(len(s)):
	a(s[i])
for i in range(len(s)-1):
	a(s[i:i+2])
for i in range(len(s)-2):
	a(s[i:i+3])
for i in range(len(s)-3):
	a(s[i:i+4])
for i in range(len(s)-4):
	a(s[i:i+5])
ans=sorted(list(set(ans)))
print(ans[k-1])

Submission status: Accepted

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

substr = []
for i in range(len(s)):
    for j in range(i+1, i+K+1):
        if len(s) >= K:
            substr.append(s[i:j])
        else:
            if j == len(s)+1:
                break
            substr.append(s[i:j])

substr = list(set(substr))
substr.sort()

print(substr[K-1])

Submission status: Accepted

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

substr = []
for n in range(1, k+1):
    for i in range(len(s)-n+1):
        substr.append(s[i:i+n])

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

Submission status: Accepted

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

print(ll[k-1])

Submission status: Accepted

Solution code:

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

lis = []

alps = "abcdefghijklmnopqrstuvwxyz"

for alp in alps:

    for i in range(len(s)):

        #print (s[i],alp)
        if s[i] == alp:

            #print ("ループ北")

            for j in range(6):

                j += 1
                #print (i+ j)
                #print (s[i:i+j] in lis,s[i:i+j],lis)
                if i + j <= len(s) and (s[i:i+j] not in lis):
                    #print ("来た！！")
                    lis.append(s[i:i+j])

    if len(lis) >= K:
        break


lis.sort()

#print (lis)
print (lis[K - 1])

Submission status: Accepted

Solution code:
import os
import sys

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

S = sys.stdin.readline().rstrip()
K = int(sys.stdin.readline())

s = set()
for k in range(1, K + 1):
    for i in range(len(S)):
        s.add(S[i:i + k])

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

Submission status: Accepted

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

alpha=[]

for i in range(1,k+1):
    for j in range(len(s)-i+1):
        if not (s[j:j+i] in alpha):
            alpha.append(s[j:j+i])

alpha.sort()

print(alpha[k-1])

Submission status: Accepted

Solution code:
s=input()
K=int(input())
List=[]
Length=len(s)
for i in range(Length):
    List.append(s[i])
    n=1
    while n<=4:
        if i+n>=Length:
            break
        else:
            List.append(s[i:i+n+1])
            n+=1
List_unique=list(set(List))    
List_unique.sort()
print(List_unique[K-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
l = len(S)
d = set()
K = int(input())
for i in range(5):
    for j in range(l-i):
        d.add(S[j:j+i+1])
D = sorted(list(d))
print(D[K-1])

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  s=S()
  k=I()

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

  l=list(set(l))
  l.sort()

  return l[k-1]

print(main())

Submission status: Accepted

Solution code:
from collections import Counter

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

chars = sorted(list(Counter(S).keys()))

## 辞書列で一番若い文字を見つけ、そこから多くてK文字の文字列をリストに入れる
idx_pre_char = 0
solver = []
while len(solver) < K:
  for i in range(len(S)):
    if S[i] == chars[idx_pre_char]:
      if i <= len(S) - K:
        for j in range(K):
          solver.append(S[i:i+j+1])
      else:
        for j in range(len(S)-i):
          solver.append(S[i:i+j+1])
  solver = list(set(solver))
  idx_pre_char += 1


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

Submission status: Accepted

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

l = []

for i in range(K):
    for j in range(len(s)-i):
        l.append(s[j:j+i+1])
l.sort()

ind = 0
cnt = 0
while cnt <K-1:
    if l[ind+1] != l[ind]:
        cnt += 1
    ind += 1
print(l[ind])

Submission status: Accepted

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

a = list(s)
a = [ord(i) for i in a]
b = sorted(set(a))
c = []
INF = 97+26
for m in b:
    a1 = [i for i in range(len(s)) if a[i] == m]

    for i in a1:
        j = 0
        for j in range(k):
            if i + j >= len(s):
                break
            if s[i:i+j+1] not in c:
                c.append(s[i:i+j+1])
                c.sort()
            if len(c) > 5:
                del c[-1]

    if len(c) >= k:
        break

print(c[k-1])

Submission status: Accepted

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

a = list(s)
a = [ord(i) for i in a]
b = sorted(set(a))
c = []
INF = 97+26
for m in b:
    a1 = [i for i in range(len(s)) if a[i] == m]

    for i in a1:
        j = 0
        for j in range(k):
            if s[i:i+j+1] not in c:
                c.append(s[i:i+j+1])
                c.sort()
            if len(c) > 5:
                del c[-1]
            j += 1
            if i + j >= len(s):
                break
    if len(c) >= k:
        break

print(c[k-1])

Submission status: Accepted

Solution code:
#!/usr/bin/env python
S = input()
K = int(input())
s_len = len(S)
s_set = set()
for i in range(1, s_len+1):
    j = 0
    if i > K:
        break
    else:
        while j+i <= s_len:
            s_set.add(S[j:j+i])
            j += 1
    
s_sort = sorted(list(s_set))
print(s_sort[K-1])

Submission status: Accepted

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

for i in range(len(s)):
    a.append(s[i])
    j = 2
    while i + j <= len(s) and j <= 5:
        a.append(s[i:i+j])
        j += 1
print(sorted(set(a))[K-1])

Submission status: Accepted

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

kn = len(s) * k
k_list = set(s)

for i in range(2, k+1):
    for j in range(len(s)):
        k_list.add(s[j:j+i])

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

Submission status: Accepted

Solution code:
s=input()
K=int(input())
As=sorted(set(s))
ans=[]
for A in As:
    ans.append(A)
    if len(ans)>=K:break
    for j in range(2,K+1):
        ind=0
        for i in s:
            if i==A:
                Judge=s[ind:ind+j]
                if len(Judge)==j and (Judge not in ans):
                    ans.append(Judge)
            ind+=1
print(sorted(ans)[K-1])

Submission status: Accepted

Solution code:
#substring列挙したらTLE(部分点)
s = input()
k = int(input())
s_set_sort = sorted(set(s))

li = []
count = 0
while count < k:
    s_head = s_set_sort.pop(0)
    for i in range(len(s)):
        if s[i] == s_head:
            for j in range(k):
                if s[i:i+j+1] not in li:
                    count += 1
                    li.append(s[i:i+j+1])
li.sort()
print(li[k-1])

Submission status: Accepted

Solution code:


S = input()
N = int(input())
l = len(S)

sub = {}
for i in range(1,6):
    for j in range(0,l+1-i):
        temp = S[j:j+i]
        if temp not in sub:
            sub [temp]  = True

sub = list(sub.keys())
sub.sort()
print(sub[N-1])

Submission status: Accepted

Solution code:
s=input()
n=int(input())
l=["z" for i in range(5)]
for i in s:
  if l[4]>i and not i in l:
    l[4]=i
    l=sorted(l)
l2=[]
s+=" "
for i in range(len(s)-1):
  if s[i] in l:
    w=""
    for j in range(5):
      if s[i+j]==" ":
        break
      else:
        w+=s[i+j]
        l2.append(w)
l2=sorted(list(set(l2)))
print(l2[n-1])

Submission status: Accepted

Solution code:
S = input()
# 5000
K = int(input())
# K < 5

s = []
for i in range(len(S)):
    for k in range(1,K+1):
        s.append(S[i:i+k])

print(sorted(set(s))[K-1])

Submission status: Accepted

Solution code:
s,k=input(),int(input())
L=[]
for i in range(0,k):
    L+=[s[j:j+i+1] for j in range(len(s)-i+1)]
L=sorted(list(set(L)))
L.remove('')

print(L[k-1])

Submission status: Accepted

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

substrings = set()

for i in range(5):
    for j in range(len(s)):
        substring = s[j:j+i+1]
        substrings.add(substring)

answer = sorted(substrings)
print(answer[k-1])

Submission status: Accepted

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


sub_string = []
for i in range(len(s)):
    for j in range(0, len(s) + 1 - i):
        if j > K:
            break
        sub_string.append(s[i:i+j])

#重複の排除
sub_string = set(sub_string)
#辞書順にソートする
sub_string = sorted(sub_string)
#空白を削除
sub_string.remove('')
print(sub_string[K - 1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#ABC097-C
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return list(input())
import collections
S=input()
k=I()
ans=[]
for i in range(len(S)):
    for j in range(1,k+1):
        ans.append(S[i:i+j])
ans=list(set(ans))
ans.sort()
print(ans[k-1])

Submission status: Accepted

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

se = set()
for L in range(1,K+1):
  for i in range(len(s)):
    se.add(s[i:i+L])

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

Submission status: Accepted

Solution code:
s=input()
K=int(input())
L=[]
for i in range(len(s)):
    for j in range(K+1):
        if i+j==len(s)+1:
            break
        L.append(''.join(s[i:i+j]))
LL=list(set(L))
LL.sort()
print(LL[K])

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import string
import itertools


def solve(s: str, K: int):

    part_set = set()
    for c in string.ascii_lowercase:
        for i in range(len(s)):
            if c == s[i]:
                part_set |= set(itertools.accumulate(s[i:min(i+K,len(s))]))
        if len(part_set) >= K:
            print(sorted(part_set)[K-1])
            return
                


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    K = int(next(tokens))  # type: int
    solve(s, K)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  s=S()
  k=I()

  l=[]

  for i in range(len(s)):
    l.append(s[i])
    l.append(s[i:i+2])
    l.append(s[i:i+3])
    l.append(s[i:i+4])
    l.append(s[i:i+5])

  l=list(set(l))
  l.sort()

  return l[k-1]

print(main())

Submission status: Accepted

Solution code:
# a〜z
l=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

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

l2=[]

a=''
for x in l:
  if x in s:
    a=x
    for i in range(len(s)):
      if s[i]==a:
        for j in range(6):
          l2.append(''.join(s[i:i+j]))

l2=list(set(l2))
l2.remove('')
l2.sort()
print(l2[K-1])

Submission status: Accepted

Solution code:
s = raw_input()
chrindex = []
for i in range(0, 26):
	chrindex.append([])
for i in range(0, len(s)):
	chrindex[ord(s[i])-97].append(i)
K = int(raw_input())
sublist = []
for i in range(0, K):
	sublist.append('')
for i in range(0, 26):
	for j in range(0, len(chrindex[i])):
		word = ''
		for k in range(chrindex[i][j], len(s)):
			word = word + s[k]
			if sublist[-1] < word and sublist[-1] != '': break
			if word not in sublist:
				for p in range(0, K):
					if word < sublist[p]:
						for q in range(0, K-p-1):
							sublist[-1-q] = sublist[-2-q]
						sublist[p] = word
						break
					if sublist[p] == '':
						sublist[p] = word
						break
	if '' not in sublist: break
print sublist[-1]

Submission status: Accepted

Solution code:
s = input()
K = int(input())
ans = [s[l:l + r + 1] for l in range(len(s)) for r in range(K)]
print(sorted(set(ans))[K - 1])

Submission status: Accepted

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

subs = []
for j in range(K):
    for i in range(len(s)-j):
        new = s[i:i+j+1]
        if not(new in subs):subs += [new]
        
subs.sort()
print(subs[K-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()
k=int(input())
n=len(s)
count=[]
for i in range(k):
    count.append('zzzzz')
for i in range(n):
    for j in range(k):
        for l in range(k):
            if s[i:i+j+1]==count[l]:
                break
            elif s[i:i+j+1]<count[l]:
                for m in range(k-1,l,-1):
                    count[m]=count[m-1]
                count[l]=s[i:i+j+1]
                break
print(count[k-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#全探索-->len(s) = 50程度までしか通らない
s = list(input())
K = int(input())

lst = []
for i in range(len(s)):
    for j in range(i+1, min(len(s)+1, i+1+K)):
#        print ('i, j=', i, j)
        lst += [s[i:j]]

lst.sort()
#print (lst)
k = 0
flag = 0
ans = str()
check = True
while check:
    if ans != lst[flag]:
        #print ('k', k)
        k += 1
    ans = lst[flag]
    #print ('ans=', ans)
    flag += 1
    if k == K:
        ans = ''.join(ans)
        print(ans)
        check = not check

Submission status: Accepted

Solution code:
S = input()
K = int(input())
store = []
for i in range(len(S)):
  N = i+1
  while N <= i + 5:
    store.append(S[i:N])
    N += 1
store_set = set(store)
store_list = list(store_set)

List = [ x for x in store_list  if len(x) <= K ]
List.sort()
print(List[K-1])

Submission status: Accepted

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


def solve_(S: str, K: int, used):
    #print(S, K, used)
    mn = 'z'
    tmp = set()
    for i, c in enumerate(S):
        if c < mn and not c in used:
            tmp = set()
            for j in range(5):
                tmp.add(S[i:i + j + 1])
            mn = c
        elif c == mn:
            for j in range(5):
                tmp.add(S[i:i + j + 1])
    if len(tmp) < K:
        used.append(mn)
        return solve_(S, K - len(tmp), used)
    else:
        tmp = list(tmp)
        tmp.sort()
        return tmp[K - 1]

def solve(S, K):
    ret = solve_(S, K, [])
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    K = int(next(tokens))  # type: int
    solve(s, K)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
k = int(input())
n = len(s)
a = []
for i in range(k):
    if n - i > 0:
        for j in range(n - i):
            a.append(s[j:j + i + 1])
a = list(set(a))
a.sort()
print(a[k-1])

Submission status: Accepted

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

l_s = len(s)
U = set()
for c in [chr(i) for i in range(97, 97+26)]:
    for i in range(l_s):
        if c == s[i]:
            for j in range(k):
                U.add(s[i:i + j + 1])
    if len(U) > 5:
        break
print(sorted(list(U))[k - 1])

Submission status: Accepted

Solution code:
###############################################################################

from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    if y > x // 2:
        y = x - y

    ret = 1

    for i in range(0, y):
        j = x - i
        i = i + 1

        ret = ret * j
        ret = ret // i

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


###############################################################################


def main():
    s = input()
    k = intin()

    nlist = [(ord(c), i) for i, c in enumerate(s)]
    nlist.sort()

    prev_oc = None
    slist = []

    for oc, i in nlist:
        kcount = len(slist)
        if prev_oc is not None and oc != prev_oc and kcount >= k:
            break
        for j in range(min(k, len(s) - i)):
            slist.append(s[i:(i+j+1)])
        slist = list(set(slist))
        prev_oc = oc

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

N = len(s)

alphabet = "abcdefghijklmnopqrstuvwxyz"

def search(k, start_dict):
    for chara in alphabet:
        if chara in start_dict:
            k -= 1
            if k == 0:
                return True, chara
            start_list = start_dict[chara]
            new_start_dict = {}
            for start in start_list:
                index = start + 1
                if index == N:
                    continue
                if s[start+1] in new_start_dict:
                    new_start_dict[s[index]].append(index)
                else:
                    new_start_dict[s[index]] = [index]
            has_found, ret = search(k, new_start_dict)
            if has_found:
                return True, chara + ret
            else:
                k = ret
    return False, k

start_dict = {}
for idx, chara in enumerate(s):
    if chara in start_dict:
        start_dict[chara].append(idx)
    else:
        start_dict[chara] = [idx]
_, ans = search(K, start_dict)
print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
s = input()
K = int(input())

l = {s}
for n in range(1, K+1):
    for i in range(0, len(s)-n+1):
        l.add(s[i:i+n])
l = list(l)

l.sort()

print(l[K-1])

Submission status: Accepted

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

N = len(s)

li = []
for i in range(N):
    for j in range(5):
        if (i+j >= N):
            break
        li.append(s[i:i+j+1])
    li = sorted(list(set(li)))[:6]
    

print(li[K-1])

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#A
def A():
    return

#B
def B():
    return

#C
def C():
    s = stdin.readline().rstrip()
    K = II() - 1
    ans = []
    for i in range(len(s)):
        for k in range(K+1):
            b = s[i:i + k + 1]
            ans.append(b)
    ans = list(set(ans))
    ans.sort()
    print(ans[K])
    return

#D
def D():
    n, m = LI()
    p = LI_()
    xy = LIR_(m)
    size = [1 for _ in range(n)]
    height = [1 for _ in range(n)]
    par = [i for i in range(n)]
    def find(x):
        if par[x] == x:
            return x
        else:
            par[x] = find(par[x])
            return par[x]
    def union(x, y):
        s1 = find(x)
        s2 = find(y)
        if s1 != s2:
            if height[s1] < height[s2]:
                par[s1] = s2
                size[s2] += size[s1]
                size[s1] = 0
            else:
                par[s2] = s1
                size[s1] += size[s2]
                size[s2] = 0
                if height[s1] == height[s2]:
                    height[s1] += 1

    for x,y in xy:
        union(x, y)
    ans = 0
    for i in range(n):
        if find(p[i]) == find(i):
            ans += 1
    
    print(ans)
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

Solution code:
S=input()
K=int(input())
alpha=set()
for i in S:
    alpha.add(i)
for i in range(len(S)-1):
    alpha.add(S[i:i+2])
for i in range(len(S)-2):
    alpha.add(S[i:i+3])
for i in range(len(S)-3):
    alpha.add(S[i:i+4])
for i in range(len(S)-4):
    alpha.add(S[i:i+5])
alpha=list(alpha)
alpha.sort()
print(alpha[K-1])

Submission status: Accepted

Solution code:
s=input()
k=int(input())
a={s}
for i in range(k):
  for j in range(len(s)-i):a.add(s[j:j+i+1])
print(sorted(list(a))[k-1])

Submission status: Accepted

Solution code:
def main():
    data = list(input())
    num = int(input())

    set_dic = set()
    for i in range(26):
        point = chr(97 + i)
        for j in range(len(data)):
            if data[j] == point:
                for k in range(min(len(data) - j, num)):
                    set_dic.add(''.join(data[j:j+k + 1]))
        if len(set_dic) >= num:
            break
    set_dic = list(set_dic)
    set_dic.sort()
    print(set_dic[num - 1])



if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
K = int(input())
sub = []
break_flag = False
for i in range(min(len(s), 5)):
  for j in range(len(s) - i):
    sub.append(s[j: j + i + 1])
# print(sub)
print(sorted(set(sub))[K - 1])