题目大意
给出 次询问,每次询问只有三种可能:往集合里插一个字符串,删一个字符串和进行多模匹配(集合里的)。强制在线。
题目思路
如果没有强制在线还可以直接上 CF163E 的套路,预先看看所有的末尾节点会影响到那些(对所有字符串),不幸的是由于强制在线这不可行……
不过,由于每个字符串只能被插入和删除一次,因此这里采用把插入和删除分别进行搜寻(对,分别建 AC 自动机),把插入的出现次数减去删除的出现次数就好了。
于是我们想着对 AC 自动机搞点事情。由于二进制分组是一种广为人知的套路因此我直接按字面了解,认为是按照字符串长度进行二进制分组进而造就了以下的代码:
#include <iostream>
#include <queue>
#include <string>
#include <cmath>
using namespace std;
struct ACAM{
const static int SIGMA=26;
vector<vector<int>> trans,bktr;
vector<int> fail,ed;
bool changed,got;
int tot,root=0,tans=0;
ACAM(int ze){
fail=vector<int>((ze+1)*SIGMA,0),ed=vector<int>((ze+1)*SIGMA);
//fail.resize(ze+1),ed.resize(ze+1);
bktr=trans=vector<vector<int>>((ze+1)*SIGMA,vector<int>(SIGMA,0));
// trans.resize(ze+1,vector<int>(SIGMA));
root=tot=0,changed=got=false;
}
void insert(string st){
if(!changed) trans=bktr;
int co=root;
for(auto ch:st){
if(trans[co][ch-'a']==0) trans[co][ch-'a']=(++tot);
co=trans[co][ch-'a'];
}
ed[co]++,changed=true,got=false;
}
void build(){
if(!changed) return;
changed=false,got=false;
bktr=trans,fail=vector<int>(fail.size(),0);
queue<int> que;
// for(int i=0;i<SIGMA;i++) trans[0][i]=1;
// que.push(1),fail[1]=0;
for(int i=0;i<SIGMA;i++) if(trans[root][i]!=0) que.push(trans[root][i]);
while(!que.empty()){
int co=que.front();que.pop();
for(int i=0;i<SIGMA;i++){
if(trans[co][i]) fail[trans[co][i]]=trans[fail[co]][i],que.push(trans[co][i]);
else trans[co][i]=trans[fail[co]][i];
}
}
}
int query(string st){
if(got) return tans;
got=true;
int co=root,ans=0;
for(auto ch:st){
co=trans[co][ch-'a'];
for(int nw=co;nw!=0;nw=fail[nw]) ans+=ed[nw];
}
return tans=ans;
}
}inserted[13]={ACAM(2),ACAM(4),ACAM(8),ACAM(16),ACAM(32),ACAM(64),ACAM(128),ACAM(256),ACAM(512),ACAM(1024),ACAM(2048),ACAM(4096),ACAM(8192)},
deleted[13]={ACAM(2),ACAM(4),ACAM(8),ACAM(16),ACAM(32),ACAM(64),ACAM(128),ACAM(256),ACAM(512),ACAM(1024),ACAM(2048),ACAM(4096),ACAM(8192)};
int main(){
int m,t;
string st;
cin>>m;
while(m--){
cin>>t>>st;
if(t==1||t==2){
int lon=log2(st.length());
if(t==1) inserted[lon].insert(st);
else deleted[lon].insert(st);
}else{
int ins=0,del=0;
for(int i=0;i<13;i++){
inserted[i].build();
deleted[i].build();
ins+=inserted[i].query(st);
del+=deleted[i].query(st);
}
cout<<ins-del<<endl<<flush;
}
}
return 0;
}好吧,它会在第五个点炸掉,因此我重新学了下,原来是按照大小合并。
我们可以在插入时合并相同大小的 AC 自动机,而后把合并出来的最小的 AC 自动机建边(因为前面的最小的已经建完了边)。
代码如下。
#include <iostream>
#include <queue>
#include <string>
#include <cmath>
using namespace std;
const int SIZE=6e5+10;
struct ACAM{
const static int SIGMA=26;
int cnt,rts,trans[SIZE][SIGMA],tarns[SIZE][SIGMA];
int root[SIZE],fail[SIZE],val[SIZE],siz[SIZE];
long long tmp[SIZE];
void insert(string s,int &root){
if(root==0) root=++cnt;
int co=root;
for(auto ch:s){
if(trans[co][ch-'a']==0) trans[co][ch-'a']=++cnt;
co=trans[co][ch-'a'];
}
val[co]++;
}
void build(int root){
queue<int> que;
fail[root]=root;
for(int i=0;i<SIGMA;i++){
if(trans[root][i]!=0) tarns[root][i]=trans[root][i],fail[trans[root][i]]=root,que.push(trans[root][i]);
else tarns[root][i]=root;
}
while(!que.empty()){
int co=que.front();que.pop();
tmp[co]=val[co]+tmp[fail[co]];
for(int i=0;i<SIGMA;i++)
if(trans[co][i]) tarns[co][i]=trans[co][i],fail[trans[co][i]]=tarns[fail[co]][i],que.push(trans[co][i]);
else tarns[co][i]=tarns[fail[co]][i];
}
}
int merge(int x,int y){
if(x==0||y==0) return x+y;
val[x]+=val[y];
for(int i=0;i<SIGMA;i++) trans[x][i]=merge(trans[x][i],trans[y][i]);
return x;
}
void insert(string st){
siz[++rts]=1;
insert(st,root[rts]);
while(siz[rts-1]==siz[rts]) siz[rts-1]+=siz[rts],root[rts-1]=merge(root[rts-1],root[rts]),root[rts]=0,rts--;
build(root[rts]);
}
long long query(string st){
long long ans=0;
for(int i=1;i<=rts;i++){
int co=root[i];
for(auto ch:st){
co=tarns[co][ch-'a'];
ans+=tmp[co];
}
}
return ans;
}
}in,de;
int main(){
int m,t;
string st;
cin>>m;
while(m--){
cin>>t>>st;
if(t==1) in.insert(st);
if(t==2) de.insert(st);
if(t==3) cout<<in.query(st)-de.query(st)<<endl<<flush;
}
return 0;
}EOF