Problem
给一个含有 N 个单词的词典,每个单词是 Si 。进行 M 次操作,操作分三种:一,删除词典里第 k 个单词,若已被删除则跳过;二,恢复词典里第 k 个单词,若已存在则跳过;三,给一篇文章 Article ,问当前词典里的单词在 Article 中出现的次数之和。
Limits
TimeLimit(ms):3000
MemoryLimit(MB):256
N,M∈[1,105]
k∈[1,N]
|Article|,|S1+S2+…+SN|∈[1,106]
字符集∈[a,z]
Look up Original Problem From here
Solution
AC自动机+有向树+树状数组优化
Complexity
TimeComplexity:O(|S|×log)
MemoryComplexity:O(|S|×26)
My Code
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-6
#define MOD 1000000007
#define MAXN 1001000
#define N 100100
#define M 26
ll ans;
int n,k,lens;
char s[MAXN];
bool here[N];
class Directed_Tree{
public:
vector<int>vto[MAXN];
inline void plant(int from,int to){
vto[from].pb(to);
}
int num,mapto[MAXN],upto[MAXN],depth[MAXN];
inline void dfs(int now,int kth){
num+=1;
mapto[now]=num;
depth[num]=kth;
int len=gsize(vto[now]);
rep(i,0,len){
dfs(vto[now][i],kth+1);
}
upto[now]=num;
}
}dirtree;
class Bit{
public:
ll c1[MAXN],c2[MAXN],sum[MAXN];
int limit;
inline int lowbit(int x){
return x&-x;
}
inline void build_sum(int *a,int n){
repin(i,1,n){
sum[i]=sum[i-1]+a[i];
}
}
inline void add(int x,ll val,ll *c){
for(int i=x;i<=limit;i+=lowbit(i)){
c[i]+=val;
}
}
inline ll ask(int x,ll *c){
ll res=0;
for(int i=x;i>0;i-=lowbit(i)){
res+=c[i];
}
return res;
}
inline void update(int l,int r,ll val){
add(l,val,c1);
add(r+1,-val,c1);
add(l,l*val,c2);
add(r+1,-(r+1)*val,c2);
}
inline ll query(int l,int r){
ll res=sum[r]+(r+1)*ask(r,c1)-ask(r,c2);
res-=sum[l-1]+l*ask(l-1,c1)-ask(l-1,c2);
return res;
}
}bit;
class Trie{
public:
int num_node,trie[MAXN][M],val[MAXN],reval[N];
inline int idx(char c){
return c-'a';
}
inline void clear(){
clr(trie[0]);
num_node=1;
}
inline void insert(char *s,int len,int kth){
int now=0,letter;
rep(i,0,len){
letter=idx(s[i]);
if(!trie[now][letter]){
trie[now][letter]=num_node;
val[num_node]=0;
clr(trie[num_node]);
num_node+=1;
}
now=trie[now][letter];
}
val[now]=kth;
reval[kth]=now;
}
int fail[MAXN],last[MAXN];
queue<int>q;
inline void bfs(){
fail[0]=last[0]=0;
int now=0;
rep(i,0,M){
int son=trie[now][i];
if(son){
fail[son]=0;
last[son]=0;
q.push(son);
}
}
int son,f;
while(!q.empty()){
now=q.front();
q.pop();
if(val[now]) dirtree.plant(last[now],now);
rep(i,0,M){
son=trie[now][i];
f=fail[now];
if(!son){
trie[now][i]=trie[f][i];
continue;
}
while(f && !trie[f][i]) f=fail[f];
fail[son]=trie[f][i];
last[son]=val[fail[son]]?fail[son]:last[fail[son]];
q.push(son);
}
}
}
inline void update_ans(int now){
ans+=bit.query(dirtree.mapto[now],dirtree.mapto[now]);
}
inline void search(char *s,int len){
int now=0,letter;
ans=0;
rep(i,0,len){
letter=idx(s[i]);
now=trie[now][letter];
if(val[now]) update_ans(now);
else if(last[now]) update_ans(last[now]);
}
}
}trie;
int main(){
scanf("%d %d",&n,&k);
trie.clear();
repin(i,1,k){
scanf("%s",s);
lens=len(s);
trie.insert(s,lens,i);
here[i]=true;
}
trie.bfs();
dirtree.dfs(0,0);
bit.limit=dirtree.num;
bit.build_sum(dirtree.depth,dirtree.num);
repin(Case,1,n){
char type;
scanf(" %c",&type);
if(type=='-'){
int x;
scanf("%d",&x);
if(!here[x]) continue;
here[x]=false;
int node=trie.reval[x];
bit.update(dirtree.mapto[node],dirtree.upto[node],-1);
}
else if(type=='+'){
int x;
scanf("%d",&x);
if(here[x]) continue;
here[x]=true;
int node=trie.reval[x];
bit.update(dirtree.mapto[node],dirtree.upto[node],+1);
}
else{
scanf("%s",s);
lens=len(s);
trie.search(s,lens);
printf("%lld\n",ans);
}
}
}