BZOJ.4516.[SDOI2016]生成魔咒(后缀自动机 map)
生活随笔
收集整理的這篇文章主要介紹了
BZOJ.4516.[SDOI2016]生成魔咒(后缀自动机 map)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接
后綴數(shù)組做法見(jiàn)這。
直接SAM+map。對(duì)于每個(gè)節(jié)點(diǎn)其產(chǎn)生的不同子串?dāng)?shù)為len[i]-len[fa[i]]。
//15932kb 676ms #include <map> #include <cstdio> #include <cctype> #include <algorithm> //#define gc() getchar() #define MAXIN 1500000 #define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++) const int N=2e5+5;char IN[MAXIN],*SS=IN,*TT=IN; struct Suffix_Automaton {int tot,las,fa[N],len[N];long long ans;std::map<int,int> son[N];void Insert(int c){int np=++tot,p=las; len[las=np]=len[p]+1;for(; p&&!son[p][c]; p=fa[p]) son[p][c]=np;if(!p) fa[np]=1;else{int q=son[p][c];if(len[q]==len[p]+1) fa[np]=q;else{int nq=++tot;len[nq]=len[p]+1, son[nq]=son[q];fa[nq]=fa[q], fa[q]=fa[np]=nq;for(; son[p][c]==q; p=fa[p]) son[p][c]=nq;}}printf("%lld\n",ans+=(long long)(len[np]-len[fa[np]]));} }sam;inline int read() {int now=0;register char c=gc();for(;!isdigit(c);c=gc());for(;isdigit(c);now=now*10+c-'0',c=gc());return now; }int main() {int n=read(); sam.las=sam.tot=1;for(int i=1; i<=n; ++i) sam.Insert(read());return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/SovietPower/p/9241663.html
總結(jié)
以上是生活随笔為你收集整理的BZOJ.4516.[SDOI2016]生成魔咒(后缀自动机 map)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: JAVA 框架-Spring
- 下一篇: 关于DJANGO MODELS的个人理解