bzoj千題計劃318:bzoj1396: 識別子串(字尾自動機 + 線段樹)
#include<cstdio> #include<cstring> #include<algorithm> #define N 100001 using namespace std; char s[N]; int ch[N<<1][26],tot=1; int fa[N<<1],len[N<<1]; int siz[N<<1]; int last=1,p,q,np,nq; int leaf[N]; int v[N],sa[N<<1]; structSegment { int mx[N<<2]; int tag[N<<2]; void down(int k) { mx[k<<1]=min(mx[k<<1],tag[k]); mx[k<<1|1]=min(mx[k<<1|1],tag[k]); tag[k<<1]=min(tag[k<<1],tag[k]); tag[k<<1|1]=min(tag[k<<1|1],tag[k]); tag[k]=2e9; } void build(int k,int l,int r) { mx[k]=tag[k]=2e9; if(l==r) return; int mid=l+r>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); } void change(int k,int l,int r,int opl,int opr,intw) { if(l>=opl && r<=opr) { mx[k]=min(mx[k],w); tag[k]=min(tag[k],w); return; } int mid=l+r>>1; if(tag[k]!=2e9) down(k); if(opl<=mid) change(k<<1,l,mid,opl,opr,w); if(opr>mid) change(k<<1|1,mid+1,r,opl,opr,w); mx[k]=min(mx[k<<1],mx[k<<1|1]); } int query(int k,int l,int r,int x) { if(l==r) return mx[k]; int mid=l+r>>1; if(tag[k]!=2e9) down(k); if(x<=mid) return query(k<<1,l,mid,x); return query(k<<1|1,mid+1,r,x); } }; Segment tr1,tr2; void extend(int c) { len[np=++tot]=len[last]+1; siz[tot]=1; for(p=last;p && !ch[p][c];p=fa[p]) ch[p][c]=np; if(!p) fa[np]=1; else { q=ch[p][c]; if(len[q]==len[p]+1) fa[np]=q; else { nq=++tot; fa[nq]=fa[q]; memcpy(ch[nq],ch[q],sizeof(ch[nq])); fa[q]=fa[np]=nq; len[nq]=len[p]+1; for(;ch[p][c]==q;p=fa[p]) ch[p][c]=nq; } } last=np; } int main() { scanf("%s",s+1); int n=strlen(s+1); for(int i=1;i<=n;++i) { leaf[i]=tot+1; extend(s[i]-'a'); } for(int i=1;i<=tot;++i) v[len[i]]++; for(int i=1;i<=n;++i) v[i]+=v[i-1]; for(int i=1;i<=tot;++i) sa[v[len[i]]--]=i; int x; for(int i=tot;i;--i) { x=sa[i]; siz[fa[x]]+=siz[x]; } tr1.build(1,1,n); tr2.build(1,1,n); int l,r,end; for(int x=2;x<=tot;++x) if(siz[x]==1) { l=len[fa[x]]; r=len[x]; end=len[x]; tr1.change(1,1,n,end-l,end,l+1); tr2.change(1,1,n,end-r+1,end-l,end+1); } int a,b; for(int i=1;i<=n;++i) { a=tr1.query(1,1,n,i); b=tr2.query(1,1,n,i)-i; printf("%d\n",min(a,b)); } return 0; }
相關推薦
bzoj千題計劃318:bzoj1396: 識別子串(字尾自動機 + 線段樹)
#include<cstdio> #include<cstring> #include<algorithm> #define N 100001 using namespace std; char s[N]; int ch[N<&
bzoj 1396: 識別子串 (字尾自動機+線段樹)
1396: 識別子串 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 308 Solved: 190 [Submit][Status][Discuss] Description Input 一行,一個由小寫字母組成
bzoj千題計劃312:bzoj2119: 股市的預測(字尾陣列+st表)
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #d
bzoj千題計劃319:bzoj2865: 字串識別(字尾自動機 + 線段樹)
#include<map> #include<cstdio> #include<cstring> #include<algorithm> #define N 500001 using namespace std; char s[
bzoj千題計劃309:bzoj4332: JSOI2012 分零食(分治+FFT)
#include<cmath> #include<cstdio> #include<algorithm> using namespace std; const int M=1<<17; #define N 10001 int m,
bzoj千題計劃322:bzoj2561: 最小生成樹(最小割)
#include<cstdio> #include<queue> #include<cstring> #include<iostream> #include<algorithm> using namespace std;
bzoj千題計劃311:bzoj5017: [Snoi2017]炸彈(線段樹優化tarjan構圖)
#include<cstdio> #include<vector> #include<iostream> #include<algorithm> using namespace std; const int mod=1e9+7;
bzoj千題計劃310:bzoj5285: [Hnoi2018]尋寶遊戲(思維題+雜湊)
#include<cstdio> #include<algorithm> #define N 5001 using namespace std; const int mod=1e9+7; int bit[N]; char s[N]; int has
bzoj千題計劃316:bzoj3173: [Tjoi2013]最長上升子序列(二分+樹狀陣列)
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 100001 #define lowbit(x) x&-x
bzoj千題計劃321:bzoj5251: [2018多省省隊聯測]劈配(網路流 + 二分)
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #d
bzoj千題計劃317:bzoj4650: [Noi2016]優秀的拆分(字尾陣列+差分)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 30002 using namespace std; int n;
bzoj千題計劃323:bzoj1951: [Sdoi2010]古代豬文(Lucas+CRT+尤拉定理)
#include<cmath> #include<cstdio> #include<iostream> using namespace std; const int mod=999911659; const int phi=mod-1; typ
bzoj千題計劃324:bzoj5249: [2018多省省隊聯測]IIIDX(線段樹)
#include<cmath> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 500001 int d[N
bzoj千題計劃313:bzoj3879: SvT(字尾陣列+st表+單調棧)
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 500001 #define M 3000001 int n,m,mm;
bzoj千題計劃314:bzoj3238: [Ahoi2013]差異(字尾陣列+st表+單調棧)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define N 500001 int n
bzoj千題計劃320:bzoj4939: [Ynoi2016]掉進兔子洞(莫隊 + bitset)
#include<cmath> #include<cstdio> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> us
bzoj千題計劃308:bzoj4589: Hard Nim(倍增FWT+生成函式)
#include<cstdio> #include<cstring> using namespace std; #define N 50001 const int mod=1e9+7; const int M=1<<16; int inv
bzoj千題計劃315:bzoj3172: [Tjoi2013]單詞(AC自動機)
#include<queue> #include<cstdio> #include<cstring> using namespace std; #define N 2000001 using namespace std; int pos[20
BZOJ1396: 識別子串(後綴自動機,線段樹)
ans 距離 text inpu EDA algo 帶來 有效 can Description Input 一行,一個由小寫字母組成的字符串S,長度不超過10^5 Output L行,每行一個整數,第i行的數據表示關於S的第i個元素的最短識別子串有多長. Sa
BZOJ1396識別子串(後綴自動機)
cout oid out node ons spa amp 大小 nod 題目鏈接 BZOJ 解析 後綴自動機+線段樹 若一個子串可識別,那麽它的\(right\)集合大小一定為\(1\) 對於一個\(right\)大小為\(1\)的節點: 它的\(right\)僅包含\