BZOJ 4516: [Sdoi2016]生成魔咒 字尾自動機
阿新 • • 發佈:2019-02-04
4516: [Sdoi2016]生成魔咒
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1328 Solved: 745
[Submit][Status][Discuss]
Description
魔咒串由許多魔咒字元組成,魔咒字元可以用數字表示。例如可以將魔咒字元 1、2 拼湊起來形成一個魔咒串 [1,2]。一個魔咒串 S 的非空字串被稱為魔咒串 S 的生成魔咒。例如 S=[1,2,1] 時,它的生成魔咒有 [1]、[2]、[1,2]、[2,1]、[1,2,1] 五種。S=[1,1,1] 時,它的生成魔咒有 [1]、[1,1]、[1,1,1] 三種。最初 S 為空串。共進行 n 次操作,每次操作是在 S 的結尾加入一個魔咒字元。每次操作後都需要求出,當前的魔咒串 S 共有多少種生成魔咒。Input
第一行一個整數 n。 第二行 n 個數,第 i 個數表示第 i 次操作加入的魔咒字元。 1≤n≤100000。,用來表示魔咒字元的數字 x 滿足 1≤x≤10^9Output
輸出 n 行,每行一個數。第 i 行的數表示第 i 次操作後 S 的生成魔咒數量
Sample Input
71 2 3 3 3 1 2
Sample Output
13
6
9
12
17
22
考慮每次加入一個字元,可能產生的新子串一定是一個字尾
所以我們只要每次找到新建節點的parent
其mx就是不會產生的新字尾數 用串長減掉即可
突然發現個nq賦狀態的時候寫了個迭代器。。太傻了。。。
#include<cmath> #include<ctime> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #include<iomanip> #include<vector> #include<string> #include<bitset> #include<queue> #include<set> #include<map> using namespace std; typedef long long ll; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();} return x*f; } void print(ll x) {if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');} const int N=200100; struct SAM { map<int,int> trans[N]; int par[N],mx[N]; int sz,suff,root; SAM(){sz=suff=root=1;} int insert(int x) { int p=suff,np=++sz; mx[np]=mx[p]+1; while(p && !trans[p][x]) trans[p][x]=np,p=par[p]; if(!p) par[np]=root; else { int q=trans[p][x]; if(mx[q]==mx[p]+1) par[np]=q; else { int nq=++sz; mx[nq]=mx[p]+1; map<int,int>::iterator it; for(it=trans[q].begin();it!=trans[q].end();++it) trans[nq][it->first]=it->second; par[nq]=par[q]; par[q]=par[np]=nq; while(p && trans[p][x]==q) trans[p][x]=nq,p=par[p]; } } suff=np; return mx[par[np]]; } }sam; int main() { int n=read(); ll ans(0); register int i,x; for(i=1;i<=n;++i) { x=read(); ans+=i-sam.insert(x); print(ans);putchar('\n'); } return 0; }