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:
s = input()
k = int(input())
t = set()
for i in range(1, min(len(s),k)+1):
    for j in range(len(s)-i+1):
        t.add(s[j:j+i])
print(sorted(list(t))[k-1])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def sub_k(string, I, K):
    temp = []
    for i in range(len(string)):
        if i-1+I == len(string):
            break
        temp.append(string[i:i+I])
    return sorted(set(temp))[0:K]

def main(string, K):
    result = []
    for i in range(K):
        result += sub_k(string, i+1, K-i)
    print(sorted(set(result))[K-1])

string = input()
K = int(input())
main(string, K)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import *

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

s = input()
K = read()
L = len(s)
sbs = sorted(set(s[i:i+j] for i in range(L) for j in range(1, K+1) if i + j <= L))
print(sbs[K-1])

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import heapq
from itertools import islice

def substrings(s):
    heap = [s[i:] for i in range(len(s))]
    heapq.heapify(heap)
    popSet = set()

    while True:
        sub = heapq.heappop(heap)
        for i in range(1, len(sub) + 1):
            subi = sub[:i]
            if subi not in popSet:
                yield subi
                popSet.add(subi)


if __name__ == "__main__":
    s = input()
    k = int(input())
    print(list(islice(substrings(s), k))[k - 1])

Submission status: Accepted

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

sub = ["zzzzz" for i in range(k)]


def compare(sub_word):
    for i in range(len(sub)):
        #print("{} vs {}".format(sub[i], sub_word))
        if sub_word == sub[i]:
            return
        if sub_word < sub[i]:
            #print("----")
            for j in reversed(range(i, k - 1)):
                sub[j + 1] = sub[j]
            sub[i] = sub_word
            return


count = 0
for i in range(len(s)):
    for j in range(i, min(i + k, len(s))):
        compare(s[i:j + 1])
print(sub[-1])

Submission status: Accepted

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

indices = [0]*5
substrings = [sorted(list(set([s[i:i+l] for i in range(len(s)-l+1)])))[:5]+['zzzzz'] for l in range(1,6)]
for k in range(K):
    s = min(substrings[l][i] for l, i in enumerate(indices))
    # print([substrings[l][i] for l, i in enumerate(indices)])
    indices[len(s)-1] += 1
    if k == K-1:
        print(s)

Submission status: Accepted

Solution code:
s = input().rstrip()
k = int(input())
result = []
for i in range(len(s)):
  for j in range(i+1,i+k+1):
    if j <= len(s) and s[i:j] not in result:
      result.append(s[i:j])
result.sort()
print(result[k-1])

Submission status: Accepted

Solution code:
from sys import stdin, stdout

s = stdin.readline().strip()
k = int(stdin.readline())
challengers = []

for i in range(len(s)):
    cur = ''
    for j in range(i, min(i + 5, len(s))):
        cur += s[j]
        challengers.append(cur)

challengers.sort()

for i in range(len(challengers)):
    if not i or challengers[i - 1] != challengers[i]:
        k -= 1
    
    if not k:
        stdout.write(challengers[i])
        break

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
s = input()
K = int(input()) # 1 <= K <= 5

alpha = "abcdefghijklmnopqrstuvwxyz"
substring = ''
L = []
for i in alpha:
    indeces = [key for key, x in enumerate(s) if x == i]
    for j in indeces:
        for k in range(j,j+K):
            substring += s[j:k+1] + ','
    L += substring.split(',')[:-1] 
    if len(set(L)) >= K:
        break
    substring = ""
print(sorted(set(L))[K-1])

Submission status: Accepted

Solution code:
S = str(input())
K = int(input())
 
sub = set()
for i in range(1,min(K, len(S))+1):
    for j in range(len(S)-i+1):
        sub.add(S[j:j+i])
m=list(sub)
m.sort()
print(m[K-1])

Submission status: Accepted

Solution code:
s = input()
K = int(input())
# substrings = {}
substrings = []
for i in range(1,6):
    for j in range(0,len(s)):
        if s[j:j+i] not in substrings:
            # substrings[s[j:j+1]] = True
            substrings.append(s[j:j+i])
print(sorted(substrings)[K-1])

Submission status: Accepted

