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 _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
# https://atcoder.jp/contests/abc107/tasks/arc101_a
# C - Candles
"""
need to consider the case that ticket is not enough to lower everything
"""

s = gw()
K = gi()
ss = set()

for i in range(len(s)):
    for k in range(1, K + 1):
        if i + k > len(s):
            break
        ss.add(s[i:i + k])

sl = []
for st in ss:
    sl.append(st)
sl.sort()
print(sl[K - 1])

Submission status: Accepted

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

for l in range(1, 6):
    for i in range(len(S)-l+1):
        s.add(S[i:i+l])

l = list(s)
l.sort()

print(l[K-1])

Submission status: Accepted

Solution code:
#参考：https://atcoder.jp/contests/arc097/submissions/9281415

s = input()
K = int(input())
#辞書順でk番目に小さいｓのsubstringを出力する

#set型で集合演算
seta = set()

for i in range(len(s)):
    for j in range(1,6):
        seta.add(s[i:i+j])
        #print(s[i:i+j])
lista = sorted(list(seta))

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

Submission status: Accepted

Solution code:
s,k=open(0)
n=len(s)-1
k=int(k)
t=set()
for a in s[:n]:
  t.add(a)
for a,b in zip(s,s[1:n]):
  t.add(a+b)
for a,b,c in zip(s,s[1:],s[2:n]):
  t.add(a+b+c)
for a,b,c,d in zip(s,s[1:],s[2:],s[3:n]):
  t.add(a+b+c+d)
for a,b,c,d,e in zip(s,s[1:],s[2:],s[3:],s[4:n]):
  t.add(a+b+c+d+e)
print(sorted(t)[k-1])

Submission status: Accepted

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

Submission status: Accepted

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

print(sorted(d)[k - 1])

Submission status: Accepted

Solution code:
s = list(input())
n = len(s)
k = int(input())
if k > n:
    K = n
else:
    K = k
anset = set()
for lenght in range(1,K+1):
    for i in range(n - lenght + 1):
        anset.add("".join(s[i:i+lenght]))
anset = list(anset)
anset.sort()
print(anset[k-1])

Submission status: Accepted

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

D = {c:[] for c in "abcdefghijklmnopqrstuvwxyz"}
for i,c in enumerate(s):
    D[c].append(i)

ans = []
for I in D.values():
    t = []
    for l in range(1,K+1):
        for i in I:
            if i+l < len(s)+1:
                t.append(s[i:i+l])
    ans += sorted(list(set(t)))
    if len(ans)>=K:
        break
print(ans[K-1])

Submission status: Accepted

Solution code:
s=input()
N=int(input())
#s="atcoderandatcodeera"
#N=5
D={}

if(len(s)<=1):
    for i in range(1,len(s)+1):
        for j in range(len(s)):
            if D.get(s[j:j+i],None) == None:
                D[s[j:j+i]]=1
    D=sorted(D)
    print(D[N-1])
    exit()
else:
    t=sorted(s)
    i=0
    k=0
    for c in t:
        if(c!=k):
            i=0
            if(len(D)>=5):
                break
        k=c
        while(i<len(s)):
            if(s[i]==c):
                break
            i+=1
        j=i+1
        while(j<=min(len(s),i+5)):
            D[s[i:j]]=1
            j+=1
        i+=1
    
    D=sorted(D)
    print(D[N-1])
    exit()

Submission status: Accepted

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

ans = []
for i in range(len(s)):
    for j in range(i, i+K+1):
        # print(i, j)
        ans.append(s[i:j])
ans = list(set(ans))
ans.sort()
# print(ans)
print(ans[K])

Submission status: Accepted

Solution code:
import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    al = [chr(i) for i in range(97, 97 + 26)]
    s = str(readline().rstrip().decode('utf-8'))
    k = int(readline())
    ans = collections.defaultdict(int)
    for alv in al:
        for i, v in enumerate(s):
            if alv == v:
                for j in range(i + 1, i + k + 1):
                    ans[s[i:j]]
        if len(ans) > k:
            break
    print(sorted(ans.items())[k-1][0])


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

