Queries on a String

这是一个编程问题,要求处理两个字符串s和p。s是给定的主字符串,p初始为空。你需要对p执行添加和移除字符的操作,并在每次操作后判断s是否包含p作为子序列。输入包括s、操作数量q以及一系列push和pop操作。输出是对每次操作的结果(YES或NO)。解决方案涉及使用next数组来跟踪s中字符的出现位置。
摘要由CSDN通过智能技术生成

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.

Sample 1

InputcopyOutputcopy
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
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<bits/stdc++.h>
using namespace std;
const int N=2e6;
int ne[N][26];
int ans[N];//p串的最后一个字母再s中的位置
int pos[26];
string p;
char s[N];
char c;
int n,m,len;
int main()
{
   scanf("%s",s+1);
   cin>>n;
   memset(ne,-1,sizeof(ne));
   memset(pos,-1,sizeof(pos));
   len=strlen(s+1);
   for(int i=len;i>=0;i--)//用来记录s串里第i个字符后面的26个英文字母的全部第一次出现的位置,如果没有用pos 
    标记为-1
   {
    for(int j=0;j<26;j++)
    {
        ne[i][j]=pos[j];
    }
    pos[s[i]-'a']=i;
   }
    while(n--)
   {
    cin>>p;
    if(p=="push")
    {
       cin>>c;
       if(ans[m]!=-1&&ne[ans[m]][c-'a']!=-1)//ne[ans[m]][c-'a']就是看在s串第ans[m]个字符后 
                                              面有没有c
       {
         ans[m+1]=ne[ans[m]][c-'a'];有的话跟新ans
         m++;
         cout<<"YES"<<endl;
       } 
       else{
        ans[++m]=-1;没有的话也跟新一下ans-1
        cout<<"NO"<<endl;
       }
    }
    else{//pop操作
        m--;
        if(ans[m]==-1)
        cout<<"NO"<<endl;
        else
        cout<<"YES"<<endl;
    }
   }
    return 0;
}

​
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值