感觉位置不同算相同还是比较好想的,我们top排序后算每个点后面连接了多少点就知道里面有多少字串了,然后求k大就行了;
考了位置不同算不同的话,我们就要考虑endpos了。那么具体体现在SAM上,就是后缀链接。如果有链接,就说明出现过相同后缀,我们才把他们连接在一起对吧;不然后缀链接就会接在根节点,这是性质。那么我们初始值就不设为1,在后缀链接上一样跑一次求和,就把位置不同的也考虑上了。
那么最后求k小还有一点,就是假如k小于后缀链接上的和,就说明这里的点不需要考虑,因为前面已经出现过了。
/*keep on going and never give up*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define db(x) cerr<<(#x)<<" "<<(x)<<" "<<endl;
#define endl "\n"
#define fast std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int maxn=2e6+10;
struct node{
int ch[26];
int len,fa;
node(){memset(ch,0,sizeof(ch));len=fa=0;}
}tre[maxn];int cnt=1,las=1;
int a[maxn],id[maxn],sz[maxn],sum[maxn];
void ex_sam(int c){
int p=las,np=las=++cnt;sz[np]=1;
tre[np].len=tre[p].len+1;
for(;p&&!tre[p].ch[c];p=tre[p].fa)tre[p].ch[c]=np;
if(p==0)tre[np].fa=1;
else{
int q=tre[p].ch[c];
if(tre[q].len==tre[p].len+1)tre[np].fa=q;
else{
int nq=++cnt;
tre[nq]=tre[q];tre[nq].len=tre[p].len+1;
tre[q].fa=tre[np].fa=nq;
for(;p&&tre[p].ch[c]==q;p=tre[p].fa)tre[p].ch[c]=nq;
}
}
}
int op,k;
void tops(){
for(int i=1;i<=cnt;i++) a[tre[i].len]++;
for(int i=1;i<=cnt;i++) a[i]+=a[i-1];
for(int i=1;i<=cnt;i++) id[a[tre[i].len]--]=i;//对每一个长度给个id
for(int i=cnt;i>=1;i--) sz[tre[id[i]].fa]+=sz[id[i]];
for(int i=1;i<=cnt;i++) op==0?(sum[i]=sz[i]=1):(sum[i]=sz[i]);
sz[1]=sum[1]=0;
for(int i=cnt;i>=1;i--){
for(int j=0;j<26;j++){
int v=tre[id[i]].ch[j];
if(!v)continue;
sum[id[i]]+=sum[v];
}
}
}
void query(int k){
int x=1;
while (k){
if(k<=sz[x]) break;
k-=sz[x];
for (int i=0;i<26;i++){
if (tre[x].ch[i]){
if (sum[tre[x].ch[i]]>=k){
putchar('a'+i);
x=tre[x].ch[i];
break;
}else k-=sum[tre[x].ch[i]];
}
}
}
}
string s;
signed main(){
fast
cin>>s;
for(auto c:s) ex_sam(c-'a');
cin>>op>>k;
tops();
// cout<<sum[1]<<endl;
if(sum[1]<k) cout<<-1;
else
query(k);
}