本文版權歸ljh2000和博客園共有,歡迎轉載,但須保留此聲明,並給出原文連接,謝謝合做。php
本文做者:ljh2000
做者博客:http://www.cnblogs.com/ljh2000-jump/
轉載請註明出處,侵權必究,保留最終解釋權!ios
題目連接:BZOJ4896spa
正解:$Trie+vector$blog
解題報告:字符串
$THUSC2016$ $T2$get
考慮每次加入刪除對出現次數的影響只會加減一,那麼我能夠先用$Trie$來維護整個問題中出現的字符串,而後對於每一個節點我都記錄一下以當前串爲前綴的字符串個數,同時開個$vector$維護出現次數爲$x$時的最先時刻。博客
由於每次修改只會加減一,那麼空間與字符串長度同級,這個用$vector$實現很方便。string
//It is made by ljh2000 //有志者,事竟成,破釜沉舟,百二秦關終屬楚;苦心人,天不負,臥薪嚐膽,三千越甲可吞吳。 #include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <cstdio> #include <string> #include <queue> #include <cmath> #include <ctime> using namespace std; typedef long long LL; const int MAXN = 100011; int n,ch[MAXN*60][10],c[MAXN*60],len,S,cnt,ans; char s[MAXN]; vector<int>w[MAXN*60]; inline int getint(){ int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w; } inline void work(){ n=getint(); S=cnt=1; int type,u,now; LL x,A,B,C; for(int o=1;o<=n;o++) { type=getint(); u=S; scanf("%s",s); len=strlen(s); if(type==1) { for(int i=0;i<len;i++) { now=s[i]-'a'; if(!ch[u][now]) ch[u][now]=++cnt; u=ch[u][now]; c[u]++; if(c[u]>(int)w[u].size()) w[u].push_back(o); } } else if(type==2) { for(int i=0;i<len;i++) { now=s[i]-'a'; u=ch[u][now]; c[u]--; } } else { u=S; scanf("%lld%lld%lld",&A,&B,&C); ans=abs(ans);//!!! x=1LL*ans*A%C; x+=B; x%=C; ans=0; for(int i=0;i<len;i++) { now=s[i]-'a'; if(!ch[u][now]) { ans=-1; break; } u=ch[u][now]; } if(ans!=-1) { if((int)w[u].size()<=x) ans=-1; else ans=w[u][x]; } printf("%d\n",ans); } } } int main() { #ifndef ONLINE_JUDGE freopen("selection.in","r",stdin); freopen("selection.out","w",stdout); #endif work(); return 0; } //有志者,事竟成,破釜沉舟,百二秦關終屬楚;苦心人,天不負,臥薪嚐膽,三千越甲可吞吳。