您的位置:首页 > 其它

JZOJ 3600. 【CQOI2014】通配符匹配

2018-01-06 20:09 330 查看

Description



Input



Output



Sample Input

*abc?e**e

3 abcee

ppabcqexe

abcdefgee

Sample Output

NO

YES

YES

Data Constraint



Solution

由于通配符个数不超过10,考虑DP。

设包含通配符的串为 S ,要匹配的没有通配符的串为 T 。

设 f[i][j] 表示在 S 中做到第 i 个通配符、在 T 中匹配到第 j 个字符 是否可行

那么我们先预处理出字符串的前缀的哈希值(用自然溢出),

这样就可以很方便直接提取字符串的一个区间了。

接着我们在就直接开始转移即可。

注意通配符“∗”可以完全匹配一整段(循环整段赋值),

而如果是“?” 转移的时候就要多匹配一位(因为至少要匹配一位)。

时间复杂度 O(T∗10∗|S|) 。

Code

#include<cstdio>
#include<cstring>
using namespace std;
typedef unsigned long long ULL;
const int N=1e5+5;
const ULL M=1e9+7;
int T,n,num,m;
int p[12];
ULL hs
,ht
,g
;
bool f[12]
;
char s
,t
;
inline ULL hashs(int l,int r){return hs[r]-hs[l-1]*g[r-l+1];}
inline ULL hasht(int l,int r){return ht[r]-ht[l-1]*g[r-l+1];}
int main()
{
freopen("3.in","r",stdin);
scanf("%s",s+1),n=strlen(s+1),s[++n]='?';
for(int i=1;i<=n;i++)
{
hs[i]=hs[i-1]*M+s[i];
if(s[i]=='*' || s[i]=='?') p[++num]=i;
}
for(int i=g[0]=1;i<N;i++) g[i]=g[i-1]*M;
scanf("%d",&T);
while(T--)
{
scanf("%s",t+1),m=strlen(t+1),t[++m]='$';
for(int i=1;i<=m;i++) ht[i]=ht[i-1]*M+t[i];
memset(f,false,sizeof(f));
f[0][0]=true;
for(int i=0;i<num;i++)
{
if(s[p[i]]=='*')
for(int j=1;j<=m;j++)
if(f[i][j-1]) f[i][j]=true;
for(int j=0,len=p[i+1]-p[i];j<=m-len+1;j++)
if(f[i][j] && hashs(p[i]+1,p[i+1]-1)==hasht(j+1,j+len-1))
f[i+1][j+len-(s[p[i+1]]!='?')]=true;
}
puts(f[num][m]?"YES":"NO");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: