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

  问题在于存在无通配符的字符串。显然首先这些字符串需要相同。剩下的字符串只要都能与该字符串匹配即可。然后就不会了。想了半天去看题解……暴力哈希。为啥跑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. 【BZOJ4803】逆欧拉函数

    [BZOJ4803]逆欧拉函数 题面 bzoj 题解 题目是给定你\(\varphi(n)\)要求前\(k\)小的\(n\). 设\(n=\prod_{i=1}^k{p_i}^{c_i}\) 则\(\ ...

  2. LUA对象

    Rectangle = {width = , height = , area = }; function Rectangle:new(o, width, height) o = o or {}; se ...

  3. Maven学习(五)-----如何从Maven远程存储库下载?

    如何从Maven远程存储库下载? 根据 Apache Maven 的说明: Downloading in Maven is triggered by a project declaring a dep ...

  4. Redash二次开发-开发环境搭建

    环境:win7+pycharm 2018.2 +redash 1.安装pycharm并如何正常使用,找度娘. 2.配置pycharm vcs,设置github用户,从github新建redash项目 ...

  5. Python3中IO文件操作的常见用法

    首先创建一个文件操作对象: f = open(file, mode, encoding) file指定文件的路径,可以是绝对路径,也可以是相对路径 文件的常见mode: mode = “r”   # ...

  6. [转]git命令之git remote的用法

    git remote git  remote -v git init git add xxx git commit -m 'xxx' git remote add origin ssh://softw ...

  7. 【snmp】Linux开启snmp及查询

    1.Linux snmp 1.安装snmp yum install -y net-snmp* 2.备份snmp配置 cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.co ...

  8. gopherjs

    An example implementation of a GopherJS client and a Go server using the Improbable gRPC-Web impleme ...

  9. 基于kcp,consul的service mesh实现

    名字kmesh 技术:proxy,kcp,consul proxy proxy分为前端和后端 前端代理服务层,包括外部的service 后端实现负债均衡 kcp kcp 基于udp,能够实现快速的传输 ...

  10. Javascript中Generator(生成器)

    阅读目录 Generator的使用: yield yield* next()方法 next()方法的参数 throw方法() return()方法: Generator中的this和他的原型 实际使用 ...