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 p as 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.
Sample 1
Inputcopy | Outputcopy |
---|---|
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 | 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 |
题意:输入一个主字符串,在生成一个新的字符串,push是在末尾读入一个字符,pop是从位删除一个字符,如果新的字符串是主字符串的子序列那么就输出yes,否者输出no。
用next[i][j]表示s串的第i个字符后边第j个字符首先出现的位置,在操作时
记录每个字符在s串中的位置,加入新的字符时,考虑之前t串的最后一个字符
在s串的匹配位置,如果合法就是子序列,否则不是。
#include<bits/stdc++.h>
using namespace std;
const int N=2e6;
int ne[N][26];
int ans[N];//p串的最后一个字母再s中的位置
int pos[26];
string p;
char s[N];
char c;
int n,m,len;
int main()
{
scanf("%s",s+1);
cin>>n;
memset(ne,-1,sizeof(ne));
memset(pos,-1,sizeof(pos));
len=strlen(s+1);
for(int i=len;i>=0;i--)//用来记录s串里第i个字符后面的26个英文字母的全部第一次出现的位置,如果没有用pos
标记为-1
{
for(int j=0;j<26;j++)
{
ne[i][j]=pos[j];
}
pos[s[i]-'a']=i;
}
while(n--)
{
cin>>p;
if(p=="push")
{
cin>>c;
if(ans[m]!=-1&&ne[ans[m]][c-'a']!=-1)//ne[ans[m]][c-'a']就是看在s串第ans[m]个字符后
面有没有c
{
ans[m+1]=ne[ans[m]][c-'a'];有的话跟新ans
m++;
cout<<"YES"<<endl;
}
else{
ans[++m]=-1;没有的话也跟新一下ans-1
cout<<"NO"<<endl;
}
}
else{//pop操作
m--;
if(ans[m]==-1)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
}
return 0;
}