先处理出长度的差别,再根据*的位置做判断。因为长度差确定了,*会代替多少个字符也。暴力匹配就行。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long int ll;
string s,s1,s2;
int n;
int flag;
int main(){
cin>>s>>s1>>n;
bool flaggy;
int len1=s1.length();
int len2;
if(s1.find('*')!=-1)
flaggy=true;
else
flaggy=false;
for(int i=0;i<n;i++){
cin>>s2;
len2=s2.length();
int ok=1;
if(flaggy){
if(len2<len1-1){
printf("NO\n");
continue;
}
int bound=len2-len1;
for(int i=0,j=0;j<len2&&ok;i++,j++){
if(s2[j]==s1[i])
continue;
if(s1[i]=='?'&&s.find(s2[j])!=-1)
continue;
else if(s1[i]=='?')
ok=0;
if(s1[i]=='*'){
if(bound==-1)
j--;
else{
for(int k=0;k<=bound&&ok;k++,j++){
if(s.find(s2[j])==-1)
continue;
else
ok=0;
}
j--;
}
}
else
ok=0;
}
if(ok)
printf("YES\n");
else
printf("NO\n");
}
else{
if(len2!=len1){
printf("NO\n");
continue;
}
for(int i=0;i<len1&&ok;i++){
if(s1[i]==s2[i]||(s1[i]=='?'&&s.find(s2[i])!=-1))
continue;
else
ok=0;
}
if(ok)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}