P4070 [SDOI2016] 生成魔咒

题意

给定一个字符串 s,对其每一个前缀求本质不同子串个数。

n105,|Σ|=O(n)

分析

求本质不同子串个数有经典结论是 总个数 减去 height 数组总和。

而对每个前缀求,实际上就是每次往 sa 数组里面插入一个数,然后动态维护 height 和。只需要维护一个 set,找到 i 后缀在排名数组上的前驱后继 pre,nxt,height 和减去 lcp(pre,nxt),加上 lcp(pre,i)+lcp(i,nxt)

但这个做法有点问题。假设,若 lcp(pre,i)2,那么我们维护的 height 和会加上 2。但实际上,在前缀为 i 时,它们的 lcp 只有 1,所以会造成多减。

一种好想的做法是维护一个树状数组记录前缀为 i 时需要多加多少的长度。这里介绍另一种简单的做法。

考虑将原字符串反转,对反转后的字符串做 sa,对前缀查询变为对后缀查询,答案显然不变。但这时 lcp 长度是确定的,不会受到后缀长度的限制,于是延续原先的做法即可。复杂度 O(nlogn)

点击查看代码
#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();
}
/*

*/

作者:dcytrl

出处:https://www.cnblogs.com/dcytrl/p/18690829

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   dcytrl  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2024-01-25 P9697 [GDCPC2023] Canvas(强联通分量)
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示