2019 ccpc南昌邀请赛网络赛 M. Subsequence (字符串)

题目链接

题意:判断t串是不是s串的子串

思路:预处理记录s串中每个字符后面的26个字符的位置

要用scanf,用cin疯狂超时

#include<bits/stdc++.h>
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
#define fi first
#define se second

const int maxn=1e5+10;
int a[maxn][30];
char s[maxn],s1[maxn];

void init(){//预处理
    memset(a,0,sizeof(a));
    int l=strlen(s);
    for(int i=l-1;i>=0;i--){
        for(int j=0;j<26;j++){
            a[i][j]=a[i+1][j];
        }
        a[i][s[i]-'a']=i+1;
    }
}
int main()
{
   // fastio
    scanf("%s",s);
    init();
    int m;
    scanf("%d",&m);
    while(m--){
        scanf("%s",s1);
        int l=strlen(s1);
        int f=0,j=0;
        for(int i=0;i<l;i++){
            if(a[j][s1[i]-'a']) j=a[j][s1[i]-'a'];
            else{
                f=1;
                break;
            }
        }
        if(f) printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42754600/article/details/89449466