【刷题】BZOJ 4516 [Sdoi2016]生成魔咒
生活随笔
收集整理的這篇文章主要介紹了
【刷题】BZOJ 4516 [Sdoi2016]生成魔咒
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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^9
Output
輸出 n 行,每行一個數。第 i 行的數表示第 i 次操作后 S 的生成魔咒數量
Sample Input
7
1 2 3 3 3 1 2 Sample Output
1
3
6
9
12
17
22 Solution
SAM模板題
每次加入新字符,對答案造成的貢獻是 \(len[x]-len[fa[x]]\) ,因為以新字符為結尾,開頭可以有 \(len[x]-len[fa[x]]\) 種情況
然后字符集的范圍是真的無聊,于是就上了pbds的hash_table
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
using namespace __gnu_pbds;
const int MAXN=100000+10;
int las=1,tot=1,len[MAXN<<1],fa[MAXN<<1],n;
ll ans;
cc_hash_table<int,int> ch[MAXN<<1];
template<typename T> inline void read(T &x)
{T data=0,w=1;char ch=0;while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();if(ch=='-')w=-1,ch=getchar();while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+'0');if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline ll extend(int c)
{int p=las,np=++tot;las=np;len[np]=len[p]+1;while(p&&!ch[p][c])ch[p][c]=np,p=fa[p];if(!p)fa[np]=1;else{int q=ch[p][c];if(len[q]==len[p]+1)fa[np]=q;else{int nq=++tot;fa[nq]=fa[q];ch[nq]=ch[q];len[nq]=len[p]+1,fa[q]=fa[np]=nq;while(p&&ch[p][c]==q)ch[p][c]=nq,p=fa[p];}}return len[np]-len[fa[np]];
}
int main()
{read(n);for(register int i=1,x;i<=n;++i){read(x);ans+=extend(x);write(ans,'\n');}return 0;
}
轉載于:https://www.cnblogs.com/hongyj/p/9102276.html
總結
以上是生活随笔為你收集整理的【刷题】BZOJ 4516 [Sdoi2016]生成魔咒的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《长相思·九月西风兴》第一句是什么
- 下一篇: python安装虚拟环境virtuale