传送门
题意:求字典序第k小的字串
对于T为0的情况,每个状态我们计数都为1
对于T为1的情况,对于每个状态他的计数应加上他fail树结束节点的个数(实际的对应串的个数)
然后随便DFS就好了
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include
using namespace std;
typedef long long ll;
const int N = 5e5 + 10;
struct SAM
{
static const int KN = N << 1;
static const int KM = 30;
int fail[KN], net[KN][KM], len[KN], cnt, root;
int val[KN],sum[KN];
int newnode(int _len)
{
memset(net[cnt], -1, sizeof(net[cnt]));
//fail[cnt] = -1;
len[cnt] = _len;
return cnt++;
}
void init()
{
cnt = 0;
memset(fail,-1,sizeof(fail));
root = newnode(0);
}
int add(int p, int x)
{
int np = newnode(len[p] + 1);
val[np]=1;
while(~p && net[p][x] == -1) net[p][x] = np, p = fail[p];
if(p == -1) fail[np] = root;
else
{
int q = net[p][x];
if(len[q] == len[p] + 1) fail[np] = q;
else
{
int nq = newnode(len[p] + 1);
memcpy(net[nq], net[q], sizeof(net[q]));
fail[nq] = fail[q];
fail[q] = fail[np] = nq;
while(~p && net[p][x] == q) net[p][x] = nq, p = fail[p];
}
}
return np;
}
void build(char *s, char ch)
{
int now = root;
for(int i = 0; s[i]; ++i) now = add(now, s[i] - ch);
}
int ord[KN], pri[KN];
void topo()
{
int maxVal=0;
memset(pri, 0, sizeof(pri));
for (int i = 0; i < cnt; ++i) maxVal = max(maxVal, len[i]), ++ pri[len[i]];
for (int i = 1; i <= maxVal; ++i) pri[i] += pri[i - 1];
for (int i = 0; i < cnt; ++i) ord[--pri[len[i]]] = i;
}
void sum_init(int t)
{
for(int i=cnt-1;i>=0;i--)
{
int pos=ord[i];
if(t) val[fail[pos]]+=val[pos];
else val[pos]=1;
}
val[0]=0;
for(int i=cnt-1;i>=0;i--)
{
int pos=ord[i];
sum[pos]=val[pos];
for(int j=0;j<26;j++) sum[pos]+=sum[net[pos][j]];
}
}
void dfs(int k,int x)
{
if(val[x]>=k) return ;
else k-=val[x];
for(int i=0;i<26;i++)
{
int pos=net[x][i];
if(pos)
{
if(k<=sum[pos])
{
printf("%c",i+'a');
dfs(k,pos);
return ;
}
else k-=sum[pos];
}
}
}
void gao(int t,int k)
{
topo();
sum_init(t);
dfs(k,0);
}
} sam;
char s[N];
int main()
{
scanf("%s",s);
int t,k;
scanf("%d%d",&t,&k);
sam.init();
sam.build(s,'a');
sam.gao(t,k);
}