http://www.lydsy.com/JudgeOnline/problem.php?id=3574

我们发现如果所有的字符串都有*,那么只需要比较他们的“前缀”和“后缀”相同即可。“前缀”指第一个*前的字符串,“后缀”指最后一个*后的字符串

如果存在一个字符串没有*,那么要求其他串都能跟这个串匹配,用哈希即可

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
#include<cassert>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;
typedef pair<int,int> PII;
typedef pair<DB,DB> PDD;
typedef complex<DB> CP;
typedef vector<int> VI; #define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b) for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define fi first
#define se second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))
#define SZ(x) (int(x.size()))
#define all(x) (x).begin(),(x).end()
#define ire(i,v,x) for(i=0,v=i<SZ(x)?x[i]:0;i<SZ(x);v=x[++i]) template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} inline int sgn(DB x){if(abs(x)<1e-)return ;return(x>)?:-;}
const DB Pi=acos(-1.0); int gint()
{
int res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
}
LL gll()
{
LL res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
} const int maxn=;
const int sumlen=; int n;
char mem[sumlen];int now;
int len[maxn],from[maxn],to[maxn];
int mark[maxn],com,nowlen,maxlen; #define P 991
ULL power[sumlen];
ULL sum[sumlen];
void read()
{
int i,j;
n=gint();
now=;
com=-;
maxlen=;
re(i,,n)
{
from[i]=now+;
cin>>mem+now+;
len[i]=strlen(mem+now+);
to[i]=from[i]+len[i]-;
now=to[i];
mark[i]=;
re(j,from[i],to[i])if(mem[j]=='*'){mark[i]=;break;}
if(!mark[i])com=i;
upmax(maxlen,len[i]);
}
power[]=;
while(nowlen<maxlen)power[nowlen+]=power[nowlen]*P,nowlen++;
re(i,,now)sum[i]=sum[i-]*P+mem[i];
} ULL gethash(int l,int r){return sum[r]-sum[l-]*power[r-l+];} int ch[maxn];
char RT[sumlen]; int check(int a,int b)
{
int head=from[b];
for(int l=from[a],r;l<=to[a];l=r+)
{
while(mem[l]=='*' && l<=to[a]) l++;
if(l>to[a])break;
for(r=l;r+<=to[a] && mem[r+]!='*';r++);
ULL RIG=gethash(l,r);
while(head+(r-l+)-<=to[b] && gethash(head,head+(r-l+)-)!=RIG) head++;
if(head+(r-l+)->to[b])return ;
head+=r-l+;
}
if(mem[from[a]]!='*')
{
int i,l=from[a],r;
for(r=l;r+<=to[a] && mem[r+]!='*';r++);
re(i,l,r)if(mem[i]!=mem[from[b]+i-l])return ;
}
if(mem[to[a]]!='*')
{
int i,l,r=to[a];
for(l=r;l->=from[a] && mem[l-]!='*';l--);
re(i,l,r)if(mem[i]!=mem[to[b]-(r-i)])return ;
}
return ;
} int check()
{
if(com!=-)
{
int i;
re(i,,n)
{
if(!mark[i] && (len[i]!=len[com] || gethash(from[i],to[i])!=gethash(from[com],to[com])))return ;
if(mark[i] && !check(i,com))return ;
}
return ;
}
else
{
int i,j,p=-;
re(i,,n)
{
for(ch[i]=;ch[i]+<=len[i] && mem[from[i]+ch[i]]!='*';ch[i]++);
if(p==- || ch[p]<ch[i])p=i;
}
re(i,,ch[p])RT[i]=mem[from[p]+i-];
re(i,,n)re(j,,ch[i])if(RT[j]!=mem[from[i]+j-])return ;
p=-;
re(i,,n)
{
for(ch[i]=;ch[i]+<=len[i] && mem[to[i]-ch[i]]!='*';ch[i]++);
if(p==- || ch[p]<ch[i])p=i;
}
re(i,,ch[p])RT[i]=mem[to[p]-i+];
re(i,,n)re(j,,ch[i])if(RT[j]!=mem[to[i]-j+])return ;
}
return ;
} int main()
{
freopen("hs.in","r",stdin);
freopen("hs.out","w",stdout);
int T=gint();
while(T--)
{
read();
PF("%c\n",check()?'Y':'N');
}
return ;
}

bzoj3574[Hnoi2014]抄卡组的更多相关文章

  1. BZOJ3574 HNOI2014抄卡组(哈希)

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

  2. luogu P3234 [HNOI2014]抄卡组

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

  3. 【LG3234】[HNOI2014]抄卡组

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

  4. [HNOI2014]抄卡组

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

  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. configure JAAS for jboss 7.1 and mysql--reference

    Hello all, In this tutorial we are going to configure JAAS for jboss 7.1 and mysql for Form based au ...

  2. Android(java)学习笔记217:开发一个多界面的应用程序之清单文件

    清单文件的重要参数:     <intent-filter>             代表的应用程序的入口界面           <action android:name=&quo ...

  3. centos7 部署ssserver

    centos7 部署shadowsocks服务端 为什么要选centos7? 以后centos7 肯定是主流,在不重要的环境还是尽量使用新系统吧 centos7 的坑 默认可能会有firewall 或 ...

  4. Dij的堆优化

    #include<algorithm> #include<iostream> #include<cstdio> #include<cstring> #i ...

  5. jQuery选择器实现隔行变色

    <script type="text/javascript"> $(function(){ $("#tableName tr:nth-child(even)& ...

  6. Android设计图(标注、切图)

    1.Android: 1)Android的单位是dp 2)分为ldpi/mdpi/hdpi/xhdpi/xxhdpi. 3)分辨率对应DPI ldpi  QVGA (240×320) mdpi  HV ...

  7. UIScrollView不能响应touch事件的解决办法

    UIScrollView本身事是不支持touch的,我们可以给她添加拓展 #import "UIScrollView+util.h" @implementation UIScrol ...

  8. 数据类型的转换String

    x.toString(): 无法转换null和undefined 不过String()却是万能的,其中的原理如下 function String(x){ if(x===undefined){ retu ...

  9. 使用Spring MVC,Mybatis框架等创建Java Web项目时各种前期准备的配置文件内容

    1.pom.xml 首先,pom.xml文件,里面包含各种maven的依赖,代码如下: <project xmlns="http://maven.apache.org/POM/4.0. ...

  10. 【转】C++之内部类(嵌套类)与外部类及友元

    [转]http://baike.baidu.com/link?url=Md223wQoT5s-3cZ5xRnj1pGmvm310DKAuh-HDrcEdc2l24rwobHrdEc_Mi4Z3BGP0 ...