Solution code:
from copy import deepcopy
import itertools


def read():
    return int(input())


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


def readlist():
    return list(map(int, input().split()))


S = input()
K = read()

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

sub = sorted(list(substring))
print(sub[K-1])

Submission status: Accepted

Solution code:
s = input()
K = int(input())
l = len(s)
dic = {}
for i in range(l):
	if not s[i] in dic:
		dic[s[i]] = []
	for j in range(1,6):
		if i + j > (l):
			break
		else:
			dic[s[i]].append(s[i:i+j])

lst = list(dic.keys())
lst.sort()
for c in lst:
	dic[c] = sorted(set(dic[c]))
	l = len(dic[c])
	if K > l:
		K -= l
	else:
		print(dic[c][K-1])
		exit(0)

Submission status: Accepted

Solution code:
substrs = set()
string = input()
k = int(input())

for i in range(len(string)):
	for j in range(i, i+5):
		substrs.add(string[i:j+1])

substrs = sorted(list(substrs))
for i,s in enumerate(substrs):
	if i == k-1:
		print(s)
		break

Submission status: Accepted

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

Submission status: Accepted

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    s = S()
    k = I()
    l = len(s)
    t = set()
    for i in range(1,k+1):
        for j in range(l-i+1):
            t.add(s[j:j+i])

    return sorted(list(t))[k-1]



print(main())

Submission status: Accepted

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


def enumerate_substring(k, s):
    s_list = list(s)
    for i in range(1, k + 1):
        for j in range(len(s)):
            if j + i < len(s):
                s_list.append(s[j:j + i + 1])
    s_list = list(set(s_list))
    s_list.sort()
    return s_list


s_list = enumerate_substring(k, s)
print(s_list[k - 1])

Submission status: Accepted

Solution code:
import numpy as np

s = input()
K = int(input())
L = len(s)

candidate = []

for l in range(1, K + 1):
    for j in range(0, L - l + 1):
        candidate.append(s[j:j+l])

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

Submission status: Accepted

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

l = [s[i:min(i+k,len(s))] for i in range(len(s))]

l.sort()

count = k
ll = []
flag = True

for x in l:
    if not flag:
        break
    else:
        for i in range(len(x)):
            tmp = x[:i+1]
            if tmp not in ll:
                count -= 1
                if count == 0:
                    print(x[:i+1])
                    flag = False
                else:
                    ll.append(tmp)
            else:
                pass

Submission status: Accepted

Solution code:
import numpy as np
import heapq


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


def lii():
    return list(map(int, input().split(' ')))


def lvi(N):
    l = []
    for _ in range(N):
        l.append(ii())
    return l


def lv(N):
    l = []
    for _ in range(N):
        l.append(input())
    return l


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

    sub = []
    for i in range(len(s)):
        sub.append(s[i:])

    heapq.heapify(sub)
    minsub = heapq.heappop(sub)

    if len(minsub) >= K:
        print(minsub[:K])
        return

    cand = []
    for i in range(1, len(minsub)+1):
        cand.append(minsub[:i])
    K -= len(minsub)
    assert K > 0

    while K > 0:
        minsub_ = heapq.heappop(sub)
        for i in range(1, len(minsub_)+1):
            if not minsub_[:i] in cand:
                cand.append(minsub_[:i])
                K -= 1
                if K == 0:
                    print(minsub_[:i])
                    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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


L = []

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

M = sorted(list(set(L)))

print(M[K-1])

Submission status: Accepted

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

subs = list()

for k in range(K):
	for i in range(len(s) - k):
		subs.append("".join(s[i : i + k + 1]))

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

arr = [S[:j] for j in range(1, min(K, L)+1)]
for i in range(1, L):
  arr2 = [S[i:i+j] for j in range(1, min(K, L-i)+1)]
  arr = sorted(set(arr + arr2))
  arr = arr[:min(K, len(arr))]

print(arr[K-1])

Submission status: Accepted

Solution code:
def main():
    s = input()
    k = int(input())
    
    l = []
    for i in range(0, len(s)):
        for j in range(0, min(len(s)-i, k)):
            sub = s[i:i+j+1]

            if sub not in l:
                l.append(sub)

    l.sort()
    
    print(l[k-1])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

