【CQOI2014】通配符匹配

时间:2021-11-08 06:13:56

题面

https://www.luogu.org/problem/P3167

题解

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#define N 100500
#define ri register int
#define uLL unsigned long long

const uLL p=107;
using namespace std;
uLL pp[N];
struct hash {
  uLL sum[N];
  inline void getsum(char *s) {
    sum[0]=0;
    int l=strlen(s+1);
    for (ri i=1;i<=l;i++) sum[i]=sum[i-1]*p+(s[i]-'a');
  }
  inline uLL getval(int l,int r) {
    //if (l>r) return 0;
    return sum[r]-sum[l-1]*pp[r-l+1];
  }
} H1,H2;

bool f[15][N];
char s1[N],s2[N];
int L[15],loc[15];

int main(){
  pp[0]=1;
  for (ri i=1;i<N;i++) pp[i]=pp[i-1]*p;
  scanf("%s",s1+1);
  int l=strlen(s1+1);
  s1[l+2]=s1[l+1]; s1[l+1]='?';
  int cnt=0,pre=0;
  loc[0]=0;
  for (ri i=1;i<=l+1;i++) if (s1[i]=='?'||s1[i]=='*') {
    loc[++cnt]=i;
    L[cnt]=i-pre-1;
    pre=i;
  }
  H1.getsum(s1);
  int n;
  scanf("%d",&n);
  while (n--) {
    scanf("%s",s2+1);
    int l=strlen(s2+1);
    s2[l+2]=s2[l+1]; s2[l+1]='#';
    H2.getsum(s2);
    memset(f,0,sizeof(f));
    f[0][0]=1;
    for (ri i=1;i<=cnt;i++) 
      for (ri j=0;j<=l+1;j++) {
        if (s1[loc[i]]=='*') {
          if (H1.getval(loc[i-1]+1,loc[i]-1)==H2.getval(j-L[i]+1,j)&&f[i-1][j-L[i]]) f[i][j]=1;
          if (H1.getval(loc[i-1]+1,loc[i]-1)==H2.getval(j-L[i],j-1)&&f[i-1][j-L[i]-1]) f[i][j]=1;
          f[i][j]|=f[i][j-1];
        }
        else {
          if (H1.getval(loc[i-1]+1,loc[i]-1)==H2.getval(j-L[i],j-1)&&f[i-1][j-L[i]-1]) f[i][j]=1;
        }
      }
    if (f[cnt][l+1]==1) puts("YES"); else puts("NO");
  }
}