容易发现通配符中间的部分可以任意匹配,会造成的无法匹配的仅仅是前后缀,前缀和后缀可以分别独立处理。如果字符串均有通配符,只需要按前/后缀长度排序然后暴力匹配就可以了。

  问题在于存在无通配符的字符串。显然首先这些字符串需要相同。剩下的字符串只要都能与该字符串匹配即可。然后就不会了。想了半天去看题解……暴力哈希。为啥跑2e8这么自信啊。

  bzoj莫名T。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
#define N 100010
#define L 10000010
#define ul unsigned long long
int T,n,pre[N],suf[N],id[N];
ul hash[][L],p[L];
vector<char> s[N];
bool isac(char c){return c>='a'&&c<='z'||c>=''&&c<=''||c=='*';}
bool cmp(const int&a,const int&b)
{
return pre[a]<pre[b];
}
bool check(int n)
{
for (int i=;i<=n;i++) id[i]=i;
sort(id+,id+n+,cmp);
for (int i=;i<=n;i++)
for (int j=;j<pre[id[i-]];j++)
if (s[id[i-]][j]!=s[id[i]][j]) return ;
return ;
}
ul gethash(int k,int l,int r)
{
if (l>r) return ;
return hash[k][r]-hash[k][l-]*p[r-l+];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj3574.in","r",stdin);
freopen("bzoj3574.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
T=read();
p[]=;for (int i=;i<=L-;i++) p[i]=p[i-]*;
while (T--)
{
n=read();
for (int i=;i<=n;i++)
{
s[i].clear();
char c=getchar();while (!isac(c)) c=getchar();
while (isac(c)) s[i].push_back(c),c=getchar();
}
hash[][]=;int len=;bool flag=;
for (int i=;i<=n;i++)
{
int l=s[i].size();
pre[i]=l,suf[i]=-;
for (int j=;j<l;j++)
if (s[i][j]=='*') {pre[i]=j;break;}
for (int j=l-;~j;j--)
if (s[i][j]=='*') {suf[i]=j;break;}
if (pre[i]==l)
if (!len)
{
len=l;
for (int j=;j<l;j++) hash[][j+]=hash[][j]*+s[i][j];
}
else
{
ul tot=;
for (int j=;j<l;j++) tot=tot*+s[i][j];
if (tot!=hash[][len]) {flag=;break;}
}
}
if (!flag) {cout<<"N\n";continue;}
if (!len)
{
if (!check(n)) {cout<<"N\n";continue;}
for (int i=;i<=n;i++)
{
reverse(s[i].begin(),s[i].end());
pre[i]=(int)s[i].size()-suf[i]-;
}
if (!check(n)) {cout<<"N\n";continue;}
cout<<"Y\n";
}
else
{
bool flag=;
for (int i=;i<=n;i++)
if (pre[i]!=s[i].size())
{
hash[][]=;
int l=s[i].size();
for (int j=;j<l;j++) hash[][j+]=hash[][j]*+s[i][j];
if (pre[i]+l--suf[i]>len||gethash(,,pre[i])!=gethash(,,pre[i])||gethash(,len-l+suf[i]+,len)!=gethash(,suf[i]+,l)) {flag=;break;}
int x=pre[i]+;flag=;
for (int j=pre[i];j<=suf[i];j++)
{
if (j==suf[i]) flag=;
if (s[i][j]=='*') continue;
int t=j;
while (s[i][t+]!='*') t++;
while (x+t-j<len-l+suf[i]+&&gethash(,x,x+t-j)!=gethash(,j+,t+)) x++;
if (x+t-j>=len-l+suf[i]+) break;
j=t;
}
if (flag) break;
}
if (flag) cout<<"N\n";else cout<<"Y\n";
}
}
return ;
}

BZOJ3574 HNOI2014抄卡组(哈希)的更多相关文章

  1. bzoj3574[Hnoi2014]抄卡组

    http://www.lydsy.com/JudgeOnline/problem.php?id=3574 我们发现如果所有的字符串都有*,那么只需要比较他们的“前缀”和“后缀”相同即可.“前缀”指第一 ...

  2. 【LG3234】[HNOI2014]抄卡组

    题面 题解 分三种情况: 若所有串都没有通配符,直接哈希比较即可. 若所有串都有通配符, 把无通配符的前缀 和 无通配符的后缀哈希后比较即可. 中间部分由于通配符的存在,一定可以使所有串匹配. 若部分 ...

  3. [HNOI2014]抄卡组

    [Luogu3234] [LOJ2208] 题解及代码 锻炼哈希码力的一道题 , 具体细节见代码 #include<cstdio> #include<cstring> #inc ...

  4. luogu P3234 [HNOI2014]抄卡组

    传送门 nmdwsm 自己看吧,不想写了qwq 垃圾代码如下 和题解完全不一样 #define LL long long #define uLL unsigned long long #define ...

  5. 洛谷P3234 抄卡组 [HNOI2014] 字符串hash

    正解:字符串hash 解题报告: 传送门! 字符串hash是字符串匹配中很常见的一个方法,原理也很好懂,这里就不做太多阐述辣有时间放到hash笔记里面去QAQ 题意不说了挺好理解的,自带一句话概括好评 ...

  6. 【HNOI2014】抄卡组

    题面 题解 如果所有的字符串都有通配符,那么只要比较不含通配符的前缀和后缀就可以了. 否则一定有一个串没有通配符.找出这个字符串,然后将所有串与这个串匹配,通配符将\(B\)分成一段一段在\(A\)上 ...

  7. 【LOJ6254】最优卡组 堆(模拟搜索)

    [LOJ6254]最优卡组 题面 题解:常用的用堆模拟搜索套路(当然也可以二分).先将每个卡包里的卡从大到小排序,然后将所有卡包按(最大值-次大值)从小到大排序,并提前处理掉只有一张卡的卡包. 我们将 ...

  8. HearthBuddy卡组

    偶数萨 手打两天已上传说,各位加油  欧洲牧羊人 ### 火元素换艾雅# 职业:萨满祭司# 模式:狂野模式## 2x (2) 图腾魔像        # 2x (2) 大漩涡传送门   # 2x (2 ...

  9. 服务器&阵列卡&组raid 5

    清除raid信息后,机器将会读不到系统, 后面若进一步操作处理, raid信息有可能会被初始化掉,那么硬盘数据就有可能会被清空, 导致数据丢失, 否则如果只是清除raid信息,重做raid是可以还原系 ...

随机推荐

  1. centos7下将java -jar命令运行一个项目做成systemd服务

    有些时候运行一个java项目在linux下通过一条简单的java命令即可,如: #nohup java -jar jenkins.war & ###这里为后台运行jenkins 在此背景下,j ...

  2. KVM虚拟化的安装

    kvm介绍 一.KVM的历史 Kvm在2007年2月正式合并Linux2.6.20核心中,成为内核源代码的一部分.2008年9月4日,RedHat公司收购了Qumranet,开始在RHEL中用kvm取 ...

  3. BZOJ3174. [TJOI2013]拯救小矮人(dp)

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=3174 题解 其实此题并不需要那么多YY的部分. 我们考虑若干个小矮人逃出的顺序.若跳出的 ...

  4. [SHELL]查看端口,文件,服务关系的四个命令netstat,lsof,fuser,nmap

    一,netstat (1)简介 netstat主要是用来打印系统网络的状态信息,当输入netstat后,输出如下: 可以看出,netstat的输出分为两个部分组成: 一个是Active Interne ...

  5. 启动sshd时,报“Could not load host key”错

    原文发表于cu:2016-05-24 现象:启动sshd服务时,虽看似服务启动成功,但客户端并不能连接上sshd服务器端.如下: [root@aefe8007a17d ~]# /usr/sbin/ss ...

  6. openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 三

    openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 一 openstack-r版(rocky)搭建基于centos7.4 的openstac ...

  7. 排查GCC 4.4.X版本优化switch-enum的BUG

    起因 一次偶然碰到一个诡异的bug,现象是同一份C++代码使用GCC4.4.x版本在开启优化前和优化后的结果不一样,优化后的代码逻辑不正确. 示例代码如下: //main.cpp #include & ...

  8. Python常用模块之PIL

    官方网址:http://www.pythonware.com/products/pil/index.htm Python Imaging Library (PIL) Python图像库(PIL)将图像 ...

  9. 工作在Amazon:为何晋升如此难?

    英文原文:Why It's So Difficult to Climb Amazon's Corporate Ladder 本文作者 Brad Stone 的新书 The Everything Sto ...

  10. Is It A Tree?(并查集)

    Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...