sub = list(sub)
sub.sort()
print(sub[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,i+K+1):
    if j<=len(s):
      ans.add(s[i:j])
ans=list(sorted(ans))
print(ans[K-1])

Submission status: Accepted

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

list = {}

for i in range(len(s)):
    for j in range(i + 1,i + 6):
        if(s[i:j] not in list):
            list[s[i:j]] = 0

keys = sorted(list.keys())

print(keys[k - 1])

Submission status: Accepted

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

S=set([])

for i in range(l):
    for j in range(i,min(l,i+5)):
#        print(s[i:j+1])
        S.add(s[i:j+1])
print(list(sorted(S))[K-1])

Submission status: Accepted

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


def brute_force(s, K):
    substring_set = set()

    # brute-force
    for start in range(len(s)):
        # 5まででよかった...
        for length in range(1, 6):
            substring = s[start:start+length]
            substring_set.add(substring)

    substring_list = sorted(list(substring_set))
    print(substring_list[K - 1])

brute_force(s, K)

Submission status: Accepted

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

s1 = []
for i in range(len(s)):
    for j in range(i + 1, i + k + 1):
        s1.append(s[i:j])
s1.sort()
substring = []
i = 0
while len(substring) <= 4:
    try:
        if s1[i] not in substring:
            substring.append(s1[i])
        i = i + 1
    except:
        break
print(substring[k - 1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
from heapq import heapify, heappop, heappush
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque


# d = defaultdict(lambda: 0)
# setrecursionlimit(10**7)
# inf = float("inf")


##### stdin ####
def LM(t, r): return list(map(t, r))
def R(): return stdin.readline()
def RS(): return R().split()
def I(): return int(R())
def F(): return float(R())
def LI(): return LM(int,RS())
def LF(): return LM(float,RS())
def ONE_SL(): return list(input())
def ONE_IL(): return LM(int, ONE_SL())
def ALL_IL(): return LM(int,stdin)

##### tools #####
def ap(f): return f.append
def pll(li): print('\n'.join(LM(str,li)))
def pljoin(li, s): print(s.join(li))


##### Library ####


##### main #####
def main():

    input_S = input()
    K = I() - 1

    # input_S = 'abcaa'
    # K = 2

    len_S = len(input_S)
    unique_S = len(set(input_S))
    
    lis = set()
    min_s = 'z'

    if len_S == 1:
        print(input_S)

    else:
        for i in range(5):
            for j in range(len_S):
                if input_S[j] > min_s:
                    continue

                s = input_S[j:j+i+1]
                lis.add(s)

        print(sorted(lis)[K])

   
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def suffix_array(s):
    def k(a):
        return s[a:]
    
    x = list(range(len(s)))
    x.sort(key=k)
    return x

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

    a = suffix_array(s)
    v = set()

    for i in a:
        for j in range(i + 1, len(s) + 1):
            v.add(s[i:j])
            if len(v) == k:
                print(s[i:j])
                return

main()

Submission status: Accepted

Solution code:
def main():
    string = input().strip()
    k = int(input())
    s = set()
    for char in sorted(set(string)):
        for i in range(len(string)):
            if string[i] == char:
                s.add(string[i])
                s.add(string[i:i+1])
                s.add(string[i:i+2])
                s.add(string[i:i+3])
                s.add(string[i:i+4])
                s.add(string[i:i+5])
        if len(s) >= k:
            print(sorted(s)[k - 1])
            return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

cand = [str(s[0])]
for i in range(len(s)):
    if len(cand) == n and s[i] > cand[-1]:
        continue
    for j in range(i, len(s)):
        tmp = s[i:j+1]
        #print(i,j, tmp, cand)
        if len(cand) == n and tmp > cand[-1]:
            break
        if tmp in cand:
            continue
        cand += [tmp]
        #cand = sorted(cand)
        cand.sort()
        if len(cand) > n:
            cand.pop()


print(cand.pop())

Submission status: Accepted

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

tmp=[s[0]]

for i in range(len(s)):
    for j in range(i,len(s)):
        cur_s=s[i:j+1]
        if cur_s not in tmp:
            if len(tmp)==k:
                if tmp[-1]<cur_s:
                    break
            idx=[ele<cur_s for ele in tmp].count(True)
            if len(tmp)==k:
                tmp=tmp[:idx]+[cur_s]+tmp[idx:-1]
            else:
                tmp=tmp[:idx]+[cur_s]+tmp[idx:]
print(tmp[-1])

Submission status: Accepted

Solution code:
#!python
# -*- coding: utf-8 -*-


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

substrings = []


for n in range(1, K + 1):
    for i in range(0, len(S) + 1 - n):
        substrings.append(S[i:i + n])
substrings.sort()

_ = ""
n = 0
for s in substrings:
    if s == _:
        continue
    n += 1
    if n == K:
        print(s)
        break
    else:
        _ = s

Submission status: Accepted

Solution code:
s=input()
k=int(input())
s1=list(s)
s2=[s[i:i+2] for i in range(len(s)-1)]
s3=[s[i:i+3] for i in range(len(s)-2)]
s4=[s[i:i+4] for i in range(len(s)-3)]
s5=[s[i:i+5] for i in range(len(s)-4)]
sall=sorted(list(set(s1+s2+s3+s4+s5)))
print(sall[k-1])

Submission status: Accepted

Solution code:
string = input()
k = int(input())
if len(string) > k:
    substrs = set()
    ls = sorted(list(string))
    for i in range(k):
        for s, c in enumerate(string):
            if c == ls[i]:
                for l in range(1, k+1):
                    substrs.add(string[s:s+l])
    substrs = list(substrs)
    substrs.sort()
    print(substrs[k-1])

else:
    substrs = set()
    for leng in range(1, len(string) + 1):
        for s in range(0, len(string) - leng + 1):
            substrs.add(string[s:s+leng])
    substrs = list(substrs)
    substrs.sort()
    print(substrs[k-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:

import re
import itertools

s=input()
k=int(input())
alphs=sorted(list(set(s)))[0:k]
candsubstrings0=map(lambda x:[s[m.start():(m.end()+k)] for m in re.finditer(x,s)],alphs)
candsubstrings1= sorted(list(set([e for  inner_list in candsubstrings0 for e in inner_list])))[0:k]

def get_all_substrings(input_string):
  length = len(input_string)
  return [input_string[i:j + 1] for i in range(length) for j in range(i,length)]

candsubstrings2 = list(map(get_all_substrings,candsubstrings1))
candsubstrings3= sorted(list(set([e for  inner_list in candsubstrings2 for e in inner_list])))[0:k]
    
print(candsubstrings3[k-1])

Submission status: Accepted

Solution code:
def f(s, t):
    n = len(t)
    r = set()
    i = 0
    while True:
        i = s.find(t, i)
        if i == -1:
            break
        cand = s[i: (i+n)+1]
        if len(cand) == n + 1:
            r.add(cand)
        i += 1
    return sorted(r)

class Counter:
    def __init__(self, K):
        self._cnt = 0
        self.cur_c = ''
        self.K = K

    @property
    def cnt(self):
        return self._cnt

    @cnt.setter
    def cnt(self, i):
        self._cnt = i
        if self._cnt == K:
            raise ValueError

if __name__ == '__main__':
    s = input()
    K = int(input())

    l = sorted(set(s))



    ctr = Counter(K)
    try:
        for c1 in l:
            ctr.cur_c = c1
            ctr.cnt += 1
            for c2 in f(s, c1):
                ctr.cur_c = c2
                ctr.cnt += 1
                for c3 in f(s, c2):
                    ctr.cur_c = c3
                    ctr.cnt += 1
                    for c4 in f(s, c3):
                        ctr.cur_c = c4
                        ctr.cnt += 1
                        for c5 in f(s, c4):
                            ctr.cur_c = c5
                            ctr.cnt += 1
    except ValueError:
        print(ctr.cur_c)

Submission status: Accepted

Solution code:
# n = int(input())
# a, b = map(int, input().split())
# c = list(map(int, input().split()))
# d = [list(map(int, input().split())) for _ in range(n)]

alphabet = "abcdefghijklnmopqrstuvwxyz"
s = input()
k = int(input())
s_set = set(s)
ans = set()

if k == 1:
    s_set_list = list(s_set)
    s_set_list.sort()
    print(s_set_list[0])

elif k >= 2:
    for c in alphabet:
        for i in range(len(s)):
            if c == s[i]:
                ans.add(c)
                if i == len(s):
                    break
                for j in range(i+1, i+k+2):
                    if j > len(s):
                        break
                    ans.add(s[i:j])
        if len(ans) > k:
            break


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

Submission status: Accepted

Solution code:
def get_indices_of_letter(string, letter):
    indices_of_min_letter = []
    for i, c in enumerate(string):
        if c == letter:
            indices_of_min_letter.append(i)

    return indices_of_min_letter


def solve(s, k):
    letters_in_s = sorted(set(s))

    min_letter = letters_in_s[0]
    if k == 1:
        return min_letter

    indices_of_min_letter = get_indices_of_letter(s, min_letter)

    substrings_seen = set([min_letter])

    for i in indices_of_min_letter:
        for j in xrange(i + 1, i + 6):
            substrings_seen.add(s[i:j])

    if len(substrings_seen) >= k:
        return sorted(substrings_seen)[k - 1]

    second_smallest_letter = letters_in_s[1]

    substrings_seen.add(second_smallest_letter)
    if len(substrings_seen) == k:
        return second_smallest_letter

    indices_of_second_letter = get_indices_of_letter(s, second_smallest_letter)

    for i in indices_of_second_letter:
        for j in xrange(i + 1, i + 6):
            substrings_seen.add(s[i:j])

    if len(substrings_seen) >= k:
        return sorted(substrings_seen)[k - 1]

    return letters_in_s[2]


s = raw_input()
k = int(raw_input())

print solve(s, k)

Submission status: Accepted

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


def solve(tc):
    s = stdin.readline().strip()
    k = int(stdin.readline().strip())

    n = len(s)
    li = [list() for i in range(26)]
    for i in range(n):
        li[ord(s[i])-ord('a')].append(i)
    
    p = 0
    while len(li[p])==0:
        p += 1
    
    ans = set()
    while len(ans)<k and p<26:
        for i in range(k):
            for j in range(len(li[p])):
                b = li[p][j]
                e = min(n, li[p][j] + i + 1)
                ans.add(s[b:e])
        p += 1
    lans = list(ans)
    lans.sort()
    print(lans[k-1])
    # print(lans[k-1])
    pass


LOCAL_TEST = not __debug__
if LOCAL_TEST:
    infile = __file__.split('.')[0] + "-test.in"
    stdin = open(infile, 'r')

tcs = (int(stdin.readline().strip()) if LOCAL_TEST else 1)
tc = 1
while tc <= tcs:
    solve(tc)
    tc += 1

Submission status: Accepted

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

x = ['a', 'b', 'ab', 'ba', 'aba']
a=[]
m = min(s)
for i in range(len(s)):
    a.append(s[i])
    a.append(s[i:min(len(s),i+2)])
    a.append(s[i:min(len(s),i+3)])
    a.append(s[i:min(len(s),i+4)])
    a.append(s[i:min(len(s),i+5)])
a=list(set(a))
a.sort()
if a[0]=='':
    a=a[1:]
print(a[k-1])

Submission status: Accepted

Solution code:
ALPHA = "abcdefghijklmnopqrstuvwxyz"


def solve(s, K):
    for a in ALPHA:
        if a not in s:
            continue
        sss = [i for i, p in enumerate(s) if p == a]
        l = 1
        res = a
        if K == 1:
            return res
        while True:
            q = [(s[pos:pos + 1 + l], pos) for pos in sss]
            q = [(a, j) for a, j in q if len(a) == l + 1]
            if len(q) == 0:
                K -= 1
                break
            mins = sorted([a for a, j in q])[0]
            min_indexs = [i for a, i in q if a == mins]
            sss = min_indexs
            res = mins
            l += 1
            K -= 1

            if K == 1:
                return res


def main():
    s = input()
    K = int(input())
    res = solve(s, K)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
import string

s=raw_input()
K=int(raw_input())
A=defaultdict(list)

T=[]
for i in range(len(s)):
    for j in range(1,6):
        T += [s[i:i+j]]
ans=sorted( list(set(T)) )[K-1]

print ans

Submission status: Accepted

Solution code:
import collections
import string
import heapq
s=input()
n = len(s)
k = int(input())

m = ['zzzzzzz','zzzzzzz','zzzzzzz','zzzzzzz','zzzzzzz']
subs = []
for i in range(n):
    for j in range(i+1, min(i+6, n+1)):
        sub = s[i:j]
        if sub not in subs:
            subs.append(sub)
            subs.sort()
            subs = subs[:5]
print(subs[k-1])

Submission status: Accepted

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

Ts = [s[i:] for i in range(len(s))]
Ts.sort()

subs = set()
for T in Ts:
    for i in range(len(T)):
        subs.add(T[:i + 1])
        if len(subs) == K:
            print(T[:i + 1])
            exit()

Submission status: Accepted

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

ls = len(s)
m = ['zzzzz']*5
for i in range(ls):
    for j in range(i, i+5):
        ss = s[i:j+1]
        if ss in m:
            continue
        for k in range(len(m)):
            if m[k] > ss:
                m.insert(k, ss)
                break
        m = m[0:5]
print(m[kk-1])

Submission status: Accepted

Solution code:
s = str(input())
K = int(input())
le = len(s)
ansset = set()

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

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

Submission status: Accepted

Solution code:
s = str(input())
k = int(input())
lis = []
for u in range(6):
	for i in range(len(s)):
		try:
			if s[i:i+u] == "":
				continue
			if s[i:i+u] not in lis:
				lis.append(s[i:i+u])
		except:
			continue
lis.sort()
print(lis[k-1])

Submission status: Accepted

Solution code:
str=input()
k=int(input())
tmp=[]
for i in range (len(str)):
    for j in range (6):
        if(i+j<=len(str)):
            tmp.append(str[i:i+j])
tmp= list(set(tmp))
tmp.sort()
print(tmp[k])

Submission status: Accepted

Solution code:
import sys
from collections import Counter
import random
import numpy as np
import time


alph="abcdefghijklmnopqrstuvwxyz"
s=input()
l=len(s)
k=int(input())
cou=[s.count(alph[i]) for i in range(len(alph))]
keep=[]
for c in range(len(cou)):
    if(cou[c]==0):continue
    ind=[i for i, x in enumerate(s) if x == alph[c]]
    for i in ind:
        for j in range(1,6):
            if(i+j<=l):
                t=s[i:i+j]
                if(t not in keep):keep.append(s[i:i+j])
    if(len(keep)>=5):break

keep.sort()
print(keep[k-1])

Submission status: Accepted

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

Sset = set(zip(*(S[i:] for i in range(1))))

for i in range(2,6):
    Sset |= set(zip(*(S[i:] for i in range(i))))
    
print("".join(sorted(list(Sset))[K-1]))

Submission status: Accepted

Solution code:
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

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

l = len(s)

alph = [0]*26
for i in range(26):
    alph[i] = []

for i in range(l):
    alph[ord(s[i]) - 97].append(i)

ans = "hoge"
for i in range(26):
    hoge = set()
    for ind in alph[i]:

        for k in range(5):
            tmp = s[ind:ind+k+1]
            if tmp != '':
                hoge.add(tmp)
    flag = False
    for e in sorted(list(hoge)):
        K -= 1
        if K == 0:
            ans = e
            flag = True
            break
    if flag:
        break
print(ans)

Submission status: Accepted

Solution code:
s = input()
x = int(input())
a = set()
l = len(s)
q = ''
for i in range(l):
    q = s[i]
    a.add(q)
q = ''
for i in range(l):
    if (i <= 1):
        q += s[i]
    else:
        q = q[1:]
        q += s[i]
    a.add(q)
q = ''
for i in range(l):
    if (i <= 2):
        q += s[i]
    else:
        q = q[1:]
        q += s[i]
    a.add(q)
q = ''
for i in range(l):
    if (i <= 3):
        q += s[i]
    else:
        q = q[1:]
        q += s[i]
    a.add(q)
q = ''
for i in range(l):
    if (i <= 4):
        q += s[i]
    else:
        q = q[1:]
        q += s[i]
    a.add(q)
a = list(a)
a = sorted(a)
print(a[x-1])