牛客-富豪凯匹配串(bitset)
题目传送门
sol1:用bitset来维护,其实感觉挺暴力的,不怎么会用bitset,借着这道题学习一下。
- bitset暴力维护
#include "bits/stdc++.h"
#define debug puts("what the fuck");
using namespace std;
const int MAXN = ;
char s[MAXN];
bitset<> bs[][MAXN];
int main() {
int n, m, q;
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) {
scanf("%s", s + );
for (int j = ; s[j]; j++)
bs[s[j] ^ ''][j][i] = ;
}
scanf("%d", &q);
for (int i = ; i <= q; i++) {
scanf("%s", s + );
bitset<> res;
res.set();
for (int j = ; s[j]; j++) {
if (s[j] == '_') continue;
res &= bs[s[j] ^ ''][j];
}
printf("%d\n", res.count());
}
return ;
}
sol2:一开始做这题,看到能有多少个匹配上的字符串联想到的是字典树,但是不断优化还是超时。然后跟一位网友学到了开两颗字典树然后匹配的方法。
- 字典树
#include "bits/stdc++.h"
using namespace std;
#define debug puts("what the fuck");
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
const int MAXNODE = 3e6 + ;
const int MAXN = ;
int cnt[MAXNODE], ans[MAXNODE];
int res[MAXN];
vector<int> q[MAXNODE];
struct Trie {
int son[MAXNODE][];
int tot;
void insert(char* s, int v, int op) {
// puts(s);
int p = ;
for (int i = ; s[i]; i++) {
int id = (s[i] == '_') ? : s[i] ^ '';
if (!son[p][id]) son[p][id] = ++tot;
p = son[p][id];
// printf("\t%d %c", p, s[i]);
}
// puts("");
if (op == ) cnt[p] += v;
else q[p].push_back(v);
}
} t1, t2;
char s[MAXN];
void merge(int p1, int p2, int p, int m) {
if (p == m) {
ans[p2] += cnt[p1];
// printf("\t %d %d\n", p1, p2);
return;
}
if (t2.son[p2][] && t1.son[p1][]) merge(t1.son[p1][], t2.son[p2][], p + , m);
if (t2.son[p2][] && t1.son[p1][]) merge(t1.son[p1][], t2.son[p2][], p + , m);
if (t2.son[p2][]) {
if (t1.son[p1][]) merge(t1.son[p1][], t2.son[p2][], p + , m);
if (t1.son[p1][]) merge(t1.son[p1][], t2.son[p2][], p + , m);
}
}
int main() {
int n, m, qq;
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) {
scanf("%s", s);
t1.insert(s, , );
}
scanf("%d", &qq);
for (int i = ; i <= qq; i++) {
scanf("%s", s);
t2.insert(s, i, );
}
merge(, , , m);
for (int i = ; i <= t2.tot; i++) {
for (auto index : q[i]) {
res[index] = ans[i];
}
}
for (int i = ; i <= qq; i++)
printf("%d\n", res[i]);
return ;
}代码妙在离线后的匹配最多只要把两颗字典树都跑一边,复杂度应该是两颗字典树的节点数。网上看到另一种字典树写法分类讨论,如果询问中'_'的 个数小于20就用暴力,感觉在卡数据,不过比赛的时候这种投机的方法也未尝不可。
牛客-富豪凯匹配串(bitset)的更多相关文章
- 牛客练习赛53 (C 富豪凯匹配串) bitset
没想到直接拿 bitset 能过 $10^8$~ code: #include <bits/stdc++.h> #define N 1004 #define setIO(s) freope ...
- 牛客练习赛53 C 富豪凯匹配串
思路: bitset的简单题,不幸的是当时的我并不知道bitset, C++的 bitset 在 bitset 头文件中,它是一种类似数组的结构,它的每一个元素只能是0或1,每个元素仅用1bit空间, ...
- 牛客练习赛53 C题bitset
题目链接https://ac.nowcoder.com/acm/contest/1114/C #include<bits/stdc++.h> using namespace std; #d ...
- 牛客练习赛53 A-E
牛客联系赛53 A-E 题目链接:Link A 超越学姐爱字符串 题意: 长度为N的字符串,只能有C,Y字符,且字符串中不能连续出现 C. 思路: 其实就是DP,\(Dp[i][c]\) 表示长度为 ...
- 牛客小白月赛13 小A的回文串(Manacher)
链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 题目描述 小A非常喜欢回文串,当然我们都知道回文串这种情况是非常特殊的.所以小A只想知道给定的一个 ...
- 牛客网剑指Offer——正则表达式匹配
1. 题目描述 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整 ...
- 牛客寒假算法基础集训营4 I Applese 的回文串
链接:https://ac.nowcoder.com/acm/contest/330/I来源:牛客网 自从 Applese 学会了字符串之后,精通各种字符串算法,比如……判断一个字符串是不是回文串. ...
- 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数
目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...
- 好串_via牛客网
题目 链接:https://ac.nowcoder.com/acm/contest/28537/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
随机推荐
- maven常用配置setting.xml详解
参考文章: https://www.cnblogs.com/hwaggLee/p/4579418.html 1.<localRepository/> 该值maven本地仓库的路径 < ...
- BZOJ [Cqoi2017] 小Q的棋盘
题解:枚举最后在哪里停止,然后剩下的步数/2 也就是找最大深度 枚举终止位置算是一种思路吧 #include<iostream> #include<cstdio> #inclu ...
- VS2019企业版产品密钥
Visual Studio 2019 Enterprise产品密钥(激活码) BF8Y8-GN2QH-T84XB-QVY3B-RC4DF
- C/C++源程序到可执行程序的过程
源程序.cpp 预处理得到 预处理文件.i 编译得到 汇编文件.S 汇编得到 目标文件.o 链接得到 可执行文件 例子:main.cpp fun.cpp fun.h #inclu ...
- Java--Excel操作
public static List<Info> readXml(String fileName, Map<String, Fuck> pcMap) throws Except ...
- Lyft、Uber、滴滴涉足汽车租赁领域,能打破既有汽车所有权模式吗?
自共享经济出现之后,众多相关项目遍地开花.这些共享经济项目对于人们来说,最直观的感受就是实惠.性价比高.方便.不过抛开这些使用层面的优点来看的话,共享经济项目最大的特色或许就是改变了事物的所有权.一件 ...
- Python说文解字_父类的继承
1. 第一个问题: 我们知道类是可以继承其他类的,在继承的过程中我们不光可以继承父类的方法,还可继承父类的属性,另外还可以在父类的基础上添加自己的东西. 2. 第二个问题: 我们继承父类属性和方法的时 ...
- 写一个读取Excel表格的接口
# -*- coding: gbk -*-import xlrd class Canshu: def __init__(self,filepath): """ 创建文件对 ...
- java 的HashMap底层数据结构
HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在.在HashMap中,key-value总是会当做一个整体来处理,系统会根据 ...
- android studio 修改新建EmptyActivity默认布局
https://www.jianshu.com/p/d4f201135097 打开你的Android Sudio安装目录,我的为D:\Program Files\Android\Android Stu ...