loj2291 「THUSC 2016」补退选

ref
pkusc 快到了,做点 thusc 的题涨涨 rp……

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long ll;
int n, k, ch[6000005][10], cnt, len, f[6000005], ans;
char ss[6000005];
vector<int> vec[6000005];
int main(){
    cin>>n;
    for(int j=1; j<=n; j++){
        scanf("%d %s", &k, ss);
        len = strlen(ss);
        if(k==1){
            int u=0;
            for(int i=0; i<len; i++){
                int c=ss[i]-'a';
                if(!ch[u][c])   ch[u][c] = ++cnt;
                u = ch[u][c];
                f[u]++;
                if(f[u]>vec[u].size())  vec[u].push_back(j);
            }
        }
        if(k==2){
            int u=0;
            for(int i=0; i<len; i++){
                int c=ss[i]-'a';
                u = ch[u][c];
                f[u]--;
            }
        }
        if(k==3){
            int a, b, c, u=0;
            if(ans<0)   ans *= -1;
            scanf("%d %d %d", &a, &b, &c);
            int x=((ll)a*ans%c+b)%c;
            ans = 0;
            for(int i=0; i<len; i++){
                c = ss[i] - 'a';
                if(!ch[u][c]){
                    ans = -1;
                    break;
                }
                u = ch[u][c];
            }
            if(ans!=-1 && x<vec[u].size())
                ans = vec[u][x];
            else    ans = -1;
            printf("%d\n", ans);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/poorpool/p/9073020.html