BZOJ4310 跳蚤(后缀数组+二分答案)

  注意到答案一定是原串的子串,于是考虑造出SA,二分答案是第几小的子串。第k小子串很容易在SA上求出。之后计算使他成为最大子串至少要在几个位置切割,对每个字典序比答案大的后缀,找到所有合法切割位置(求lcp即可),就转化成了选最少的点使每个区间都包含至少一个点的经典问题。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
#define N 100010
#define ll long long
int n,m,sa[N],sa2[N],rk[N<<],tmp[N<<],h[N],cnt[N],l[N];
char s[N];
ll tot=;
void make()
{
int m=;
for (int i=;i<=n;i++) cnt[rk[i]=s[i]]++,m=max(m,(int)s[i]);
for (int i=;i<=m;i++) cnt[i]+=cnt[i-];
for (int i=n;i>=;i--) sa[cnt[rk[i]]--]=i;
for (int k=;k<=n;k<<=)
{
int p=;
for (int i=n-k+;i<=n;i++) sa2[++p]=i;
for (int i=;i<=n;i++) if (sa[i]>k) sa2[++p]=sa[i]-k;
memset(cnt,,sizeof(cnt));
for (int i=;i<=n;i++) cnt[rk[i]]++;
for (int i=;i<=m;i++) cnt[i]+=cnt[i-];
for (int i=n;i>=;i--) sa[cnt[rk[sa2[i]]]--]=sa2[i];
memcpy(tmp,rk,sizeof(rk));
p=rk[sa[]]=;
for (int i=;i<=n;i++)
{
if (tmp[sa[i]]!=tmp[sa[i-]]||tmp[sa[i]+k]!=tmp[sa[i-]+k]) p++;
rk[sa[i]]=p;
}
if (p==n) break;
m=p;
}
for (int i=;i<=n;i++)
{
h[i]=max(h[i-]-,);
while (s[i+h[i]]==s[sa[rk[i]-]+h[i]]) h[i]++;
}
}
bool check(ll k)
{
int x=;
for (int i=;i<=n;i++)
if (k<=n-sa[i]+-h[sa[i]]) {x=i;break;}
else k-=n-sa[i]+-h[sa[i]];
k+=h[sa[x]];memset(l,,sizeof(l));
int lcp=k;if (k<n-sa[x]+) l[sa[x]+lcp-]=max(l[sa[x]+lcp-],sa[x]);
for (int i=x+;i<=n;i++)
{
lcp=min(lcp,h[sa[i]]);
if (!lcp) return ;
l[sa[i]+lcp-]=max(l[sa[i]+lcp-],sa[i]);
}
int t=;
for (int i=;i<=n;i++)
if (l[i])
{
t++;if (t>m) return ;
int x=i;while (x<n&&l[x+]<=i) x++;
i=x;
}
return ;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj4310.in","r",stdin);
freopen("bzoj4310.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
m=read();
scanf("%s",s+);n=strlen(s+);
make();
tot=1ll*n*(n+)>>;
for (int i=;i<=n;i++) tot-=h[i];
ll l=,r=tot,ans;
while (l<=r)
{
ll mid=l+r>>;
if (check(mid)) r=mid-,ans=mid;
else l=mid+;
}
for (int i=;i<=n;i++)
if (ans<=n-sa[i]+-h[sa[i]]) {for (int j=sa[i];ans+h[sa[i]]>;j++,ans--)printf("%c",s[j]);break;}
else ans-=n-sa[i]+-h[sa[i]];
return ;
}
上一篇:Net-Speeder为OpenVZ加速


下一篇:hdu 4869 Turn the pokers (2014多校联合第一场 I)