BZOJ3507: [Cqoi2014]通配符匹配 解题报告
2016-10-01 18:09
267 查看
日常吐槽部分可以跳过
这题又坑了蒟蒻一下午,表示很不爽(自己弱能怪谁)看完题想了一会一直觉得是很高级的做法,自己肯定不会了,搜了题解并不能看懂在讲什么,去问大神,Claris表示这不是 贪心+hash 就能搞定的题吗(为什么我连贪心都没想到啊还有怎么hash匹配并不会)
敲完怎么拍都拍不出错,一个小时后拍出来结果发现一个判 ? 的地方下标没更新,改了就A了。
但是为什么我注释掉这个判 ? 的语句都能A啊,是数据太水了还是这种题目本身容易被水….
感谢Claris耐心的讲解
吐槽结束,下面是题解
这道题因为每个串都要知道能不能匹配,只能一个一个判,那么考虑一个通配串和一个文件名判能怎么判断能否匹配。可以发现一种策略,根据 * 将通配串分成 cnt 份,通配串两端的2份肯定要和文件名两端匹配,对于剩下的cnt-2份,从第1份结束匹配的下一个位置开始,每一份枚举起点匹配,那么每一份匹配的起点肯定越左越好,这样可以给之后的多一些起点的选择
至于枚举了起点,怎么和这一份匹配,可以将这一份再按照?分成 k 份,根据题意,k≤10,每一份算出hash值,预处理文件名每个前缀的hash值,这样知道了起点后可以在O(k)的时间内判断能不能匹配
总复杂度O(nk)
有疑惑欢迎留言和我讨论
code:
#include<set> #include<map> #include<deque> #include<queue> #include<stack> #include<ctime> #include<cmath> #include<string> #include<vector> #include<string> #include<cstdio> #include<cstring> #include<climits> #include<cstdlib> #include<complex> #include<iostream> #include<algorithm> #define ll long long using namespace std; const ll M = 1000000007; const int maxn = 100010; const int maxm = 15; char a[maxn],b[maxn]; ll pw[maxn]; int cnt,pos[maxn]; int s[maxm][maxm],num[maxm]; ll f[maxm][maxm]; ll bs[maxn]; int n,m; ll get_hash( int l,int r ){ return bs[r] - bs[l-1]*pw[r-l+1]; } bool judge( int i,int k ) { for( int j=0;j<=num[k];j++ ) { if( s[k][j] == 0 ) {i++;continue;} if( f[k][j] != get_hash( i,i+s[k][j]-1 ) ) return false; i += s[k][j]+1; } return true; } bool solve() { scanf("%s",b+1); int len = strlen( b+1 ); if( !cnt ) { if( n != len ) return false; for( int i=1;i<=n;i++ ) if( a[i] != b[i] && a[i] != '?' ) return false; return true; } if( len < (pos[1]-1) + (n-pos[cnt]) ) return false; for( int i=1;i<pos[1];i++ ) if( a[i] != b[i] && a[i] != '?' ) return false; for( int i=n,j=len;i>pos[cnt];i--,j-- ) if( a[i] != b[j] && a[i] != '?' ) return false; for( int i=1;i<=len;i++ ) bs[i] = bs[i-1]*M+b[i]; int i = pos[1], up = len-(n-pos[cnt])+1; for( int k=2;k<=cnt;k++ ) { for( ;i<=up;i++ ) if( judge( i,k ) ) break; i += pos[k]-pos[k-1]-1; if( i > up ) return false; } return true; } int main() { n=0;char cc; for( ;(cc=getchar())!='\n';a[++n] = cc ); pw[0] = 1ll; cnt = 0; for( int i=1;i<=n;i++ ) pw[i] = pw[i-1]*M; for( int i=1;i<=n;i++ ) if( a[i] == '*' ) pos[++cnt] = i; for( int i=2;i<=cnt;i++ ) { num[i] = 0; for( int j=pos[i-1]+1;j<pos[i];j++ ) { if( a[j] == '?' ) { num[i]++; continue; } s[i][num[i]]++; f[i][num[i]] = f[i][num[i]]*M+a[j]; } } scanf("%d",&m); while( m-- ) { if( solve() ) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- BZOJ3507 [Cqoi2014]通配符匹配 【哈希 + 贪心】
- BZOJ3507.【CQOI2014】通配符匹配
- bzoj3507【CQOI2014】通配符匹配
- BZOJ3507: [Cqoi2014]通配符匹配
- BZOJ3503: [Cqoi2014]和谐矩阵 解题报告
- JZOJ 3600. 【CQOI2014】通配符匹配
- BZOJ3505: [Cqoi2014]数三角形 解题报告
- [CQOI2014][bzoj3507] 通配符匹配 [字符串hash+dp]
- Leetcode #44. Wildcard Matching 通配符匹配 解题报告
- 【bzoj3570】 Cqoi2014—通配符匹配
- 【CQOI2014】通配符匹配
- bzoj3507 [Cqoi2014]通配符匹配
- HDU 2063过山车 解题报告(我的第一道二分匹配)
- 【题解】HNOI-2014 Day2解题报告
- 2014 第六届蓝桥杯校内赛解题报告 答案
- HDU 1853 Cyclic Tour(二分图最优匹配) 解题报告
- 【百度之星2014~资格赛解题报告】
- 2014百度之星初赛第一轮解题报告:grids
- NOIP2014解方程解题报告
- 2014哈商大ICPC/ACM校赛解题报告