4896: [Thu Summer Camp2016]补退选
题目大意
给定n个操作。
如果k=1,加入一个字符串S。
如果k=2,删除一个字符串S。
如果k=3,查找最早什么时候以字符串S为前缀的字符串数超过
(a∗|ANS|+b)
,
ANS
是上一次查找的答案。
解题思路
不难想到维护一棵Trie树,但是对于每个节点不仅要维护出现次数,还要开一个数组记录每个出现次数最早达到的时间,马上就想到用vector,于是这题就不难处理了。
#include<cstdio>
#include<vector>
#define LL long long
using namespace std;
const int maxl=65;
int n,ans,len;
char s[maxl];
struct jz{
jz* son[26];int w;vector<int>que;
jz(jz* p=NULL){w=0;que.clear();for (int i=0;i<26;i++) son[i]=p;}
}nil,*null=&nil,*ro=null;
inline int _read(){
int num=0;char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch>='0'&&ch<='9') num=num*10+ch-48,ch=getchar();
return num;
}
int abs(int x){if (x<0) return -x;else return x;}
void Insert(jz* &x,char* now,int k){
if (x==null) x=new jz(null);
x->w++;int til=x->que.size();
if (til==0||til<x->w) x->que.push_back(k);
if (*now=='?') return;Insert(x->son[*now-'a'],now+1,k);
};
void Delete(jz* x,char* now){x->w--;if (*now=='?') return;Delete(x->son[*now-'a'],now+1);}
int query(jz* x,char* now,int k){
if (x==null) return -1;
if (*now=='?'){int til=x->que.size();if (til<=k) return -1;else return x->que[k];}
return query(x->son[*now-'a'],now+1,k);
}
int main(){
freopen("exam.in","r",stdin);
freopen("exam.out","w",stdout);
n=_read();
for (int i=1;i<=n;i++){
int x=_read(),a,b,c;
char ch=getchar();len=-1;
while (ch<'a'||ch>'z') ch=getchar();
while (ch>='a'&&ch<='z') s[++len]=ch,ch=getchar();s[len+1]='?';
if (x==1) Insert(ro,s,i);
if (x==2) Delete(ro,s);
if (x==3) {
a=_read(),b=_read(),c=_read();
printf("%d\n",ans=query(ro,s,((LL)a*abs(ans)+b)%c));
}
}
return 0;
}