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 Python (3.4.3) (judge verdict: Runtime Error)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
typedef vector<pi> vpi;
typedef double dl;
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define MOD (ll)1000000007
#define lb lower_bound
#define ub upper_bound
#define dst distance
#define INF 1000000000

string S,a;
int K,n;
map<string,bool> M;

int RA[5010], tempRA[5010]; // rank array and temporary rank array
int SA[5010], tempSA[5010]; // suffix array and temporary suffix array
int c[5010]; // for counting/radix sort
void countingSort(int k) { // O(n)
	int i, sum, maxi = max(300, n); // up to 255 ASCII chars or length of n
	memset(c, 0, sizeof c); // clear frequency table
	for (i = 0; i < n; i++){
		c[i + k < n ? RA[i + k] : 0]++; // count the frequency of each integer rank
	}
	for (i = sum = 0; i < maxi; i++) {
		int t = c[i]; 
		c[i] = sum; 
		sum += t;
	}
	for (i = 0; i < n; i++) tempSA[c[SA[i]+k < n ? RA[SA[i]+k] : 0]++] = SA[i]; // shuffle the suffix array if necessary
	for (i = 0; i < n; i++) SA[i] = tempSA[i];// update the suffix array SA
}

void constructSA() {
	int i, k, r;
	for (i = 0; i < n; i++) RA[i] = S[i]; // initial rankings
	for (i = 0; i < n; i++) SA[i] = i; // initial SA: {0, 1, 2, ..., n-1}
	for (k = 1; k < n; k <<= 1) { // repeat sorting process log n times
		countingSort(k); // actually radix sort: sort based on the second item
		countingSort(0); // then (stable) sort based on the first item
		tempRA[SA[0]] = r = 0; // re-ranking; start from rank r = 0
		for (i = 1; i < n; i++) // compare adjacent suffixes
		tempRA[SA[i]] =  (RA[SA[i]] == RA[SA[i-1]] && RA[SA[i]+k] == RA[SA[i-1]+k]) ? r : ++r; // if same pair => same rank r; otherwise, increase r
		for (i = 0; i < n; i++) RA[i] = tempRA[i]; // update the rank array RA
		if (RA[SA[n-1]] == n-1) break;
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	//freopen("input.txt","r",stdin);
	cin >> S >> K;
	n = S.size();
	constructSA();
	for (int i = 0; i < n; ++i){
		a = "";
		for (int j = SA[i]; j < n; ++j){
			a += S[j];
			if(M[a])continue;
			--K;
			M[a] = 1;
			if (K == 0){cout << a; return 0;}
		}
	}
}