[SDOI2016]生成魔咒 简易题解

         \ \ \ \ \ \ \,       一看这个题目:

  • 只是单调在后面加字符;
  • 只是统计子串数量。

         \ \ \ \ \ \ \,       哇!这不是 S A M SAM SAM 的裸题吗,周道感动得要哭了,根据【后缀自动机的性质】

         \ \ \ \ \ \ \,        i i i上面表示子串的数量为: l e n [ f a [ i ] ] − l e n [ i ] len[fa[i]]−len[i] len[fa[i]]len[i]

         \ \ \ \ \ \ \,       所以我们每次插入一个点,把新加的子串个数记累加就好了。

         \ \ \ \ \ \ \,       但是一看这个字符集大小……

         \ \ \ \ \ \ \,       不过我们知道,虽然字符集这么大,但是每个节点上面的儿子个数却是远远达不到字符集那么大的,所以我们试着用 m a p map map 代替数组来记录儿子:

         \ \ \ \ \ \ \,       然后……就过了……

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
using namespace std;
const int inf=0x7fffffff;
const double eps=1e-10;
const double pi=acos(-1.0);
//char buf[1<<15],*S=buf,*T=buf;
//char getch(){return S==T&&(T=(S=buf)+fread(buf,1,1<<15,stdin),S==T)?0:*S++;}
inline int read(){
	int x=0,f=1;char ch;ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-') f=0;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch&15);ch=getchar();}
	if(f)return x;else return -x;
}
const int N=200010;
long long ans; 
struct Suffix_Automaton{
	map<int,int>ch[N];
  	int last=1,cnt=1;
	int len[N],fa[N];
	void insert(int c){
		int np=++cnt,p=last;last=np;
		len[np]=len[p]+1;
		for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
		if(!p)fa[np]=1;
		else{
			int q=ch[p][c];
			if(len[q]==len[p]+1)fa[np]=q;
			else{
				int nq=++cnt;len[nq]=len[p]+1;
				ch[nq]=ch[q];
				fa[nq]=fa[q];fa[q]=fa[np]=nq;
				for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
			}
		}
		ans+=1ll*len[np]-len[fa[np]];
	}
}Sam;
int main()
{
	int n=read();
	while(n--){
		int a=read();
		Sam.insert(a);
		printf("%lld\n",ans);
	}
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值