1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| #include <bits/stdc++.h> using namespace std; #define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) #define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++) #define ff(i,j,k) for(int i=(j),end_i=(k);i< end_i;i++) #define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--) #define DEBUG(x) cerr<<#x<<"="<<x<<endl #define all(x) (x).begin(),(x).end() #define cle(x) memset(x,0,sizeof(x)) #define lowbit(x) ((x)&-(x)) #define VI vector<int> #define ll long long #define ull unsigned ll #define db double #define lb long db #define pb push_back #define mp make_pair #define fi first #define se second template<class T>inline void read(T &x) { x=0; char ch=getchar(); bool f=0; for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1; for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48); if(f) x=-x; } template<class T,class... V> inline void read(T &a,V&... b){read(a); read(b...);} #define CASET int ___;for(read(___);___--;)
const int N=4e5+5; const int M=4e5+5;
#define lc (u<<1) #define rc (u<<1|1) #define ls lc,l,mid #define rs rc,mid+1,r int sum[M<<2]; void add(int u,int l,int r,int p,int x) { sum[u]+=x; if(l==r) return; int mid=(l+r)>>1; if(p<=mid) add(ls,p,x); else add(rs,p,x); } int ask(int u,int l,int r,int L,int R) { if(L>R) return 0; if(L<=l&&r<=R) return sum[u]; int mid=(l+r)>>1,ans=0; if(L<=mid) ans+=ask(ls,L,R); if(mid<R) ans+=ask(rs,L,R); return ans; }
const int S=26; struct query{ int opt; ll x; char c; }; pair<int,int> as[M]; pair<int,int> hap[M]; namespace Trie{ int ne[M][S],siz[M],cnt=1; void ins(const string &s,int x,int len,const vector<int> v) { int u=1; fo(i,1,len) { int c=s[i-1]-'a'; if(!ne[u][c]) ne[u][c]=++cnt; if(i>x) hap[v[i-x-1]]={u,ne[u][c]}; u=ne[u][c]; if(i==x) siz[u]++; } } int dd[S][M],_dd[S][M],dw[S][M],tim,now; void cal(int u) { dd[now][u]=++tim;_dd[now][tim]=u; int v; if(v=ne[u][now]) cal(v),dw[now][u]=dw[now][v]+1; else dw[now][u]=0; fo(i,0,S-1) if(i!=now&&(v=ne[u][i])) cal(v); } struct node{ int pos; ll k; int c; node() {} node(int _p,ll _k,int _c){pos=_p; k=_k; c=_c;} }; struct node2{ ll k; int c; }; node f[M]; node2 st[M]; int top; node get(node x,node2 t) { if(x.k!=0ll) return node(x.pos,x.k+t.k,x.c); ll len=dw[t.c][x.pos]; if(len>=t.k) return node(_dd[t.c][dd[t.c][x.pos]+t.k],0,-1); return node(_dd[t.c][dd[t.c][x.pos]+len],t.k-len,t.c); } void work(const string &t,vector<query> p) { fo(i,0,S-1) now=i,tim=0,cal(1); int m=p.size()-1; int n=t.length(); int c; f[top=0]={1,0,-1}; fo(i,1,n) { c=t[i-1]-'a'; if(!top||st[top].c!=c) st[++top]={1ll,c}; else st[top].k++; f[top]=get(f[top-1],st[top]); } fo(i,1,m) { if(p[i].opt==2) { for(;p[i].x;) { ll d=min(st[top].k,p[i].x); st[top].k-=d; p[i].x-=d; if(!st[top].k) top--; } if(top) f[top]=get(f[top-1],st[top]); } else if(p[i].opt==3) { c=p[i].c-'a'; if(!top||st[top].c!=c) st[++top]={p[i].x,c}; else st[top].k+=p[i].x; f[top]=get(f[top-1],st[top]); } else if(p[i].opt==4) as[i]={f[top].pos,f[top].c}; } } int dfn[M],ppos[M][S]; void dfs(int u,int pre) { dfn[u]=++tim; int v; fo(i,0,S-1) { ppos[u][i]=tim; if(v=ne[u][i]) dfs(v,u); } } } using Trie::ins; using Trie::dfs; using Trie::work; using Trie::dfn; using Trie::ppos; using Trie::cnt; string s[N],t; int n,q;
vector<query> p; int l[N],len[N]; vector<int> v[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>q>>t; p.resize(q+1); fo(i,1,n) { cin>>s[i]; l[i]=len[i]=s[i].length(); } fo(i,1,q) { int opt; ll x; string ch; cin>>opt; if(opt!=4) cin>>x; else x=0; if(opt==1||opt==3) cin>>ch; else ch="-"; p[i]={opt,x,ch[0]}; if(opt==1) { s[x]+=ch; v[x].pb(i); len[x]++; } } fo(i,1,n) ins(s[i],l[i],len[i],v[i]); Trie::work(t,p); Trie::tim=0; dfs(1,0); fo(i,1,cnt) add(1,1,cnt,dfn[i],Trie::siz[i]); fo(i,1,q) { int opt=p[i].opt; if(opt==1) add(1,1,cnt,dfn[hap[i].fi],-1),add(1,1,cnt,dfn[hap[i].se],1); else if(opt==4) printf("%d\n",ask(1,1,cnt,1,as[i].se==-1?dfn[as[i].fi]-1:Trie::ppos[as[i].fi][as[i].se])); } return 0; }
|