One day in the IT lesson Anna and Maria learned about the lexicographic order.
String x is lexicographically less than string y, if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that xi < yi, and for any j (1 ≤ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages.
The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework.
The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≤ k ≤ 105).
Print the string Anna and Maria need — the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes).
aa 2
a
abc 5
bc
abab 7
b
In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define LONG long long
const LONG INF=0x3f3f3f3f;
const LONG MOD=1e9+7;
const double PI=acos(-1.0);
#define clr0(x) memset(x,0,sizeof x)
#define clrI(x) memset(x,-1,sizeof(x))
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
string s;
int t1[101000],t2[101000],cc[101000],x[101000],sa[101000],Rank[101000],height[101000];
LONG len;
bool cmp(int *y,int a,int b,int k)
{
int a1=y[a];
int b1=y[b];
int a2=a+k>=len ? -1:y[a+k];
int b2=b+k>=len ? -1:y[b+k];
return a1==b1 && a2==b2;
}
int make_sa()
{
int *x=t1,*y=t2;
int m=26;
for(int i=0; i<m; i++) cc[i]=0;
for(int i=0; i<len; i++) ++cc[x[i]=s[i]-'a'];
for(int i=1; i<m; i++) cc[i]+=cc[i-1];
for(int i=len-1; i>=0; i--) sa[--cc[x[i]]]=i;
for(int k=1; k<=len; k<<=1)
{
int p=0;
for(int i=len-k; i<len; i++) y[p++]=i;
for(int i=0; i<len; i++)
if( sa[i]>=k ) y[p++]=sa[i]-k;
for(int i=0; i<m; i++) cc[i]=0;
for(int i=0; i<len; i++) ++cc[x[y[i]]];
for(int i=1; i<m; i++) cc[i]+=cc[i-1];
for(int i=len-1; i>=0; i--) sa[--cc[x[y[i]]]]=y[i];
swap(x,y);
m=1; x[sa[0]]=0;
for(int i=1; i<len; i++)
x[sa[i]]=cmp(y,sa[i],sa[i-1],k) ? m-1:m++;
if( m>=len ) break;
}
}
void make_height()
{
for(int i=0; i<len; i++) Rank[sa[i]]=i;
height[0]=0;
int k=0;
for(int i=0; i<len; i++)
{
if(!Rank[i]) continue;
int j=sa[Rank[i]-1];
if(k) k--;
while(s[i+k]==s[j+k]) k++;
height[Rank[i]]=k;
}
}
int main()
{
// freopen("C:\\Users\\ZhangYuyang\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\ZhangYuyang\\Desktop\\out.txt","w",stdout);
LONG k ;
len = s.length();
if( k > len * (len + 1)/2)
{
cout<<"No such line."<<endl;
return 0;
}
make_sa();
make_height();
vector<LONG > vec[30];
for(int i = 0 ; i< 30 ; ++ i)vec[i].clear();
for(int i = 0 ;i < len ;++ i)
vec[s[i]-'a'+ 1].push_back((LONG )i);
LONG sum[30] ;
for(int i =1; i<= 26 ;++ i)
{
sum[i] = 0;
for(int j = 0 ; j< vec[i].size(); ++ j)
sum[i] += len - vec[i][j] ;
}
int id ;
for(int i =1; i<= 26 ;++ i)
{
if( k <= sum[i])
{
id = i ;
break ;
}
k -= sum[i];
}
int p =vec[id].size();
int pre = 0;
for(int i = 0 ; i < len ;++ i)
{
if(s[sa[i]] != id + 'a' - 1)continue ;
int now = 1;
for(int j = sa[i] ; j < len ;++ j)
{
int t = j - sa[i] + 1;
int res = 0;
if(t > pre )res= 1;
for(int l = i + 1 ; l < len ; l ++)
{
if( height[l] < t)
break ;
if(t > pre)
res ++ ;
now = max(now , t);
}
if( k <= res)
{
for(int tt = sa[i] ; tt <= j ; ++ tt)
printf("%c",s[tt]);cout<<endl;
return 0;
}
k -= res;
}
pre = now ;
}
}