根据题意模拟匹配就好
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
#include <cmath>
#include <vector>
#include <utility>
#include <set>
#include <climits>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define INF 2147483647
using namespace std;
typedef long long ll;
string good,base;
int n,i,j,k;
int main()
{
cin>>good>>base;
scanf("%d",&n);
for(k=0; k<n; k++)
{
string s;
cin>>s;
if(s.size()+1<base.size())//字符串比base小一个字符以上直接continue
{
printf("NO\n");
continue;
}
if(s.size()<base.size())//当字符串小于base
{
bool ok=false;
for(i=0,j=0; i<base.size(); i++,j++)
{
if(base[i]!='?'&&base[i]!='*')
{
if(base[i]!=s[j])
break;
}
else if(base[i]=='?')
{
std::size_t found=good.find(s[j]);
if (found==std::string::npos)
{
break;
}
}
else if(base[i]=='*')
{
j--;
}
}
//cout<<i<<" "<<j<<endl;
if(i>=base.size()&&j>=s.size())
printf("YES\n");
else
printf("NO\n");
}
else if(s.size()==base.size())//当两字符串长度相等
{
for(i=0,j=0; i<base.size()&&j<s.size(); i++,j++)
{
if(base[i]!='?'&&base[i]!='*')
{
if(base[i]!=s[j])
break;
}
else if(base[i]=='?')
{
std::size_t found=good.find(s[j]);
if (found==std::string::npos)
{
break;
}
}
else
{
std::size_t found=good.find(s[j]);
if (found!=std::string::npos)
{
break;
}
}
}
if(i>=base.size()&&j>=s.size())
printf("YES\n");
else
printf("NO\n");
}
else//当字符串大于base
{
int num=s.size()-base.size();
for(i=0,j=0; i<base.size()&&j<s.size(); i++,j++)
{
if(base[i]!='?'&&base[i]!='*')
{
if(base[i]!=s[j])
break;
}
else if(base[i]=='?')
{
std::size_t found=good.find(s[j]);
if (found==std::string::npos)
{
break;
}
}
else
{
while(1)
{
if(j>=s.size()||j-i>num)
break;
std::size_t found=good.find(s[j]);
if (found!=std::string::npos)
{
break;
}
j++;
}
j--;
}
}
//cout<<i<<" "<<j<<endl;
if(j>=s.size()&&i>=base.size())
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}