[Str记录]P5334 [JSOI2019]节日庆典
command_block · · 个人记录
题意 : 给出串
配合 Border理论小记 中 Significant Suffixes 理论使用。
显然,一个位置是最小循环表示的开头
-
结论① 对于任意两个
{\rm Ssuf}\ U,V ,|U|<|V|\Rightarrow U 是V 的前缀。 -
结论② 若串
S 有两个{\rm Ssuf}\ U,V 满足|U|<|V| ,则2|U|<|V| 推论 :
\rm Ssuf 的个数是O(\log n) 的。
所以,我们只需要维护这些
维护方法 :每在串末端加入一个字符,则将这个字符作加入
接下来,按照长度从小到大检查相邻的两个
若
若
找出最小循环表示时需要支持字符串比较,若使用
寻找性质以简化问题,能够发现,我们比较的两个
设
同时消去
使用扩展
算法复杂度
#include<algorithm>
#include<cstring>
#include<cstdio>
#define MaxN 3005000
using namespace std;
char oupch[1 << 25], *oup = oupch;
inline void Write(int x) {
char *tmp = oup;
while(x) *(oup++) = x % 10 + '0', x /= 10;
reverse(tmp, oup), *(oup++) = ' ';
}
void zkmp(char *a,int n,int *z)
{
z[1]=n;
for (int i=2,l=0,r=0;i<=n;i++){
z[i]=(r<i) ? 0 : min(z[i-l+1],r-i+1);
while(i+z[i]<=n&&a[i+z[i]]==a[z[i]+1])z[i]++;
if (i+z[i]-1>r){l=i;r=i+z[i]-1;}
}
}
char s[MaxN];
int n,z[MaxN],st[25],top;
inline bool cmp(int x,int y,int lim)//x>y [x]<[y]
{
int p1=y+lim-x+1,len1=z[p1];
if (len1>=x-y){
int p2=x-y,len2=z[p2];
return len2+lim-x+1<lim&&s[p2+len2]<s[len2];
}return s[1+len1]<s[p1+len1];
}
int aaa;
int main()
{
scanf("%s",s+1);
n=strlen(s+1);
zkmp(s,n,z);
for (int p=1;p<=n;p++){
st[++top]=p;
for (int i=top;i>1;i--){
char sav=s[p-st[i]+st[i-1]];
if (sav>s[p]){st[i-1]=st[i];st[i]=0;}
if (s[p]>sav||(s[p]==sav&&2*(p-st[i]+1)>(p-st[i-1]+1)))st[i]=0;
}
int tn=0;
for (int i=1;i<=top;i++)
if (st[i])st[++tn]=st[i];
top=tn;
int ans=st[1];
for (int i=2;i<=top;i++)
if (cmp(st[i],ans,p))ans=st[i];
Write(ans);
}fwrite(oupch, 1, oup-oupch, stdout);
return 0;
}
-
O(n)
不会,咕咕咕。