L. Queries on a String

L. Queries on a String
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
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 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
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
Copy
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。
很显然这个题每一次读入或者删除一个字符都重新判断一下是不是子序列肯定会超时,需要每读入一个字符或者删除一个就能直接判断是否为子序列才行,这时我用了一个二维数组dd[200100][27],来保存 当前这个位置的后面的  离当前位置最近的 每一个字母的位置 就相当于是一个next数组。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <ctime>
#include <stack>
using namespace std;
typedef long long ll;
char str[200010];
int dd[200010][27],before[200010];
int main()
{
    int T,len;
    scanf("%s",str+1);
    len=strlen(str+1);
    for(int i=len-1; i>=0; i--)
    {
        for(int j=0; j<26; j++)
        {
            dd[i][j]=dd[i+1][j];
        }
        dd[i][str[i+1]-'a']=i+1;
    }
//    for(int i=1;i<=len;i++)
//        printf("%d ",dd[i][1]);
    int xx=0,wa=0;
    before[0]=0;
    char asd[10];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",asd);
        if(asd[1]=='u')
        {
            char c[5],cc;
            scanf("%s",c);
            cc=c[0];
            if(wa)
            {
                wa++;
                printf("NO\n");
            }
            else
            {
                if(dd[xx][cc-'a'])///它的下一个有cc
                {
                    before[dd[xx][cc-'a']]=xx;
                    xx=dd[xx][cc-'a'];
                    printf("YES\n");
                }
                else
                {
                    wa++;
                    printf("NO\n");
                }
            }
        }
        else
        {
            if(wa==1)///不用更新
            {
                wa--;
                printf("YES\n");
            }
            else if(wa>1)
            {
                wa--;
                printf("NO\n");
            }
            else if(wa==0)
            {
                xx=before[xx];///更新
                printf("YES\n");
            }
        }
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值