【51Nod 2504】—是子序列的个数(序列自动机)

传送门

模板题

建出序列自动机之后暴力匹配即可

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
    char ch=gc();
    int res=0,f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
#define poly vector<int>
template<class T>inline void chemx(T &a,T b){a<b?a=b:0;}
template<class T>inline void chemn(T &a,T b){a>b?a=b:0;}
cs int N=50005;
int nxt[N][26];
inline void init_nxt(char *s){
	for(int i=strlen(s+1);i;i--){
		int c=s[i]-'a';
		for(int j=0;j<26;j++)
		if(j!=c)nxt[i-1][j]=nxt[i][j];
		nxt[i-1][c]=i;
		
	}	
}
inline int query(char *s){
	int p=0;
	for(int i=1,len=strlen(s+1);i<=len;i++){
		int c=s[i]-'a';
		if(!nxt[p][c])return 0;
		p=nxt[p][c];
	}
	return 1;
}
int n;
char s[N];
int main(){
	scanf("%s",s+1);
	init_nxt(s);
	n=read();int res=0;
	for(int i=1;i<=n;i++){
		scanf("%s",s+1);
		res+=query(s);
	}cout<<res;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值