1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <queue>6#include <cstring>7 #definePAU Putchar (")8 #defineENT Putchar (' \ n ')9 using namespacestd;Ten Const intmaxn=1000000+Ten, sig=2; One structacauto{ A structNode{node*ch[sig],*fail;intCntCharx;} Ac[maxn],*root;intnodecnt; - intid[156];queue<node*>RAM; -node*NewNode () { thenode*T; - if(! Ram.empty ()) t=Ram.front (); - Elset=&ac[++nodecnt]; - //t->fail=root; +T->cnt=0; - returnT; + } A voidinit () { at for(intI=0; i<sig;i++) id[i+'a']=i; -root=&ac[0];root->cnt=0; root->x='#'; nodecnt=0;return; - } - voidInsertChar*s) { - intLen=strlen (s); node*x=Root; - for(intI=0; i<len;i++){ in intC=Id[s[i]]; - if(!x->ch[c]) x->ch[c]=NewNode (); tox=x->ch[c];x->x=S[i]; +} x->cnt++;return; - } the voidGetfail () { *Queue<node*>p; Q.push (root); $ while(!Q.empty ()) {Panax Notoginsengnode*x=Q.front (); Q.pop (); - for(intC=0; c<sig;c++){ the if(x->Ch[c]) { +node*p=x->fail; while(P&&!p->ch[c]) p=p->fail; Ax->ch[c]->fail=p?p->ch[c]:root; Q.push (x->ch[c]); the}ElseX->ch[c]=x==root?root:x->fail->Ch[c]; + } -}return; $ } $ intQueryChar*s) { - intLen=strlen (s); node*x=root,*p;intsum=0; - for(intI=0; i<len;i++){ the intC=id[s[i]];x=x->Ch[c]; - for(Node*p=x;p&&p->cnt;p=p->fail) sum++;Wuyi}returnsum; the } - }sol; WuInlineintRead () { - intx=0, sig=1;CharCh=GetChar (); About while(!isdigit (CH)) {if(ch=='-') sig=-1; ch=GetChar ();} $ while(IsDigit (CH)) x=Ten*x+ch-'0', ch=GetChar (); - returnx*=Sig; - } -InlinevoidWriteintx) { A if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; + intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; the for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; - } $ Chars[1000010],t[1000010]; the voidinit () { thescanf"%s", s); thescanf"%s", T); the sol.init (); - Sol.insert (s); in Sol.getfail (); the Write (Sol.query (t)); the return; About } the voidWork () { the return; the } + voidprint () { - return; the }Bayi intMain () {init (); work ();p rint ();return 0;}
AC Automatic correction