Reprint please specify the source 233:http://blog.csdn.net/vmurder/article/details/42875307
This is a divine question.
First we need to build an AC automaton and then build a fail tree, and then we find
If a string is asked to appear in the string B several times, then only need to look at the string B in the AC automaton all nodes in the number of nodes, in the end of a string in the tree of the fail trees can be.
And that would be fine, but still not AC,
Then we just need to follow the DFS sequence of the fail tree to create a data structure (I wrote a tree array) to do interval query.
At this time for the above B-string, we follow the DFS sequence sweep (according to the input string is the natural superior DFS order), and then each transfer of a node, on the data structure Gaga minus,
Then for each ' B ', the chain asks what ' A ' has appeared in it, and the tree-like array O (LOGN) is a good time to check.
Single-point modification interval query, time complexity Mlogn.
Code:
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #define N 101000#define MAX 220000#define T 26using namespace std;struct fail{int v[n],next[n],head[n],cnt; void cls () {Cnt=0;memset (head,0,sizeof head);} void Add (int u,int _v) {v[++cnt]=_v;next[cnt]=head[u];head[u]=cnt;} int in[n],out[n],dfn;void BUILD_DFN (int x=0) {in[x]=++dfn;for (int i=head[x];i;i=next[i]) BUILD_DFN (V[i]); out[x]=++ DFN;}} Fail;struct FENWICK//tree array {int fenwick[max];inline void Add (int x,int W) {for (x=fail.in[x];x<max;x+= (x&-x)) Fenwick[x]+=w;} inline int query (int x) {int ans=0,temp=x;for (x=fail.out[temp]; x;x-= (x&-x)) ans+=fenwick[x];for (x=fail.in[temp]- 1;x;x-= (x&-x)) Ans-=fenwick[x];return ans;}} Fw;struct trie{int pa[n],next[n][t],fail[n];int end[n],crs[n];int root,cnt,id;char s[N];void Build_trie ()//Build trie{ scanf ("%s", s), int i,x=root=0,alp;for (i=0;s[i];i++) {if (s[i]== ' P ') end[x]=++id,crs[id]=x;else if (s[i]== ' B ') x=pa[x]; else {alp=s[i]-' a '; if (!neXt[x][alp]) Next[x][alp]=++cnt,pa[cnt]=x;x=next[x][alp];}} void Build_fail ()//Establish AC Automaton {queue<int>q;q.push (root); int I,u,v,temp;while (!q.empty ()) {U=q.front (), Q.pop (); for (i=0;i<t;i++) if (V=next[u][i]) {if (u==root) fail[v]=root;else {temp=fail[u];while (temp&&!next[temp][ I]) temp=fail[temp];fail[v]=next[temp][i];} Q.push (v); Fail.add (FAIL[V],V);}}} int v[n],nxt[n],head[n];void Add (int u,int _v)//Ask offline {v[++cnt]=_v;nxt[cnt]=head[u];head[u]=cnt;} int m,fians[n];void input ()//input Query {int a,b;scanf ("%d", &m), cnt=0;for (int i=1;i<=m;i++) {scanf ("%d%d", &a, &B); add (b,a);}} void query (int PDD) {for (int i=head[pdd];i;i=nxt[i]) fians[i]=fw.query (Crs[v[i]);} void work ()//traversal B string {int i,x=root=0,alp,now;for (i=0;s[i];i++) {if (s[i]== ' P ') query (end[x]); else if (s[i]== ' B ') fw.add (x, -1), X=pa[x];else {alp=s[i]-' a '; X=next[x][alp];fw.add (x,1);}} return;} void output ()//output answer {for (int i=1;i<=m;i++) printf ("%d\n", Fians[i]);}} Trie;int Main () {//freopen ("my.in", "R", stdin);//freopen ("My.out", "w", stdout);Trie.build_trie (); Trie.build_fail (); FAIL.BUILD_DFN (); Trie.input (); Trie.work (); Trie.output (); return 0;}
"BZOJ2434" "NOI2011" Ali typewriter AC automatic machine