Main topic:
Given a base string, and then multiple strings, the number of different substrings that can be obtained in the underlying string does not appear in the following multiple strings
It's okay to build the suffix from the base string here.
Multiple substrings in the back are constantly matched on the suffix automaton, and each time a state point is reached, it is necessary to update the other string reached by the current point to reach the maximum length mx, then the length of the mismatch is CUR->L-MX
because the parent node is affected by child nodes, it is necessary to sort the topology, update the maximum length of the parent node , and finally count the sum of all the numbers.
1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6 #defineN 1000107 #definell Long Long8 structsamnode{9Samnode *son[ -] , *F;Ten intl, MX; One voidinit () { A for(intI=0; i< -; i++) Son[i] =NULL; -f=NULL; -L = mx =0; the } -}sam[n<<1], *root, *last, *b[n<<1]; - - intcnt, num[n]; + ll ret; - CharS[n]; + A voidAddintx) at { -Samnode *p = &sam[++cnt], *JP =Last ; -P->init (); -P->l = jp->l+1 ; -Last =p; - for(; jp&&!jp->son[x]; jp=jp->f) jp->son[x] =p; in if(!JP) P->f =Root; - Else{ to if(jp->l+1= = jp->son[x]->l) P->f = jp->Son[x]; + Else{ -Samnode *r = &sam[++cnt], *q = jp->Son[x]; theR->init (); **r = *Q; $R->l = jp->l+1;Panax NotoginsengP->f = Q->f =R; - for(; jp&&jp->son[x]==q; jp=jp->f) jp->son[x]=R; the } + } A } the + voidSolveintNint&CAs) - { $ intL, len=strlen (s); $memset (NUM,0,sizeof(num)); - for(intI=0; i<=cnt; i++) num[sam[i].l]++; - for(intI=1; I<=len; i++) num[i]+=num[i-1]; the for(intI=0; i<=cnt; i++) b[--num[sam[i].l]]=&Sam[i]; - for(intI=0; I<n; i++){Wuyiscanf"%s", s); theL=strlen (s), len=0;//Len indicates the maximum length that can be reached at the moment -Samnode *cur =Root; Wu for(intj=0; J<l; J + +){ - intx = s[j]-'a'; About if(cur->Son[x]) { $len++; -Cur = cur->Son[x]; -CUR->MX = Max (cur->MX, len); -}Else{ A while(Cur&&!cur->son[x]) cur=cur->F; + if(!cur) thecur = root, len=0; - Else{ $Len = cur->l+1; theCur = cur->Son[x]; theCUR->MX = Max (cur->MX, len); the } the } - } in } theLL ret =0; the for(inti=cnt; i>=1; i--){ About //cout<<i<< "" <<b[i]->l<< "" <<b[i]->mx<<endl; the if(B[I]->MX) ret = ret + (b[i]->l-b[i]->mx>=0? B[I]->L-B[I]->MX:0); the ElseRET = ret + b[i]->l-b[i]->f->l; theB[I]->F->MX = Max (B[I]->F->MX, b[i]->MX); + } -printf"Case %d:%i64d\n", ++cas, ret); the }Bayi the intMain () the { - //freopen ("a.in", "R", stdin); - intT, N, cas=0; thescanf"%d", &T); the while(t--) the { thescanf"%d%s", &N, s); - intL =strlen (s); thesam[0].init (); theroot = last = &sam[cnt=0], ret =0; the for(intI=0; I<l; i++)94Add (s[i]-'a'); the solve (n, CAs); the } the return 0;98}
HDU 4622 Reincarnation