http://www.lydsy.com/JudgeOnline/problem.php?id=4516
http://blog.csdn.net/doyouseeman/article/details/52245413
后缀自动机的性质应用,求子串种类数。
又是自动机写错了查了半天,字符集太大了导致什么都没对拍出来。。。最后肉眼看出来了,我是个zz。
/**************************************************************
Problem: 4516
User: 137shoebills
Language: C++
Result: Accepted
Time:856 ms
Memory:14940 kb
****************************************************************/ #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
const int maxn=;
map< int , int >sig[maxn*];
int n;
struct nod{
int len,f;
}t[maxn*];
int la=,tot=;
long long ans=;
long long getnum(int x){return (long long)(t[x].len-t[t[x].f].len);}
void add(int z){
int x=++tot;int i=la;
t[x].len=t[la].len+;
for(;i&&!sig[i][z];i=t[i].f){
sig[i][z]=x;
}
if(!i){t[x].f=;ans+=getnum(x);}
else{
int p=sig[i][z];
if(t[p].len==t[i].len+){t[x].f=p;ans+=getnum(x);}
else{
int y=++tot;
t[y]=t[p];sig[y]=sig[p];
t[y].len=t[i].len+;
ans+=getnum(y);ans-=getnum(p);
t[p].f=t[x].f=y;
ans+=getnum(x)+getnum(p);
for(;i&&sig[i][z]==p;i=t[i].f){
sig[i][z]=y;
}
}
}
la=x;
printf("%lld\n",ans);
}
int main(){
memset(t,,sizeof(t));
scanf("%d",&n);int x;
for(int i=;i<=n;i++){scanf("%d",&x);add(x);}
return ;
}