# [CQOI2014]通配符匹配

Description

Input

Output

Sample Input
*aca?ctc
6
acaacatctc
acatctc
aacacatctc
aggggcaacacctc
aggggcaacatctc
aggggcaacctct

Sample Output
YES
YES
YES
YES
YES
NO

HINT

1 <=n<=100

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
#define for_vec(it,x) for (vector<int>::iterator it=x.begin();it!=x.end();it++)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
}
inline int frd(){
int x=0,f=1;char ch=gc();
for (;ch<'0'||ch>'9';ch=gc())   if (ch=='-')    f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar())  if (ch=='-')    f=-1;
for (;ch>='0'&&ch<='9';ch=getchar())    x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0)    putchar('-'),x=-x;
if (x>9)    print(x/10);
putchar(x%10+'0');
}
const int N=1e5;
int pos[N+10],pos_cnt,lenS;
bool BGN,END;
struct S1{
int trie[N+10][26],fail[N+10];
int tot,root,num,L;
vector<int>End[N+10];
void insert(char *s,int l,int r){
static char T[N+10];
for (int i=l;i<r;i++)   T[i-l]=s[i];
int len=r-l,p=root; T[len++]='?';
for (int i=0;i<len;i++){
while (T[i]=='?'&&i<len){
if (i&&T[i-1]!='?') End[p].push_back(i-1),num++;
p=root,i++;
}
if (i>=len) break;
if (!trie[p][T[i]-'a']) trie[p][T[i]-'a']=++tot;
p=trie[p][T[i]-'a'];
}L=--len;
}
void make_fail(){
static int h[N+10];
for (int i=0;i<26;i++)  if (trie[root][i])  h[++tail]=trie[root][i];
for_vec(it,End[fail[Now]])  End[Now].push_back(*it);
for (int i=0;i<26;i++){
if (trie[Now][i]){
int son=trie[Now][i];
fail[son]=trie[fail[Now]][i];
h[++tail]=son;
}else   trie[Now][i]=trie[fail[Now]][i];
}
}
}
bool check(char *s,bool Fir,bool Lst){
static int cnt[N+10];
memset(cnt,0,sizeof(cnt));
int len=strlen(s),p=root;
for (int i=0;i<len;i++){
p=trie[p][s[i]-'a'];
for_vec(it,End[p])  if (i>=*it) cnt[i-*it]++;
}
for (int i=0;i<len;i++){
if (cnt[i]!=num)    continue;
if (!pos_cnt){
if (!BGN&&Fir&&i)   break;
if (!END&&Lst&&len-i!=L)    break;
pos[++pos_cnt]=i+L;
return 1;
}else{
if (i<pos[pos_cnt]) continue;
if (!END&&Lst&&len-i!=L)    break;
pos[++pos_cnt]=i+L;
return 1;
}
}
return 0;
}
}AC[10];//Aho-Corasick automaton
char s[N+10];
int main(){
scanf("%s",s);
int len=strlen(s),tot=0; lenS=len;
BGN=s[0]=='*',END=s[len-1]=='*',s[len++]='*';
for (int i=0,Last=0;i<len;i++){
if (s[i]=='*'){
if (i>Last) AC[tot++].insert(s,Last,i);
Last=i+1;
}
}
for (int i=0;i<tot;i++) AC[i].make_fail();
if (!len){
printf("NO\n");
continue;
}
if (!tot){
printf("YES\n");
continue;
}
memset(s,0,sizeof(s));
memset(pos,0,sizeof(pos)); pos_cnt=0;
scanf("%s",s);
bool flag=1;
for (int i=0;i<tot;i++){
if (!AC[i].check(s,i==0,i==tot-1)){
flag=0;
break;
}
}
printf(flag?"YES\n":"NO\n");
}
return 0;
}

01-09 413

01-06 184
04-13 4369
01-06 201
03-30 127
04-20 295
11-18 71
06-18 36
11-10 843
02-02 1418
04-02 894
03-01 47
03-18 2028