problem
Bzoj Solution
Build a suffix automaton, and then we can use the idea of a binary-like search tree. First, the topological order is discharged, then the number of substrings represented by this node is processed, and then the enumeration of the small of the k is in it.
We might as well make g[u] represent the number of suffixes that can be represented from the node U, so you can do a topological motion.
We can easily get the equation:
G[U]=SZ[U]+∑U−>VG[V] g [u] = S Z [u] +∑u−> v G [v] g[u]=sz[u]+\sum_{u->v}g[v]
Note that when you calculate a repeating substring, you sz[f[i]]+=sz[i]. Code
#include <cstring> #include <cstdio> using namespace std;
const int maxn=1000010;
int t,k,n,lst,cnt,now=1,len,pre;
int A[MAXN],B[MAXN],C[MAXN],F[MAXN],L[MAXN],SZ[MAXN],CH[MAXN][26],G[MAXN];
Char s[maxn>>1];
void Insert (int c) {int p=lst,np=++cnt;
Lst=np;l[np]=l[p]+1;sz[np]=1;
For (;p &&!ch[p][c];p =f[p]) CH[P][C]=NP;
if (!p) f[np]=1;
else {int q=ch[p][c];
if (l[q]==l[p]+1) f[np]=q;
else {int nq=++cnt;l[nq]=l[p]+1;if (!t) sz[nq]=1;
Memmove (ch[nq],ch[q],sizeof (ch[q));
F[NQ]=F[Q];F[Q]=F[NP]=NQ;
for (; ch[p][c]==q;p=f[p]) ch[p][c]=nq;
}}} int main () {#ifndef Online_judge freopen ("In.txt", "R", stdin);
#endif scanf ("%s%d%d", s+1,&t,&k);
N=strlen (s+1); lst=cnt=1;
for (int i=1;i<=n;i++) Insert (s[i]-' a ');
for (int i=1;i<=cnt;i++) c[l[i]]++;
for (int i=1;i<=cnt;i++) c[i]+=c[i-1]; for (int i=1;i<=cnt;i++) a[c[L[i]]--]=i;
for (int i=cnt,j;t&&i;i--) {j=a[i];sz[f[j]]+=sz[j];}
sz[1]=sz[0]=0;
for (int i=cnt,j;i;i--) {j=a[i];g[j]=sz[j];
for (int r=0;r<26;r++) g[j]+=g[ch[j][r]];
} if (g[1]<k) {puts ("1"); return 0;}
while (K>sz[now]) {K-=sz[now];
for (int i=0;i<26;i++) {if (K>g[ch[now][i]]) k-=g[ch[now][i];
Else{putchar (i+ ' a '); now=ch[now][i];break;}
}} putchar (' \ n ');
return 0; }