【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;
}
posted @ 2019-09-27 18:08  Stargazer_cykoi  阅读(108)  评论(0编辑  收藏  举报