月月查华华的手机 //序列自动机

题目链接

牛客小白月赛12-J

题意

给定一个母串 s s s ,有 m m m 次询问,每次询问给一个字符串 t t t ,问这个字符串是否为母串的子序列。是则输出"Yes",否则输出"No"。

思路

序列自动机入门题.
设母串长为 n n n 、子串长为 m m m ,定义 n e x [ i ] [ j ] nex[i][j] nex[i][j] 表示在母串 s s s i i i 位后面(不包括第 i i i 位)的第一个 j j j 出现的位置,匹配的时候从第零位(虚根)开始,如果能一直匹配下去就输出 Y e s Yes Yes,否则输出 N o No No。预处理时间复杂度为: O ( n ∗ 26 ) O(n*26) O(n26),单次查询时间复杂度为 O ( m ) O(m) O(m)

参考代码

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=1e6+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
#define ft first
#define sd second
#define pb push_back
#define ms(x,y) memset(x,y,sizeof(x))
int m;
char t[maxn],s[maxn];
int nex[maxn][30];
int main(){
    scanf("%s",s+1);
    int n=strlen(s+1);
    scanf("%d",&m);
    for(int j=0;j<26;j++)nex[n][j]=-1;//不存在的赋值为-1
    for(int i=n;i>=1;i--){
        for(int j=0;j<26;j++)nex[i-1][j]=nex[i][j];
        nex[i-1][s[i]-'a']=i;
    }
    while(m--){
        scanf("%s",t+1);
        int len=strlen(t+1);
        bool flag=0;
        int pos=0;//从虚根开始匹配
        for(int i=1;i<=len;i++){
            pos=nex[pos][t[i]-'a'];//当前字符在母串中的位置
            if(pos==-1){
                flag=1;break;
            }
        }
        if(!flag)puts("Yes");
        else puts("No");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值