【bzoj4199】[Noi2015]品酒大会 后缀自动机

听说对反串建SAM,fa树就是后缀树?

听说两个后缀的LCP就是LCA的mx?

然后就是个树上dp?

靠,老子边界打错了


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#define maxn 600010

using namespace std;

int head[maxn],next[2*maxn],to[2*maxn];
int ch[maxn][26],fa[maxn],mx[maxn],a[maxn],w[maxn],size[maxn];
char s[maxn];
long long cnt[maxn],ans[maxn];
long long MX[maxn],MN[maxn];
int n,m,num,tot=1,last=1;

void insert(int x,int W)
{
	int p=last,np=last=++tot;
	mx[np]=mx[p]+1;w[np]=W;
	while (p && !ch[p][x]) ch[p][x]=np,p=fa[p];
	if (!p) fa[np]=1;
	else
	{
		int q=ch[p][x];
		if (mx[q]==mx[p]+1) fa[np]=q;
		else
		{
			int nq=++tot;w[nq]=1e9+1;mx[nq]=mx[p]+1;
			memcpy(ch[nq],ch[q],sizeof(ch[q]));
			fa[nq]=fa[q];
			fa[np]=fa[q]=nq;
			while (p && ch[p][x]==q) ch[p][x]=nq,p=fa[p];
		}
	}
}

void addedge(int x,int y) {num++;to[num]=y;next[num]=head[x];head[x]=num;}

void dfs(int x)
{
	MX[x]=-1e9-1;MN[x]=1e9+1;size[x]=0;
	if (w[x]!=1e9+1) size[x]=1,MX[x]=MN[x]=w[x];
	for (int p=head[x];p;p=next[p])
	{
		dfs(to[p]);
		if (MX[x]!=-1e9-1 && MN[x]!=1e9+1 && MX[to[p]]!=-1e9-1 && MN[to[p]]!=1e9+1) 
		  ans[mx[x]]=max(ans[mx[x]],max(MX[x]*MX[to[p]],MN[x]*MN[to[p]]));
		MX[x]=max(MX[x],MX[to[p]]);MN[x]=min(MN[x],MN[to[p]]);
		cnt[mx[x]]+=1ll*size[x]*size[to[p]];size[x]+=size[to[p]];
	}
}

int main()
{
	scanf("%d",&n);
	scanf("%s",s+1);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=n;i>=1;i--) insert(s[i]-'a',a[i]);
	for (int i=1;i<=tot;i++) addedge(fa[i],i);
	for (int i=0;i<=n;i++) ans[i]=-1e18;
	w[1]=1e9+1;
	dfs(1);
	for (int i=n-1;i>=0;i--) cnt[i]+=cnt[i+1],ans[i]=max(ans[i],ans[i+1]);
	for (int i=0;i<=n-1;i++) if (cnt[i]) printf("%lld %lld\n",cnt[i],ans[i]); else printf("0 0\n");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值