[Trie]BZOJ 4896——[Thu Summer Camp2016]补退选

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值