Queries on a String

本文介绍了一种高效的算法,用于解决一系列操作后判断修改后的字符串是否为原字符串的子序列问题。通过预先处理原字符串并使用特定的数据结构,能够在每次操作后快速验证子序列关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值