"bzoj3238" [Ahoi2013] Difference description
Input
One line, a string s
Output
A row, an integer that represents the value evaluated
Sample InputCacaoSample Output Wu
Exercises
What is the LCP of any two strings, just as
A, b so if A==b is Len (a)
Otherwise set sa[a]<sa[b] then for min (height[sa[a]+1-------Sa[b]])
1#include <cstring>2#include <iostream>3#include <cmath>4#include <algorithm>5#include <cstdio>6 7 #defineN 5000078 #definell Long Long9 using namespacestd;TenInlineintRead () One { A intx=0, f=1;CharCh=GetChar (); - while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} - while(ch>='0'&&ch<='9') {x= (x<<1) + (x<<3) +ch-'0'; ch=GetChar ();} the returnx*F; - } - - intN; + intStk[n],f[n],g[n]; - structSA + { A CharS[n]; at inta[n],b[n],cnta[n],cntb[n],tsa[n],height[n],sa[n],rk[n*2]; - voidGet_sa () - { - for(intI=0; i<= the; i++) cnta[i]=0; - for(intI=1; i<=n;i++) cnta[(int) s[i]]++; - for(intI=1; i<= the; i++) cnta[i]+=cnta[i-1]; in for(inti=n;i>=1; i--) sa[cnta[(int) s[i]]--]=i; -rk[sa[1]]=1; to for(intI=2; i<=n;i++) rk[sa[i]]=rk[sa[i-1]]+ (s[sa[i]]!=s[sa[i-1]]); + for(intI=1; rk[sa[n]]!=n;i<<=1) - { the for(intj=1; j<=n;j++) a[j]=rk[j],b[j]=rk[j+i]; * for(intj=0; j<=n;j++) cnta[j]=cntb[j]=0; $ for(intj=1; j<=n;j++) cnta[a[j]]++,cntb[b[j]]++;Panax Notoginseng for(intj=1; j<=n;j++) cnta[j]+=cnta[j-1],cntb[j]+=cntb[j-1]; - for(intj=n;j>=1; j--) tsa[cntb[b[j]]--]=J; the for(intj=n;j>=1; j--) sa[cnta[a[tsa[j]]]--]=Tsa[j]; +rk[sa[1]]=1; A for(intj=2; j<=n;j++) therk[sa[j]]=rk[sa[j-1]]+ (a[sa[j]]!=a[sa[j-1]]|| b[sa[j]]!=b[sa[j-1]]); + } - } $ voidget_height () $ { - intlen=0; - for(intI=1; i<=n;i++) the { - if(len) len--;Wuyi while(s[i+len]==s[sa[rk[i]-1]+len]) len++; theheight[rk[i]]=Len; - } Wu } - }s; About intMain () $ { -scanf"%s", s.s+1); -N=strlen (s.s+1); -ll ans=0; A for(intI=1; i<=n;i++) + { theans+= (LL) (I-1)*i; -ans+= (LL) i* (i-1)/2; $ } the S.get_sa (); the s.get_height (); the inttot=0; the for(intI=2; i<=n;i++) - { in while(tot>0&&S.height[i]<S.height[stk[tot]]) thef[stk[tot--]]=i-1; thestk[++tot]=i; About } the while(TOT) f[stk[tot--]]=N; thetot=0; the for(inti=n;i>=2; i--) + { - while(tot>0&&s.height[i]<=s.height[stk[tot]]) g[stk[tot--]]=i+1; thestk[++tot]=i;Bayi } the while(TOT) g[stk[tot--]]=2; the for(intI=2; i<=n;i++) -ans-= (LL) s.height[i]* (LL) (f[i]-i+1) * (LL) (i-g[i]+1)*2; -printf"%lld\n", ans); the}
bzoj3238 [Ahoi2013] difference suffix array + monotonic stack