The Preliminary Contest for ICPC China Nanchang National Invitational

M题:Subsequence

题目链接:

https://nanti.jisuanke.com/t/38232

题目大意:

给你一串字符串,然后n个询问,问输入的字符串是不是这串字符串的字串(可以不相邻,但字母的顺序不能变)

题目大意:

预处理每个在字符串长度的下一个字符的位置,然后,一步步往后推即可。
如 abcdef
a 1 7 7 7 7 7
b 2 2 7 7 7 7
c 3 3 3 7 7 7
d 4 4 4 4 7 7
e 5 5 5 5 5 7
f 6 6 6 6 6 6
a(1)→b(2)→c(3)→d(4)→e(5)→f(6)

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
typedef long long ll;
const int N=100010;
const int INF=0x3f3f3f3f;
char str[N],s[N];
int cnt1[300],cnt2[300];
int nxt[N][30];
int pt[30];

int getstr(char c){
    return c-'a'+1;
}

int main(){
    scanf("%s",str);
    int len=strlen(str);
    for(int i=0;i<30;i++) pt[i]=len+1; //初始化
    //for(int i=0;i<30;i++) cout<<pt[i]<<" ";
    for(int i=0;i<30;i++) nxt[len+1][i]=len+1;//初始化
    for(int i=len-1;i>=0;i--){
        for(int j=0;j<30;j++)
            nxt[i][j]=pt[j];  //记录第i的上一个j在哪
        pt[getstr(str[i])]=i; //记录上一个srtp[i]在哪
    } 
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%s",s);
        int l=strlen(s);
        int flag=1;
        int id=pt[getstr(s[0])];

        for(int i=1;i<l;i++){
            int d=nxt[id][getstr(s[i])];
            if(d>id){
                id=d;
            }
            else{
                flag=0;
                break;
            }
        }
        if(flag&&id<=len) puts("YES");
        else puts("NO");
    }
}

大佬题解链接:
https://www.cnblogs.com/widsom/p/10742707.html?tdsourcetag=s_pctim_aiomsg

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值