S=[]
for n in range(len(s)):
    for i in range(1,len(s)-n+1):
        if len(S)<5:
            S.append(s[n:i+n])
            S = list(set(S))
            S.sort()
        elif S[4]>s[n:i+n]:
            S.append(s[n:i+n])
            S = list(set(S))
            S.sort()
        else:
            break
S=list(set(S))
S.sort()

print(S[K-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
s=list(input())
K=int(input())

dp=[[[[[0]*27 for i in range(27)] for i in range(27)] for i in range(27)] for i in range(27)]

for i in range(len(s)):
    for j in range(5):
        temp=list(map(lambda x:ord(x)-96,s[i:i+j+1]))+[0,0,0,0,0]
        #print(temp)
        dp[temp[0]][temp[1]][temp[2]][temp[3]][temp[4]]=1
        
    

for a in range(len(dp)):
    for b in range(len(dp[a])):
        for c in range(len(dp[a][b])):
            for d in range(len(dp[a][b][c])):
                for e in range(len(dp[a][b][c][d])):
                    if dp[a][b][c][d][e]==1:
                        if K==1:
                            print(chr(a+96 if a!=0 else 0)+(chr(b+96) if b!=0 else "")+(chr(c+96) if c!=0 else "")+(chr(d+96) if d!=0 else "")+(chr(e+96) if e!=0 else ""))
                            exit()
                        else:
                            K-=1

Submission status: Accepted

Solution code:
s = list(input())
n = len(s)
k = int(input())
if k > n:
    K = n
else:
    K = k
anset = set()
for lenght in range(1,K+1):
    for i in range(n - lenght + 1):
        anset.add("".join(s[i:i+lenght]))
anset = list(anset)
anset.sort()
print(anset[k-1])

Submission status: Accepted

Solution code:
s = input()
n = len(s)
k = int(input())
ls = set()

for i in range(5):
    for j in range(n-i):
        ls.add(s[j:j+i+1])

ls = sorted(list(ls))
print(ls[k-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    s=input()
    m=int(input())
    tank=[]
    n=len(s)
    alpha=[chr(i) for i in range(97, 97+26)]
    for i in range(26):
        for j in range(n):
            if alpha[i]==s[j]:
                for k in range(min(n-j,m)):
                    if s[j:j+k+1] not in tank:
                        tank.append(s[j:j+k+1])
        if len(tank)>=m:
            break
    tank.sort()
    print(tank[m-1])
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

M = 'zzzzzz'

def dfs(s, K, N, r, a, ss):
    m = a[r]
    for i in range(N):
        if s[i] == m:
            t = ''
            for j in range(i, min(i+K, N)):
                t += s[j]
                if t in ss:
                    continue
                if t >= ss[-1]:
                    break
                ss.pop()
                ss.append(t)
                list.sort(ss)
    if ss[-1] == M:
        return dfs(s, K, N, r+1, a, ss)
    return ss[-1]


def solve(s: str, K: int):
    ss = [M] * K
    a = sorted(list(set(s)))
    N = len(s)
    return dfs(s, K, N, 0, a, ss)


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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

if K==1:
  print(sorted(S)[0])
  exit()

ans=[]
if K<=len(S):
  s0=sorted(S[:-K+1])[0]
  for j,e in enumerate(S[:-K+1]):
    if e==s0:
      for i in range(1,K+1):
        a="".join(S[j:j+i])
        if a not in ans:
          ans.append(a)
      
sl=S[-K+1:]
for i in range(K-1):
  for j in range(i+1,K):
    a="".join(sl[i:j])
    if a not in ans:
      ans.append(a)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()
k=int(input())
uselist=[]
count=0
checker=0
alphabet="abcdefghijklmnopqrstuvwxyz"
while count<=5 and checker<=26:
    for i in alphabet:
        for j in range(len(s)):
            if s[j]==i:
                uselist.append((j,i))
                count+=1
        checker+=1
anslist=[]
for some in uselist:
    count=0
    for j in range(some[0],len(s)):
        anslist.append(s[some[0]:j+1:])
        count+=1
        if count>5:
            break
            
anslist=sorted(set(anslist))

print(anslist[k-1])

Submission status: Accepted

Solution code:
s = input()
K = int(input())
len_s = len(s)
already = set()
for start in range(len_s):
    for end in range(start+1, min(start + 6, len_s+1)):
        already.add(s[start:end])
candidate = sorted(list(already))
print(candidate[K-1])

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines
#rstrip().decode('utf-8')

#import numpy as np
#import operator
#import bisect
#from heapq import heapify,heappop,heappush
#from math import gcd
#from fractions import gcd
#from collections import deque
#from collections import defaultdict
#from collections import Counter
#from itertools import accumulate
#from itertools import groupby
#from itertools import permutations
#from itertools import combinations
#scipy.sparse import csr_matrix
#scipy.sparse.csgraph import floyd_warshall
#from scipy.sparse.csgraph import csgraph_from_dense
#from scipy.sparse.csgraph import dijkstra
#sys.setrecursionlimit(10**7)

#map(int,input().split())

def main():
    s=input().rstrip().decode('utf-8')
    n=len(s)
    se0=set()
    t=sorted(s)
    for i in range(5):
        try:
            se0.add(t[i])
        except:
            pass
    se=se0
    for i in range(2,6):
        for j in range(n-i+1):
            try:
                if s[j] in se0:
                    se.add(s[j:j+i])
            except:
                pass
    li=sorted(list(se))
    k=int(input())
    print(li[k-1])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

a = list()

ch = 0
cn = 0
for i in range(1,27):
    b = set()
    for j in range(len(s)):
        if(ord(s[j])==(i+96)):
            for m in range(j,j+k):
                #print(s[j:m+1])
                b.add(s[j:m+1])
                cn += 1
                

    a.append(list(b))
    

for i in range(26):
    a[i].sort()
    
cn = 0
for i in range(26):
    if(len(a[i])+cn>=k):
        for j in range(len(a[i])):
            cn += 1
            if(cn == k):
                print(a[i][j])
    else:
        cn += len(a[i])
        
#print(a)

Submission status: Accepted

Solution code:
s = input()
K = int(input())
n1 = list(s)
n2 = [s[i:i+2] for i in range(len(s)-1)]
n3 = [s[i:i+3] for i in range(len(s)-2)]
n4 = [s[i:i+4] for i in range(len(s)-3)]
n5 = [s[i:i+5] for i in range(len(s)-4)]
cat = list(set(n1 + n2 + n3 + n4 + n5))
cat.sort()
#print(cat)
print(cat[K-1])

Submission status: Accepted

Solution code:
def main():
    s = str(input())
    s += '0'
    k = int(input())
    result = set()
    for i in range(1, 6):
        for j in range(len(s)-i):
            result.add(s[j:j+i])
    out = list(result)
    out.sort()
    print(out[k-1])
if __name__ =='__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
k = int(input())
subs = []
lens = len(s)
prev = ''
cnt = 0

for i in range(lens):
  for j in range(1,k+1):
    subs.append(s[i:i+j])
subs.sort()
for m in subs:
  if prev != m:
    cnt += 1
  if cnt == k:
    print(m)
    exit()
  prev = m

Submission status: Accepted

Solution code:
from collections import defaultdict
s=input()
k=int(input())
n=len(s)
l=defaultdict(int)
for i in range(n):
  for j in range(min(5,n),0,-1):
    l[s[i:i+j]]+=1
print(sorted(l)[k-1])

Submission status: Accepted

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

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

# print(ans)
print(sorted(list(ans))[k-1])

Submission status: Accepted

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

t=[]
for i in range(0,min(len(s),5)):
  for j in range(n-i):
    t.append(s[j:j+i+1])
t=list(set(t))
t.sort()
print(t[K-1])

Submission status: Accepted

Solution code:
def slove():
    import sys
    import collections
    input = sys.stdin.readline
    t = str(input().rstrip('\n'))
    s = collections.Counter(t).most_common()
    k = int(input().rstrip('\n'))
    s.sort()
    d = {}
    for key, v in s:
        for i in range(len(t)):
            if t[i] == key:
                for j in range(i + 1, i + 6):
                    d[t[i:j]] = 1
        if len(d) >= k:
            print(sorted(d.items())[k-1][0])
            exit()


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

abc = collections.defaultdict(list)

for i in range(len(s)):
  for j in range(1,K+1):
    if i+j <= len(s):
        abc[s[i:i+j]].append(i)


abc = list(abc.items())
abc.sort(key=lambda x: x[0])

print(abc[K-1][0])

Submission status: Accepted

Solution code:
s = input()
K = int(input())
l = [chr(i) for i in range(97, 97+26)]
selected = []
for i in l:
  if i in s:
    index = []
    for ind, j in enumerate(s):
      if i == j:
        index.append(ind)
    for j in index:
      for k in range(K):
        if j+k == len(s):
          break
        selected.append(s[j:j+k+1])
    selected=list(set(selected))
    selected.sort()
    if len(selected)>=K:
      print(selected[K-1])
      break

Submission status: Accepted

Solution code:
s = input()
K = int(input())
l = [chr(i) for i in range(97, 97+26)]
selected = []
for i in l:
  if i in s:
    index = []
    for ind, j in enumerate(s):
      if i == j:
        index.append(ind)
    for j in index:
      temp = []
      for k in range(5):
        if j+k == len(s) or len(temp)>=K:
          break
        selected.append(s[j:j+k+1])
    selected=list(set(selected))
    selected.sort()
    if len(selected)>=K:
      print(selected[K-1])
      break

Submission status: Accepted

Solution code:
s=input()
k=int(input())
a=set()
for i in range(5):
	for j in range(len(s)-i):
		x=s[j:j+i+1]
		a.add(x)
a=list(a)
a.sort()
print(a[k-1])

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    s = str(input().rstrip('\n'))
    k = int(input().rstrip('\n'))
    d = []
    for i in range(len(s)):
        for j in range(i + 1, min(i + 1 + k, len(s) + 1)):
            d.append(s[i:j])
    import collections
    d = sorted(collections.Counter(d).most_common())
    print(d[k-1][0])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

substr = []
N = len(s)

for i in range(1,K+1):
    for j in range(N-i+1):
        substr.append(s[j:j+i])

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

Submission status: Accepted

Solution code:
s=input()
k=int(input())
sng=len(s)
t=sorted(s)
mini=[]

koho=[t[0]]
itimo=[t[0]]
now=0
while len(koho)<=4 and now!=len(t):
    if koho[-1]!=t[now]:
        koho.append(t[now])
        itimo.append(t[now])
    now+=1

#print(koho)
swit=[]
for i in range(sng):
    if s[i] in itimo:
        swit.append(5)
    if len(swit)>0:
        for sw in swit:
            #print(i-(5-sw), i+1, s[i-(5-sw):i+1])
            sin=s[i-(5-sw):i]
            if sin!="":
                koho.append(s[i-(5-sw):i+1])
    swit2=[]
    for sw in range(len(swit)):
        if swit[sw]>0:
            swit2.append(swit[sw]-1)
    swit=swit2

koho=sorted(koho)
mae=koho[0]
now=1
koho2=[koho[0]]
while now<len(koho):
    if koho[now]!=mae:
        mae=koho[now]
        koho2.append(koho[now])
    now+=1
#print(koho)
print(koho2[k-1])

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res

def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

s = input()
n = len(s)
k = I()
q = []
for i in range(n):
    heappush(q,(s[i],i))
d = defaultdict(lambda : 1)
while k:
    si,i = heappop(q)
    if d[si]:
        k -= 1
        d[si] = 0
    if not k:
        print(si)
        quit()
    if i < n-1:
        si += s[i+1]
        i += 1
        heappush(q,(si,i))

Submission status: Accepted

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

substrings = []

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

Submission status: Accepted

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

substrings = []

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
uni = set()
for i in range(1, K+1):
    for j in range(len(s)):
        if len(s) - (j+i) < 0: break
        uni.add(s[j:j + i])

print(sorted(list(uni))[K-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
K = int(input())
l = ['zzzzzzzzzzzzzzz','zzzzzzzzzzzzzzz','zzzzzzzzzzzzz','zzzzzzzzzzzz','zzzzzzzzz']
N = len(s)

for i in range(K+1):
    for j in range(N-i):
        tmp = s[j:j+i+1]
        if tmp not in l:
            l.append(tmp)
        l.sort()
        l = l[0:5]


l.sort()
#print(l)


ans = l[K-1]
print(ans)

Submission status: Accepted

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

P = []
len_s = len(S)
for k in range(1, K+1):
    for i in range(len_s-k+1):
        P.append(S[i:i+k])
P.sort()

ans = P[0]
cnt = 1
i = 1
while cnt < K:
    if ans != P[i]:
        ans = P[i]
        cnt += 1
    i += 1

print(ans)

Submission status: Accepted

Solution code:
# 2019/08/06

s=input()
k=int(input())
ls=len(s)

ans=set(s)
for i in range(2,k+1):
    l=1
    res=s[0:i]
    while l<ls-i+1:
        ans.add(res)
        r=l+i-1
        res=res[1:]+s[r]
        l+=1
    ans.add(res)
ans=sorted(list(ans))
print(ans[k-1])

Submission status: Accepted

Solution code:
import math
import fractions
import copy
import numpy as np
def j(q):
    if q==1: print("Yes")
    elif q == 0:print("No")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
"""
#decomment when needed
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
"""
a = [i for i in input()]
n = ip()
minplace = [0]
minlet = a[0]
nextlet = 'z'
nextminplace = []
for i in range(1,len(a)):
    if a[i] < minlet:
        nextlet = minlet
        nextminplace = copy.copy(minplace)
        minlet = a[i]
        minplace.clear()
        minplace.append(i)
    elif a[i] == minlet:
        minplace.append(i)
    elif a[i] < nextlet:
        nextminplace.clear()
        nextlet = a[i]
        nextminplace.append(i)
    elif a[i] == nextlet:
        nextminplace.append(i)
wl = []
#print(minplace,nextminplace)
for start in minplace:
    word = [a[start]]
    wl.append(''.join(word))
    for i in range(start+1,start+5):
        if i >= len(a): break
        word.append(a[i])
        wl.append(''.join(word))
wl = list(set(wl))
#print(wl)
for start in nextminplace:
    word = [a[start]]
    wl.append(''.join(word))
    for i in range(start+1,start+5):
        if i >= len(a): break
        word.append(a[i])
        wl.append(''.join(word))
#print(wl)
wl = list(set(wl))
wl.sort()
print(wl[n-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
K = int(input())
def same_head_num(a,b):
    n = 0
    N = min(len(a),len(b))
    while n < N:
        if a[n] == b[n]:
            n += 1
        else:
            break
    return n

strings = ['']
N = len(s)
for i in range(N):
    strings.append(s[i:])
strings.sort()
for i in range(1,N+1):
    head_n = same_head_num(strings[i-1],strings[i])
    tail_n = len(strings[i]) - head_n
    if tail_n < K:
        K -= tail_n
        continue
    else:
        print(strings[i][:head_n+K])
        break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

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

Submission status: Accepted

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

substrings = set()
for len_sub in range(K):
    for start in range(len(s)-len_sub):
        substring = s[start:start+len_sub+1]
        substrings.add(substring)

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

Submission status: Accepted

Solution code:
# ARC097C - K-th Substring
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())

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:
from collections import deque
s=input()
length=len(s)

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

left=0
right=0
#ss=s[left:right]
#print(ss)

ss=deque()
for left in range(length+1):
    for right in range(left+1,length+1):
      if len(s[left:right]) <= K:
        ss.append(s[left:right])

myset=set(ss)
for i in range(K):
    tmp=min(myset)
    myset.remove(tmp)
print(tmp)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
k = int(input())
p = []
append = p.append
for i in range(1,min(n+1,k+1)):
    for j in range(n):
        append(s[j:j+i])

p.sort()
ans = 0
q = ""
for i in p:
    if i>q:
        ans += 1
        q = i
    if ans == k:
        print(q)
        exit()

Submission status: Accepted

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

for i in range(len(s)):
    for j in range(K):
        if i+j < len(s):
            t.add(s[i:i+j+1])

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

Submission status: Accepted

Solution code:
S = input()
K = int(input())
a = {s:[] for s in 'abcdefghijklmnopqrstuvwxyz'}
for i in range(len(S)):
    a[S[i]].append(S[i:i+K])
tmp = []
for c in 'abcdefghijklmnopqrstuvwxyz':
    for s in sorted(a[c]):
        for i in range(1,len(s)):
            if s[:i] in tmp:
                K += 1
        if len(s) >= K:
            print(s[:K])
            exit()
        else:
            K -= len(s)
            for i in range(1,len(s)+1):
                tmp.append(s[:i])

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

l = []
n = len(s)

for i in range(n):
    for j in range(6):
        if i + j <= n:
            l.append(s[i:i + j])

l = set(l)
l = sorted(l)

print(l[K])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
hoge=input()
k=int(input())
ans=[]
for i in range(len(hoge)):
  for j in range(5):
    ans.append(hoge[i:1+i+j])
ans=list(set(ans))
ans.sort()
print(ans[k-1])

Submission status: Accepted

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

for i in range(len(S)):
    for j in range(1,min(len(S) - i + 1, K + 1)):
        x.add(S[i:i + j])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
k = int(input())
l = sorted(list(set(list(s))))
x = set(list())
for i in l:
    x |= set([s[ii:iii+1] for ii in range(len(s)) for iii in range(ii,min(len(s),ii+5)) if s[ii] == i])
    if len(x) > 5:
        break
x = sorted(x)

print(x[k-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()
k=int(input())
c=[]
for i in range(len(s)):
    for j in range(min(len(s)-i,k)):
        t=s[i:i+j+1]
        if t not in c:
            c.append(t)
print(sorted(c)[k-1])

Submission status: Accepted

Solution code:
s=input()
k=int(input())
cnt=[]
for i in range(len(s)):
    for j in range(min(len(s)-i,k)):
        t=s[i:i+j+1]
        if t not in cnt:
            cnt.append(t)
print(sorted(cnt)[k-1])

Submission status: Accepted

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    s = input()
    k = f()[0]

    a = set()

    slen = len(s)
    for i in range(slen):
        for j in range(1,k+2):
            if i+j <= slen:
                a.add(s[i: i+j])
    
    a = sorted(a)
    print(a[k-1])


solve()

Submission status: Accepted

Solution code:
s = input()
K = int(input())
strs = [[] for _ in range(26)]
cnts = [0]*26

for ind,i in enumerate(s):
    if(sum(cnts[:ord(i)-97])>=5):
        continue
    for j in range(1,K+1):
        st = s[ind:ind+j]
        if(not st in strs[ord(i)-97]):
            strs[ord(i)-97].append(st)
            cnts[ord(i)-97]+=1


cnt = 0
num = 0
while(cnt<K):
    cnt += len(strs[num])
    num += 1
num -= 1
cnt -= len(strs[num])
strs[num].sort()
print(strs[num][K-cnt-1])

Submission status: Accepted

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

a = []
for i in range(len(S)):
	for l in range(1, len(S) - i + 1):
		s = S[i:i + l]
		#print(s, a)
		if s in a:
			continue
		if len(a) < 5:
			a += [s]
			a = sorted(a)
		else:
			if a[-1] < s:
				break
			else:
				a[-1] = s
				a = sorted(a)
print(a[K - 1])

Submission status: Accepted

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

Submission status: Accepted

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

ans = []
for i in range(len(S)):
    for j in range(1,K+1):
        if i+j <= len(S):
            if S[i:i+j] not in ans:
                ans.append(S[i:i+j])

ans.sort()

print ans[K-1]

Submission status: Accepted

Solution code:
s = input()
k = int(input())
N = len(s)
subs = set()
for i in range(1, N):
    l = s[:i]
    r = s[i:]
    if len(l) > k:
        l = l[-k:]
    if len(r) > k:
        r = r[:k]
    while not l == '':
        subs.add(l)
        l = l[1:]
    while not r == '':
        subs.add(r)
        r = r[:-1]
subs.add(s)
subs = sorted(subs)
print(subs[k-1])