This is to maintain a total number of strings that appear from the current location in the node of the suffix automaton
1#include <cstdio>2#include <cstring>3#include <iostream>4 5 using namespacestd;6 #defineN 2000107 #defineM 268 9 structsamnode{TenSamnode *son[ -] , *F; One intL, SC; A voidinit () { -f =NULL; - for(intI=0; i< -; i++) Son[i] =NULL; theL = SC =0; - } -}*root, *Last , Sam[n]; - + intCNT, N, M, K, ret; - CharS[n]; + A voidinit () { atsam[0].init (); -root = last = &sam[cnt=0]; -RET =0; - } - - voidAddintx) in { -Samnode *p = &sam[++cnt], *jp=Last ; toP->init (); +P->l = jp->l+1; -P->sc=1; theLast =p; * for(; jp&&!jp->son[x]; jp=jp->f) jp->son[x]=p; $ if(!JP) P->f =Root;Panax Notoginseng Else{ - if(jp->l+1= = jp->son[x]->l) P->f = jp->Son[x]; the Else{ +Samnode *r = &sam[++cnt], *q = jp->Son[x]; AR->init (); the*r = *Q; +R->l = jp->l+1; -Q->f = P->f =R; $ for(; JP && jp->son[x]==q; jp=jp->f) jp->son[x]=R; $ } - } -p =Last ; the while(p->f) { -p = p->F;WuyiP->sc++; the } - } Wu Charstr[ -]; - intSolve () About { $Samnode *cur =Root; - intLen =strlen (str); - for(intI=0; I<len; i++){ - intv = str[i]-'a'; A if(cur->Son[v]) { +Cur = cur->Son[v]; the } - Else{ $ return 0; the } the } the returnCur->SC; the } - in the intMain () the { About //freopen ("Data_in.txt", "R", stdin); the intN, m, op; the while(~SCANF ("%d%d", &n, &m)) the { +scanf"%s", s); - the init ();Bayi for(intI=0; I<n; i++) Add (s[i]-'a'); the while(m--){ the //cout<<m<<endl; -scanf"%d", &op); - if(op==2){ the //cout<< "in" <<endl; thescanf"%s", str); theprintf"%d\n", Solve ()); the - } the Else{ thescanf"%s", str); theAdd (str[0]-'a');94 } the } the } the return 0;98}
Coj 1287 to find the number of occurrences of a matching string in a pattern string