P4070 [SDOI2016] 生成魔咒
题意
给定一个字符串
分析
求本质不同子串个数有经典结论是 总个数 减去 height 数组总和。
而对每个前缀求,实际上就是每次往 sa 数组里面插入一个数,然后动态维护 height 和。只需要维护一个 set,找到
但这个做法有点问题。假设,若
一种好想的做法是维护一个树状数组记录前缀为
考虑将原字符串反转,对反转后的字符串做 sa,对前缀查询变为对后缀查询,答案显然不变。但这时 lcp 长度是确定的,不会受到后缀长度的限制,于是延续原先的做法即可。复杂度
点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define il inline
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof x)
#define popc __builtin_popcountll
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) ((x)&-(x))
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
template<typename T1,typename T2>inline void ckmx(T1 &x,T2 y){x=x>y?x:y;}
template<typename T1,typename T2>inline void ckmn(T1 &x,T2 y){x=x<y?x:y;}
inline auto rd(){
int qwqx=0,qwqf=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')qwqf=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){qwqx=(qwqx<<1)+(qwqx<<3)+ch-48;ch=getchar();}return qwqx*qwqf;
}
template<typename T>inline void write(T qwqx,char ch='\n'){
if(qwqx<0){qwqx=-qwqx;putchar('-');}
int qwqy=0;char qwqz[40];
while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
while(qwqy--)putchar(qwqz[qwqy]);if(ch)putchar(ch);
}
bool Mbg;
const int maxn=1e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,a[maxn];
int h[maxn],hcnt;
void lsh(){
rep(i,1,n)h[i]=a[i];
sort(h+1,h+n+1),hcnt=unique(h+1,h+n+1)-(h+1);
rep(i,1,n)a[i]=lower_bound(h+1,h+hcnt+1,a[i])-h;
}
int sa[maxn],rk[maxn],nrk[maxn],prk[maxn<<1],id[maxn];
int cnt[maxn];
bool cmp(int x,int y,int z){
return prk[x]==prk[y]&&prk[x+z]==prk[y+z];
}
int hi[maxn];
void SA(){
int m=hcnt;
rep(i,1,n)cnt[rk[i]=a[i]]++;
rep(i,2,m)cnt[i]+=cnt[i-1];
per(i,n,1)sa[cnt[rk[i]]--]=i;
for(int j=1,p=0;j<n&&p<n;j<<=1,m=p,p=0){
rep(i,n-j+1,n)id[++p]=i;
rep(i,1,n)if(sa[i]>j)id[++p]=sa[i]-j;
rep(i,1,m)cnt[i]=0;
rep(i,1,n)cnt[nrk[i]=rk[id[i]]]++;
rep(i,2,m)cnt[i]+=cnt[i-1];
per(i,n,1)sa[cnt[nrk[i]]--]=id[i];
rep(i,1,n)prk[i]=rk[i];
p=1,rk[sa[1]]=1;
rep(i,2,n)rk[sa[i]]=cmp(sa[i-1],sa[i],j)?p:++p;
}
// rep(i,1,n)write(sa[i],32);P__;
int p=0;
rep(i,1,n){
if(p)--p;
while(a[i+p]==a[sa[rk[i]-1]+p])++p;
hi[rk[i]]=p;
}
// rep(i,1,n)write(hi[i],32);P__;
}
i64 sum;
set<int>s;
int f[maxn][20],lg[maxn];
int lcp(int x,int y){
if(x==0||y==n+1)return 0;
int l=x+1,r=y,p=lg[r-l+1];
return min(f[l][p],f[r-(1<<p)+1][p]);
}
inline void solve_the_problem(){
n=rd();
rep(i,1,n)a[i]=rd();
reverse(a+1,a+n+1);
lsh();
SA();
rep(i,2,n)lg[i]=lg[i>>1]+1;
rep(i,1,n)f[i][0]=hi[i];
const int M=lg[n];
rep(j,1,M)rep(i,1,n-(1<<j)+1)f[i][j]=min(f[i][j-1],f[i+(1<<(j-1))][j-1]);
s.insert(0),s.insert(n+1);
per(i,n,1){
i64 j=n-i+1,tot=j*(j+1)/2;
auto it=s.upper_bound(rk[i]);
int nxt=*it,pre=*(--it);
sum-=lcp(pre,nxt)-lcp(pre,rk[i])-lcp(rk[i],nxt);
write(tot-sum),s.insert(rk[i]);
}
}
bool Med;
signed main(){
// freopen(".in","r",stdin);freopen(".out","w",stdout);
fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
int _=1;
while(_--)solve_the_problem();
}
/*
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2024-01-25 P9697 [GDCPC2023] Canvas(强联通分量)