QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#397167 | #4394. Keyboard Warrior | Diu | AC ✓ | 253ms | 52268kb | C++14 | 1.2kb | 2024-04-23 18:48:39 | 2024-04-23 18:48:39 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int T,n,m;
char s[N],ch;
int fail[N][26],to[N][32],nxt[N];
struct upt{
int c,len,p;
}st[N];int tp;
int jump(int p,int c,int len){
if(!len)return p;
--len,p=fail[p][c];
for(int i=0;len>>i;i++)if(len>>i&1)p=to[p][i];
return p;
}
signed main(){
scanf("%d",&T);
for(;T--;){
scanf("%d%d\n%s",&n,&m,s+1);
for(int i=2,j=0;i<=n;i++){
while(j&&s[j+1]!=s[i])j=nxt[j];
if(s[j+1]==s[i])++j;
nxt[i]=j;
}
for(int i=0;i<n;i++){
for(int c=0;c<26;c++){
if((char)(c+'a')==s[i+1])fail[i][c]=i+1;
else fail[i][c]=fail[nxt[i]][c];
}
}
for(int c=0;c<26;c++)fail[n][c]=n;
for(int i=1;i<=n;i++)to[i][0]=fail[i][s[i]-'a'];
for(int k=1;k<=30;k++)
for(int i=1;i<=n;i++)to[i][k]=to[to[i][k-1]][k-1];
tp=0;
bool flg=0;
for(int len;m--;){
scanf("\n%c%d",&ch,&len);
if(ch=='-'){
while(tp){
if(st[tp].len<=len)len-=st[tp].len,--tp;
else break;
}
if(!tp||!len)continue;
len=st[tp].len-len;
int x=st[tp-1].p;
st[tp].len=len;
st[tp].p=jump(x,st[tp].c,len);
}else{
int x=jump(st[tp].p,ch-'a',len);
st[++tp]={ch-'a',len,x};
flg|=x==n;
}
}
puts(flg?"yes":"no");
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 253ms
memory: 52268kb
input:
9 6 6 iloveu i 1 l 1 o 1 v 1 e 1 u 0 6 10 imfive u 10 - 20 i 1 m 1 f 1 i 1 v 5 - 4 e 2 - 2 4 4 abab a 2 b 2 - 3 b 1 2 10 dz a 1000000000 b 1000000000 - 1000000000 d 1000000000 - 1000000000 d 1000000000 z 1000000000 c 1000000000 e 1000000000 - 111111111 5 7 abcab a 1 b 1 a 1 b 1 c 1 a 1 b 1 200000 20...
output:
no yes no yes yes yes yes no yes
result:
ok 9 lines