L - Queries on a String Gym - 101755L(贪心)

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

题意:输入一个主字符串,在生成一个新的字符串,push是在末尾读入一个字符,pop是从位删除一个字符,如果新的字符串是主字符串的子序列那么就输出yes,否者输出no。

用next[i][j]表示s串的第i个字符后边第j个字符首先出现的位置,在操作时
记录每个字符在s串中的位置,加入新的字符时,考虑之前t串的最后一个字符
在s串的匹配位置,如果合法就是子序列,否则不是。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 2e5+10;
char s[maxn];
int Next[maxn][26];
int pos[26];
int ans[maxn];
int main()
{
    scanf("%s",s+1);
    int n;
    int k = strlen(s+1);
    memset(Next,-1,sizeof(Next));
    memset(pos,-1,sizeof(pos));
    for(int i=k; i>=0; i--)
    {
        for(int j=0; j<26; j++) 
        {
            Next[i][j] = pos[j]; //s串的第i个字符后面每个个字母第一次出现的位置
        }
        pos[s[i]-'a'] = i;
    }
    scanf("%d",&n);
    int m = 0; //第二个字符串长度
    ans[0] = 0;  //第二个串的最后一位在s串中的位置
    char op[10];
    while(n--)
    {
        scanf("%s",op);
        if(strcmp(op,"push")==0)
        {
            scanf("%s",op);
            if(ans[m]!=-1&&Next[ans[m]][op[0]-'a']!=-1)
            {
                ans[m+1]=Next[ans[m]][op[0]-'a'];
                m++;
                printf("YES\n");
            }
            else{
                ans[++m] = -1;
                printf("NO\n");
            }
        }
        else
        {
            m--;
            if(ans[m]==-1)
                printf("NO\n");
            else
                printf("YES\n");

        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值