3238: [Ahoi2013] Difference time limit:20 Sec Memory limit:512 MB
submit:1420 solved:662
[Submit] [Status] [Discuss] Description
Input
One line, a string s
Output
A row, an integer that represents the value evaluated
Sample InputCacaoSample Output
54
HINT
2<=n<=500000,s made up of lowercase English letters
ExercisesRight collection: Subtree sz size on PNT tree (NQ node not counted)The string is inverted to build the suffix automaton, the two states LCP is the PNT tree LCA Val, is also the string of two prefixes of the longest public suffix, that is, the longest public prefix of the original string. each of the sons of a point where each occurrence of the 22 LCP is the Val of this point, and the remainder is the contribution of subtracting the longer string of LCP within the subtree. Note that the minus should be sz[i]*sz[i-1], and not the answer after the sub-tree deworming
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #defineMAXN 10000206 7typedefLong LongLL;8 structnode{9 intnext[ -],val,pnt;Ten }SAM[MAXN]; One intA[MAXN],NUM[MAXN],SZ[MAXN]; A intN,m,root,tot,last; - LL ANS,F[MAXN],G[MAXN]; - CharCH[MAXN]; the -InlinevoidAddintx) { - intNP = ++tot,p =Last ; -Sam[np].val = Sam[p].val +1; +SZ[NP] =1; - while(P &&!sam[p].next[x]) sam[p].next[x] = NP, p =Sam[p].pnt; + intQ =Sam[p].next[x]; A if(!q) Sam[p].next[x] = NP, SAM[NP].PNT =p; at Else if(q && Sam[p].val +1= = Sam[q].val) Sam[np].pnt =Q; - Else{ - intNQ = + +tot; -Sam[nq].val = Sam[p].val +1; -SAM[NQ].PNT =Sam[q].pnt; -SAM[Q].PNT = SAM[NP].PNT =NQ; inmemcpy (Sam[nq].next,sam[q].next,sizeof(Sam[q].next)); - while(P && sam[p].next[x] = = q) sam[p].next[x] = NQ, p =Sam[p].pnt; to if(Sam[p].next[x] = = q) sam[p].next[x] =NQ; + } -Last =NP; the } *InlinevoidGetsort () { $ for(inti =1; I <= tot; i++) num[sam[i].val]++;Panax Notoginseng for(inti =1; I <= N; i++) Num[i] + = num[i-1]; - for(inti =1; I <= tot; i++) a[num[sam[i].val]--] =i; the for(inti = tot; I >=1; i--) { +SZ[SAM[A[I]].PNT] + =Sz[a[i]]; AF[a[i]] + = (LL) sz[a[i] "* (Sz[a[i])-1); theF[SAM[A[I]].PNT]-= (LL) sz[a[i]] * (Sz[a[i])-1); + } - } $ intMain () { $scanf"%s", CH +1); -n = strlen (ch +1); - for(inti = n; I >=1; i--){ theAdd (Ch[i]-'a'); - }Wuyi Getsort (); theAns = (LL) (n +1) * N * (n-1) >>1; - for(inti =1; I <= tot; i++) ans-= f[i] *(LL) sam[i].val; Wuprintf"%lld\n", ans); - return 0; About}
View Code
Bzoj 3238 Differences