Reprint please indicate 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 find
If you ask a string how many times it appears in the B string, you only need to look at the number of nodes in all the nodes in the AC automata on the B string, and at the end of the a string in the subtree of the fail tree.
And then it's okay, but it's still not AC,
Then we just need to build the data structure (I wrote the tree array) to do the interval query according to the DFS order of the fail tree.
For the above B string, we sweep through the DFS sequence (as the input string is the natural superior DFS sequence), and then each node is transferred, the data structure on the reduction,
Then for each ' B ', ask in the chain question which ' A ' is in it, how many times, the tree-like array o (LOGN) check once.
Single point modification interval query, time complexity degree mlogn.
Code:
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <
algorithm> #define N 101000 #define MAX 220000 #define T-using 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 ()//establish Trie {scanf ("%s", s); IntI,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];
Establish AC automata {queue<int>q;}} void Build_fail ()/
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)//query 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 ()//outputs 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;
}