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 in C (GCC 5.4.1) (judge verdict: Wrong Answer)

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

//for
#define REP(i,n) for(int i=0,i##_len=(int)(n);i<i##_len;i++)
#define REPS(i,n) for(int i=1,i##_len=(int)(n);i<=i##_len;i++)
#define RREP(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define RREPS(i,n) for(int i=(int)(n);i>0;i--)
#define FOR(i,s,n) for(int i=(int)(s),i##_len=(int)(n);i<i##_len;i++)

//printf
#define PRINTD(d) printf("%d\n",(d))
#define PRINTL(d) printf("%lld\n",(d))

//memset
#define m0(s) memset(s,0,sizeof(s))
#define ml(s) memset(s,63,sizeof(s))
#define fill(s,c) memset(s,c,sizeof(s))

#define INF 1e9

typedef long long ll;
int diff[4][2] = {
	{ 0, -1 },
	{ -1, 0 },
	{ 1, 0 },
	{ 0, 1 },
};

int Min(int a, int b) {
	return (a)<(b) ? (a) : (b);
}
int Max(int a, int b) {
	return (a) > (b) ? (a) : (b);
}
void Swap(int *a, int *b) {
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

int main() {
	char s[5001];
	scanf("%s", s);
	int k;
	scanf("%d", &k);
	int len = strlen(s);
	int cnt[26] = { 0 };
	char ans[5001]={'\0'};
	REP(i, len) {
		if (cnt[s[i] - 'a'] == 0)cnt[s[i] - 'a']++;
		cnt[s[i] - 'a'] += len-i-1;
	}
	/*
	PRINTD(len);
	REP(i, 26) {
		PRINTD(cnt[i]);
	}
	//*/

	int tmp = 0;
	REP(i, 26) {
		if (cnt[i] >= k) {
			ans[0] = 'a' + i;
			break;
		}
		else {
			k -= cnt[i];
		}

	}
	//printf("%c\n",ans[0]);
	//PRINTD(k);
	REPS(i,len) {
		int c = 0;
		if (k == 1)break;
		REP(j, 26) {
			cnt[j] = 0;
		}
		REP(j, len) {
			if (j + i >= len)break;
			char t[5001];
			strncpy(t, s + j, i);
			t[i] = '\0';
			//printf("%s %s %s\n", s, t, ans);
			if (strcmp(ans, t) == 0) {
				if (cnt[s[j + i] - 'a'] == 0)cnt[s[j + i] - 'a']++;
				cnt[s[j + i] - 'a']+=len-(j+i)-1;
				c++;
			}
		}
		if (c >= 2) {
			REP(j, 26) {
				//printf("%d %d\n", k, cnt[j]);
				if (cnt[j] >= k) {
					ans[i] = 'a' + j;
					break;
				}
				else {
					k -= cnt[i];
				}
			}
		}
		else {
			REP(j, len) {
				char t[5001];
				strncpy(t, s + j, i);
				t[i] = '\0';
				if (strcmp(ans, t) == 0) {
					strncpy(ans, s + j, k);
					ans[k] = '\0';
					break;
				}
			}
			break;
		}
		//puts(ans);
	}
	puts(ans);
}