题面

题解

分三种情况:

若所有串都没有通配符,直接哈希比较即可。

若所有串都有通配符,

把无通配符的前缀 和 无通配符的后缀哈希后比较即可。

中间部分由于通配符的存在,一定可以使所有串匹配。

若部分串有通配符,

首先把所有无通配符的字符串比较好。

现在问题变为,能否通过通配符使每个串变为一个模板串。

首先把前后缀比较好,然后就是中间部分。

其实只需要让有通配符的串的中间部分与模板串匹配就可以合法。

匹配暴力\(O(n)\)即可。

(参考\(YCB\)的题解)

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
using namespace std; const unsigned long long X = 60923;
unsigned long long val[10000010];
const int MAX_N = 1e5 + 5;
string s[MAX_N], s1[MAX_N];
vector<unsigned long long> h[MAX_N];
vector<int> pos[MAX_N];
inline int cmp(const string &lhs, const string &rhs) { return lhs.length() < rhs.length(); }
int T, n; void calc(int i) {
pos[i].clear(), h[i].clear();
pos[i].push_back(-1), h[i].push_back(0);
for (string::iterator it = s[i].begin(); it != s[i].end(); ++it) {
h[i].push_back(h[i].back() * X + *it);
if (*it == '*')
pos[i].push_back(it - s[i].begin());
}
pos[i].push_back(s[i].length());
} inline unsigned long long Hash(const vector<unsigned long long> &vec, int l, int r) {
++l, ++r;
return vec[r] - vec[l - 1] * val[r - l + 1];
} bool check(int x, int y) {
int lenx = s[x].length(), leny = s[y].length();
if (s[y].find('*') != string::npos)
swap(x, y), swap(lenx, leny);
if (s[x].find('*') == string::npos && s[y].find('*') == string::npos)
return Hash(h[x], 0, s[x].length() - 1) == Hash(h[y], 0, s[y].length() - 1);
else {
string A = "";
string::size_type p = 0;
for (int i = 1; i < pos[x].size(); i++) {
int tpos = p, len = pos[x][i] - pos[x][i - 1] - 1;
while (tpos + len - 1 < s[y].length() &&
Hash(h[x], pos[x][i - 1] + 1, pos[x][i] - 1) != Hash(h[y], tpos, tpos + len - 1))
++tpos;
if (tpos + len - 1 >= s[y].length()) return false;
if (tpos != 0 && p == 0) return false;
p = tpos + len;
}
return true;
}
} void Doit() {
int pos = -1;
for (int i = 1; i <= n; i++) calc(i);
for (int i = 1; i <= n; i++)
if (s[i].find('*') == string::npos) { pos = i; break; }
if (pos == -1) {
for (int i = 1; i <= n; i++) {
s1[i] = "";
for (int j = 0; j < s[i].length(); j++)
if (s[i][j] == '*') break;
else s1[i] += s[i][j];
}
sort(s1 + 1, s1 + n + 1, cmp);
for (int i = 2; i <= n; i++) {
for (int j = 0; j < s1[i - 1].length(); j++)
if (s1[i][j] != s1[i - 1][j])
return (void)(cout << 'N' << endl);
} for (int i = 1; i <= n; i++) {
s1[i] = "";
for (int j = s[i].length() - 1; ~j; j--)
if (s[i][j] == '*') break;
else s1[i] += s[i][j];
}
sort(s1 + 1, s1 + n + 1, cmp);
for (int i = 2; i <= n; i++) {
for (int j = 0; j < s1[i - 1].length(); j++)
if (s1[i][j] != s1[i - 1][j])
return (void)(cout << 'N' << endl);
}
} else
for (int i = 1; i <= n; i++) {
if (i == pos) continue;
if (!check(i, pos)) return (void)(cout << 'N' << endl);
}
cout << 'Y' << endl;
} int main() {
ios::sync_with_stdio(false);
cin >> T, val[0] = 1;
for (int i = 1; i <= 10000000; i++) val[i] = val[i - 1] * X;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> s[i];
Doit();
}
return 0;
}

【LG3234】[HNOI2014]抄卡组的更多相关文章

  1. bzoj3574[Hnoi2014]抄卡组

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

  2. luogu P3234 [HNOI2014]抄卡组

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

  3. BZOJ3574 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. 匹配IP的正则表达式

    正则表达式匹配IP 1 ((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[1-9])  

  2. java.lang.NoClassDefFoundError: org/aspectj/weaver/tools/PointcutPrimitive

    问题:使用Spring时,报错:java.lang.NoClassDefFoundError: org/aspectj/weaver/tools/PointcutPrimitive问题原因: 少了as ...

  3. java反射机制执行命令

    public class Encryptor{ public static void main(String[] args) throws IOException, ClassNotFoundExce ...

  4. 7、Dubbo-配置(2)

    重试次数 通常配合timeout超时设置进行配置 <dubbo:reference "> </dubbo:reference> <dubbo:service i ...

  5. linux 下 chkconfig安装与使用详解

    chkconfig 安装 开始的时候因为Raspbian的原因,系统是不自带chkconfig这个命令的, root@raspberrypi:~# chkconfig-bash: chkconfig: ...

  6. 在 Linux 下搭建 Git 服务器(yum安装)

    服务端(linux): 1. 安装git [root@localhost ~]# yum -y install git 2. 增加一个git账户 为了管理的方便,在linux下面增添一个 " ...

  7. Struts2通配符

    action: struts: or: 请求路径:

  8. 404 Note Found 队-Alpha1

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内 ...

  9. 二叉查找树(二叉排序树)(C语言)

    #include<stdio.h> #include "fatal.h" struct TreeNode; typedef struct TreeNode *Posit ...

  10. Oracle结合Mybatis实现取表TOP 10

    之前一直使用mysql和informix数据库,查表中前10条数据十分简单: 最原始版本: select top * from student 当然,我们还可以写的复杂一点,比如外加一些查询条件? 比 ...