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

Solution (Python):

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])

Solution (Python):
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)

Solution (Python):





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])

Solution (Python):
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)

Solution (Python):

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])
    
    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

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

Solution (Python):
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()

Solution (Python):
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

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

Solution (Python):
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()

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

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

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

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

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

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

Solution (Python):
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)

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

Solution (Python):


import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq





def solve(f):
    s = f.read_str()
    k = f.read_int()
    n = len(s)
    cand = set()

    for i in xrange(1,6):
        for j in xrange(n-i+1):
            cand.add(s[j:j+i])

    cand = sorted(list(cand))

    return cand[k-1]


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case 
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)

Solution (Python):
s = input()
K = int(input())
ans = []
for i in s:
    if not (i in ans):
        ans.append(i)
ans.sort()
ans = ans[:5]
j = len(s)
if j >= 2:
    for i in range(j-1):
        if (s[i] in ans) and not (s[i:i+2] in ans):
            ans.append(s[i:i+2])
ans.sort()
ans = ans[:5]
if j >= 3:
    for i in range(j-2):
        if (s[i:i+2] in ans) and not (s[i:i+3] in ans):
            ans.append(s[i:i+3])
ans.sort()
ans = ans[:5]
if j >= 4:
    for i in range(j-3):
        if (s[i:i+3] in ans) and not (s[i:i+4] in ans):
            ans.append(s[i:i+4])
ans.sort()
ans = ans[:5]
if j >= 5:
    for i in range(j-4):
        if (s[i:i+4] in ans) and not (s[i:i+5] in ans):
            ans.append(s[i:i+5])
ans.sort()
ans = ans[:5]
print(ans[K-1])

Solution (Python):
s=input()
N=len(s)
K=int(input())
L=[]
for i in range(1,K+1):
    for j in range(N-i+1):
        L.append(s[j:j+i])
L.sort()
M=len(L)
R=[L[0]]
k=0
for i in range(1,M):
    if L[i] !=R[k]:
        R.append(L[i])
        k+=1
print(R[K-1])

Solution (Python):
def solve(s, K):
    cs = sorted(set(s))
    ws = set([])
    for i in range(len(s)):
        c = s[i]
        if c in cs[:2]:
            ws.add(c)
            ws.add(s[i:i+2])
            ws.add(s[i:i+3])
            ws.add(s[i:i+4])
            ws.add(s[i:i+5])
    wss = sorted(ws)
    return wss[K - 1]

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

print(solve(s, K))

Solution (Python):


def main():
	s = input()
	k = int(input())
	substr = set()
	for start in range(len(s)):
		for end in range(start, start+5):
			if end <= len(s):
				substr.add(s[start:(end+1)])

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

if __name__ == '__main__':
	main()

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

from heapq import heappush, heappop

Q = set()
u = set()
N = len(s)
for i in range(N):
    for j in range(i+1, i+K+1):
        ss = s[i:j]
        if ss in u:
            continue
        u.add(ss)
        Q.add(ss)
        if len(Q) > K:
            Q.remove(max(Q))
l = sorted(Q)
print(l[K-1])

Solution (Python):
def f():
  s = input()
  k = int(input())
  ss = [s[i:] for i in range(len(s))]
  ss.sort()
  cnt = 0
  done = []
  for sb in ss:
    for i in range(len(sb)):
      res = (sb[0:i+1])
      if res not in done:
        done.append(res)
        cnt += 1
        if cnt == k:
          print(res)
          return
f()

Solution (Python):

def inpl(): return map(int, input().split())
S = input()
K = int(input())
ans = set()

for s in sorted(set(S)):
    if len(ans) >= K:
        break
    for i in range(len(S)):
        if S[i] == s:
            for k in range(K):
                ans.add(S[i:i+k+1])

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

Solution (Python):
S = input()
K = int(input())
n = len(S)

ans = []
st = set()

for i in range(n):
    for j in range(i + 1, min(n + 1, i + 1 + K)):
        sub = S[i:j]
        
        if sub in st:
            continue
        st.add(sub)
        ans.append(sub)
        ans.sort()
        
        if len(ans) > K:
            ans = ans[:K]

print(ans[-1])

Solution (Python):
s = list(input())
k = int(input())
n = len(s)

ss = []
for i in range(1, 6):
    for j in range(n):
        if j + i <= n:
            ss.append(''.join(s[j:j + i]))
ss = sorted(set(ss))
print(ss[k - 1])

Solution (Python):
def solve(s, k):
    can = set()

    for l in range(k):
        for i in range(len(s) - l):
            can.add(s[i:i + l + 1])
    can.discard('')
    can = sorted(can)
    return can[k-1]


s = input()
k = int(input())
print(solve(s, k))

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

s = set()
for i in range(1, 6):
    for j in range(len(S)):
        if i + j <= len(S):
            s.add(S[j:j + i])

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

print(ans)

Solution (Python):
S = input()
N = len(S)
K = int(input())

subs = set()
for i in range(N):
    for k in range(K):
        subs.add(S[i:i+1+k])
print(sorted(subs)[K-1])

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