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.
Example
Input
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
由于只是判断t是否为s的子序列,故贪心的用尽可能s串靠前的字符取匹配t,以Nex[i][j]表示s串的第i个字符后面第j个字母第一次出现的位置,在匹配过程中记录tt串的每个字符在s串中的对应匹配位置,每次加入新字符j时,考虑之前tt串的最后一个字符在s串的匹配位置i,如果nex[i][j]有合法值那么说明t依旧是s的子序列,否则不是
代码:
//Full of love and hope for life
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f3f3f3f3f
//https://paste.ubuntu.com/
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int a,b,nex[maxn][26],m[26],n[maxn];
char s[maxn],ss[10];
int main(){
scanf("%s",s+1);
a=strlen(s+1);
memset(nex,-1,sizeof(nex));
memset(m,-1,sizeof(m));
for(int i=a;i>=0;i--){
for(int j=0;j<26;j++){
nex[i][j]=m[j];//更新
}
m[s[i]-'a']=i;
}
cin >> b;
int t=0,len=0;
n[0]=0;
while(b--){
cin >> ss;
if(ss[1]=='u'){//push
cin >> ss;
if(n[t]!=-1&&nex[n[t]][ss[0]-'a']!=-1){
n[t+1]=nex[n[t]][ss[0]-'a'];
t++;
len++;
}
else{//这个地方未出现过
n[++t]=-1;
}
}
else{//pop
t--;
len=min(len,t);
}
if(len==t){//是s的子串
cout << "YES" << endl;
}
else{
cout << "NO" << endl;
}
}
return 0;
}