【bzoj3570】 Cqoi2014—通配符匹配
http://www.lydsy.com/JudgeOnline/problem.php?id=3507 (题目链接)
题意
给出一个主串,里面有些通配符,'*'可以代替任意字符串或者消失,'?'可以且仅可以代替一个字符。问能否用这个串匹配一些询问串。
Solution
也许我zz了,半天写不出→_→
$f[i][j]$表示主串到第$i$个匹配符,询问串到第$j$个字符能否被匹配。转移很简单,hash一下就好了。
细节
在主串末尾添加一个'*'。
代码
// bzoj3507 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #define LL long long #define inf (1ll<<30) #define Pi acos(-1.0) #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout); using namespace std; const int maxn=100010; int f[12][maxn],pos[maxn],m,n; unsigned LL bin[maxn],c[maxn],d[maxn]; char s[maxn],a[maxn]; int main() { scanf("%s",s+1); int ll=strlen(s+1); s[++ll]='*'; for (int i=1;i<=ll;i++) if (s[i]=='?' || s[i]=='*') pos[++m]=i; bin[0]=1;for (int i=1;i<=100000;i++) bin[i]=bin[i-1]*127; for (int i=1;i<=ll;i++) c[i]=c[i-1]*127+s[i]; int T;scanf("%d",&T); while (T--) { scanf("%s",a+1); int n=strlen(a+1); for (int i=1;i<=n;i++) d[i]=d[i-1]*127+a[i]; memset(f,0,sizeof(f)); f[0][0]=1; for (int i=0;i<m;i++) { if (s[pos[i]]=='*') for (int j=1;j<=n;j++) if (f[i][j-1]) f[i][j]=1; for (int j=0;j<=n;j++) if (f[i][j]) { unsigned LL id1=c[pos[i+1]-1]-c[pos[i]]*bin[pos[i+1]-pos[i]-1]; unsigned LL id2=d[j+pos[i+1]-pos[i]-1]-d[j]*bin[pos[i+1]-pos[i]-1]; if (id1!=id2) continue; if (s[pos[i+1]]=='?') f[i+1][j+pos[i+1]-pos[i]]=1; else f[i+1][j+pos[i+1]-pos[i]-1]=1; } } puts(f[m][n] ? "YES" : "NO"); } return 0; }
This passage is made by MashiroSky.