L - Queries on a String Gym - 101755L (字符串的查询)

本文介绍了一种高效的算法,用于实时判断一系列操作后,给定的字符串是否为另一字符串的子序列。通过预处理和贪心策略,算法能在每次操作后迅速响应,适用于大量查询场景。

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

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


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值