# BZOJ3507 [Cqoi2014]通配符匹配 【哈希 + 贪心】

*aca?ctc

6

acaacatctc

acatctc

aacacatctc

aggggcaacacctc

aggggcaacatctc

aggggcaacctct

YES

YES

YES

YES

YES

NO

·字符串长度不超过1 00000

· 1 <=n<=100

·通配符个数不超过10

## 题解

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
#define ULL unsigned long long int
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
ULL P[maxn];
char T[maxn];
int tot,L[maxn],n;
vector<ULL> hash[maxn];
vector<int> len[maxn];
void init(){
P[0] = 1;
for (int i = 1; i <= 100000; i++) P[i] = P[i - 1] * 107;
scanf("%s",T + 1);
n = strlen(T + 1);
if (T[1] == '*') head = true;
if (T[n] == '*') tail = true;
for (int i = 1; i <= n; i++){
if (T[i] == '*') continue;
tot++;
LL h = 0;
for (int j = i; j; j++){
if (j > n || T[j] == '*'){
hash[tot].push_back(h);
len[tot].push_back(j - i);
i = j;
break;
}
if (T[j] == '?'){
hash[tot].push_back(h);
len[tot].push_back(j - i);
h = 0;
i = j + 1;
continue;
}
h = h * 107 + T[j];
}
}
for (int i = 1; i <= tot; i++){
L[i] = len[i].size() - 1;
for (int j = 0; j < len[i].size(); j++){
L[i] += len[i][j];
}
}
/*for (int i = 1; i <= tot; i++,puts(""))
for (int j = 0; j < len[i].size(); j++)
printf("%d ",len[i][j]);*/
}
char s[maxn];
int m;
ULL h[maxn];
bool cmp(int u,int p){
int t = p;
for (int j = 0; j < len[u].size(); j++){
if (t > m) return false;
int r = t + len[u][j] - 1;
if (r > m) return false;
if (hash[u][j] != h[r] - h[t - 1] * P[len[u][j]]) return false;
t = r + 2;
}
return true;
}
void solve(){
while (q--){
scanf("%s",s + 1);
m = strlen(s + 1);
h[0] = 0;
int l = 1,r = m;
for (int i = 1; i <= m; i++) h[i] = h[i - 1] * 107 + s[i];
if (!cmp(1,1)){
puts("NO");
continue;
}
l = L[1] + 1;
}
if (!tail){
if (!cmp(tot,m - L[tot] + 1)){
puts("NO");
continue;
}
r = m - L[tot];
}
//puts("LXT");
/*if (head && !tail && tot == 1){
puts("YES");
continue;
}
if (!head && tail && tot == 1){
puts("YES");
continue;
}*/
int p = l,i,E = tail ? tot : tot - 1;
for (i = head ? 1 : 2; i <= E && p <= r; i++){
if (cmp(i,p)) p += L[i];
else p++,i--;
}
if (i <= E){
puts("NO");
}
else puts("YES");
}
}
int main(){
init();
solve();
return 0;
}


02-02 1418

10-03 68
01-15 31
04-13 4369
06-28 182
08-26 2081
01-09 413
09-21 1008
03-03 1770
04-06 875
08-28 1081
10-04 1193
11-22 812
05-26 1097
02-09 722
12-08 1040
10-04 1737