您的位置:首页 > 其它

[BZOJ3998][TJOI2015]弦论(后缀自动机)

2017-01-01 11:26 459 查看

题目描述

传送门

题解

还是要统计每一个点往后走能有多少个不同的子串。

如果T=0的时候就是主链上的点都是1,然后在parent树上把儿子加到父亲上去

如果T=1的是候则需要计算每一个点right集合的大小

最后在自动机上暴力找第k小的然后输出即可

代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long
#define N 1000005

char s
;
int n,T,p,np,q,nq,sz,root,last;
int ch
[30],pre
,step
,c
,pt
,rt
;
LL k,sum
;

void extend()
{
for (int i=0;i<n;++i)
{
int x=s[i]-'a';
p=last;np=++sz;last=np;
step[np]=step[p]+1;
while (p&&!ch[p][x])
{
ch[p][x]=np;
p=pre[p];
}
if (!p) pre[np]=root;
else
{
q=ch[p][x];
if (step[q]==step[p]+1) pre[np]=q;
else
{
nq=++sz;
step[nq]=step[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
pre[nq]=pre[q];
pre[np]=pre[q]=nq;
while (ch[p][x]==q)
{
ch[p][x]=nq;
p=pre[p];
}
}
}
}
}
void sam()
{
LL now=0;p=root;
while (1)
{
for (int i=0;i<26;++i)
{
if (now+sum[ch[p][i]]<k)
now+=sum[ch[p][i]];
else
{
putchar('a'+i);
now+=(LL)rt[ch[p][i]];
if (now>=k) return;
p=ch[p][i];
break;
}
}
}
}
int main()
{
gets(s);n=strlen(s);
root=last=++sz;
extend();
for (int i=1;i<=sz;++i) ++c[step[i]];
for (int i=1;i<=n;++i) c[i]+=c[i-1];
for (int i=sz;i>=1;--i) pt[c[step[i]]--]=i;
scanf("%d%lld",&T,&k);
if (!T)
{
for (int i=1;i<=sz;++i) rt[i]=1;
}
else
{
p=root;
for (int i=0;i<n;++i)
{
int x=s[i]-'a';
p=ch[p][x];
rt[p]=1;
}
for (int i=sz;i>=1;--i)
{
p=pt[i];
rt[pre[p]]+=rt[p];
}
}
for (int i=sz;i>=1;--i)
{
p=pt[i];
if (p!=1) sum[p]=(LL)rt[p];
for (int j=0;j<26;++j)
sum[p]+=sum[ch[p][j]];
}
if (k>sum[1]) puts("-1");
else sam();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: