output
standard output
A string s is given. Also there is a string p, and initially it is empty. You need to perform q operations of kind «add a letter to the end of the string p» and «remove a letter from the end of the string p», and after performing each operation you must say whether or not s contains pas a subsequence.
Input
The first line contains the string s of length from 1 to 200000, consisting of lowercase Latin letters.
The second line contains a single integer q (1 ≤ q ≤ 200000) — the number of operations.
Each of the next q lines describes an operation in the format «push c», which means «add letter c to the end of the string p» (c is lowercase Latin letter), or «pop», which means «remove letter from the end of the string p». The «pop» operations is guaranteed never to be applied to the empty string p.
Output
Output q lines, each of which equals «YES» or «NO», depending on whether or not the string p is contained in the string s as a subsequence after performing the corresponding operation.
Example
input
Copy
abcabc
30
push a
pop
push a
push a
push a
pop
push c
push b
pop
pop
push b
push c
push c
pop
pop
pop
pop
push b
push c
push c
pop
push b
push c
pop
pop
push a
push b
push c
push a
pop
output
YES
YES
YES
YES
NO
YES
YES
NO
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
先将原字符串的每一个字符用一个二位数组记录下来,记录的原则是nxt[i][j]记录s[i+1]的出现的字符,这样就能不断往下走,判断接下来是否存在所需要的字符,然后用一个一位整形数组代表之后变化的字符串,t[0]=0;(为二维数组遍历的起点),然后就判断是否存在,如果存在t[cnt]就存字符出现的第一个位置x,此时nxt[x]存的是接下来位置的字符情况,如果不存在,那本身也不存在了,所以就t[cnt]=-1;然后cnt++;
最后查是否t[cnt-1]是否不为-1即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 200005;
int n,Q,nxt[N][26],t[N];
char s[N],ope[10],c;
int main()
{
scanf("%s %d",s+1,&Q);
n=strlen(s+1);
memset(nxt[n],-1,sizeof(nxt[n]));
for(int i=n-1;i>=0;i--)
{
for(int j=0;j<26;j++)
{
if(s[i+1]-'a'==j)nxt[i][j]=i+1;
else nxt[i][j] = nxt[i+1][j];
}
}
int len=1;
t[0]=0;
while(Q--)
{
scanf(" %s",ope);
if(ope[2]=='s')
{
scanf(" %c",&c);
if(t[len-1]==-1)t[len]=-1;
else t[len]=nxt[t[len-1]][c-'a'];
len++;
}
else len--;
if(t[len-1]!=-1)puts("YES");
else puts("NO");
}
return 0;
}