题目链接.
dp[i][j]=1代表前i个通配符能构造出前j个字符。
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
using namespace std;
char s[100010];
char t[100010];
ull f1[100010],p1[100010];
ull f2[100010],p2[100010];
int pos[15],cnt=0;
int dp[15][100010];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>s+1;
int n=strlen(s+1);
s[++n]='?';p1[0]=1;
for(int i=1;i<=n;i++)
{
f1[i]=f1[i-1]*131+s[i];
p1[i]=p1[i-1]*131;
if(s[i]=='?'||s[i]=='*')pos[++cnt]=i;
}
int tt;cin>>tt;
while(tt--)
{
cin>>t+1;
int m=strlen(t+1);
t[++m]='?';
p2[0]=1;
for(int i=1;i<=m;i++)
{
f2[i]=f2[i-1]*131+t[i];
p2[i]=p2[i-1]*131;
}
memset(dp,0,sizeof dp);
dp[0][0]=1;
for(int i=0;i<=cnt;i++)
{
if(s[pos[i]]=='*')
{
for(int j=1;j<=m;j++)dp[i][j]|=dp[i][j-1];
}
for(int j=0;j<=m;j++)
{
if(!dp[i][j])continue;
int l1=j+1;
int r1=j+pos[i+1]-pos[i]-1;
int l2=pos[i]+1;
int r2=pos[i+1]-1;
if(f2[r1]-f2[l1-1]*p2[r1-l1+1]==f1[r2]-f1[l2-1]*p1[r2-l2+1])
{
if(s[pos[i+1]]=='?')dp[i+1][r1+1]=1;
else dp[i+1][r1]=1;
}
}
}
if(dp[cnt][m])cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}