[BZOJ3507][CQOI2014]通配符匹配(DP+Hash)

显然f[i][j]表示S匹配到第i个通配符,T匹配到第j个字符,是否可行。

一次一起转移两个通配符之间的所有字符,Hash判断。

稍微有点细节。常数极大卡时过排名倒数,可能是没自然溢出的原因。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
using namespace std; const int N=,p1=,p2=;
bool f[][N];
char s1[N],s2[N];
int n,m,T,tot,pos[],h1[N],h2[N],pw[N]; int main(){
freopen("bzoj3507.in","r",stdin);
freopen("bzoj3507.out","w",stdout);
scanf("%s",s1+); n=strlen(s1+)+; s1[n]='?';
pw[]=; rep(i,,n) pw[i]=1ll*pw[i-]*p1%p2;
rep(i,,n) h1[i]=(1ll*h1[i-]*p1+s1[i])%p2;
rep(i,,n) if (s1[i]=='?' || s1[i]=='*') pos[++tot]=i;
for (scanf("%d",&T); T--; ){
scanf("%s",s2+); m=strlen(s2+)+; s2[m]='#';
rep(i,,tot) rep(j,,m) f[i][j]=; f[][]=;
rep(i,,m) h2[i]=(1ll*h2[i-]*p1+s2[i])%p2;
rep(i,,tot) rep(j,,m){
if (s1[pos[i]]=='*') f[i][j]|=f[i][j-];
if (!f[i][j]) continue;
int l1=pos[i]+,r1=pos[i+]-,l2=j+,r2=j+(pos[i+]-pos[i])-;
if ((h1[r1]-1ll*h1[l1-]*pw[r1-l1+]%p2+p2)%p2==(h2[r2]-1ll*h2[l2-]*pw[r2-l2+]%p2+p2)%p2){
if (s1[pos[i+]]=='?') f[i+][r2+]|=f[i][j]; else f[i+][r2]|=f[i][j];
}
}
puts(f[tot][m] ? "YES" : "NO");
}
return ;
}
上一篇:mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in


下一篇:axios发送两次请求原因及解决方法