在当前字符串的后面插入一个字符串
询问字符串s在当前字符串中出现了几次?(作为连续子串)
必须在线支持这些操作。
只要维护后缀自动机上每一个节点的right集合大小就可以了。。。
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 1200005
#define maxm 100005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head
struct node
{
int len, cnt;
node *fa, *next[26];
}*tail, *last, *root, pool[maxn];
char op[maxn];
char s[3000005];
int m, mask;
node* newnode(int len)
{
tail->len = len, tail->cnt = 0, tail->fa = 0;
memset(tail->next, 0, sizeof tail->next);
return tail++;
}
void init()
{
tail = pool;
root = newnode(0);
last = root;
}
void add(int c)
{
node *p = last, *np = newnode(p->len + 1);
last = np;
for(; p && !p->next[c]; p = p->fa) p->next[c] = np;
if(!p) np->fa = root;
else {
node *q = p->next[c];
if(p->len + 1 == q->len) np->fa = q;
else {
node *nq = newnode(p->len + 1);
*nq = *q;
nq->len = p->len + 1;
q->fa = np->fa = nq;
for(; p && p->next[c] == q; p = p->fa) p->next[c] = nq;
}
}
for(; np; np = np->fa) np->cnt++;
}
void read()
{
scanf("%d%s", &m, s);
for(int i = 0; s[i]; i++) add(s[i] - 'A');
}
void decode(int mask)
{
int len = strlen(s);
for(int i = 0; i < len; i++) {
mask = (mask * 131 + i) % len;
swap(s[i], s[mask]);
}
}
void query()
{
int ans = 0, flag = 1;
node *p = root;
for(int i = 0; s[i]; i++) {
int c = s[i] - 'A';
if(p->next[c]) p = p->next[c];
else {
flag = 0;
break;
}
}
if(flag) ans = p->cnt;
mask ^= ans;
printf("%d\n", ans);
}
void work()
{
mask = 0;
while(m--) {
scanf("%s", op);
scanf("%s", s);
decode(mask);
if(op[0] == 'A') for(int i = 0; s[i]; i++) add(s[i] - 'A');
else query();
}
}
int main()
{
init();
read();
work();
return 0;
}