题目传送门

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)的更多相关文章

  1. 牛客练习赛53 (C 富豪凯匹配串) bitset

    没想到直接拿 bitset 能过 $10^8$~ code: #include <bits/stdc++.h> #define N 1004 #define setIO(s) freope ...

  2. 牛客练习赛53 C 富豪凯匹配串

    思路: bitset的简单题,不幸的是当时的我并不知道bitset, C++的 bitset 在 bitset 头文件中,它是一种类似数组的结构,它的每一个元素只能是0或1,每个元素仅用1bit空间, ...

  3. 牛客练习赛53 C题bitset

    题目链接https://ac.nowcoder.com/acm/contest/1114/C #include<bits/stdc++.h> using namespace std; #d ...

  4. 牛客练习赛53 A-E

    牛客联系赛53 A-E 题目链接:Link A 超越学姐爱字符串 题意: 长度为N的字符串,只能有C,Y字符,且字符串中不能连续出现 C. 思路: 其实就是DP,\(Dp[i][c]\) 表示长度为 ...

  5. 牛客小白月赛13 小A的回文串(Manacher)

    链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 题目描述 小A非常喜欢回文串,当然我们都知道回文串这种情况是非常特殊的.所以小A只想知道给定的一个 ...

  6. 牛客网剑指Offer——正则表达式匹配

    1. 题目描述 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整 ...

  7. 牛客寒假算法基础集训营4 I Applese 的回文串

    链接:https://ac.nowcoder.com/acm/contest/330/I来源:牛客网 自从 Applese 学会了字符串之后,精通各种字符串算法,比如……判断一个字符串是不是回文串. ...

  8. 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数

    目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...

  9. 好串_via牛客网

    题目 链接:https://ac.nowcoder.com/acm/contest/28537/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

随机推荐

  1. Navicat Premium 12安装和破解

    链接:https://pan.baidu.com/s/1x8AFWlJYGIl3TlbA1vX63g 提取码:9hu0  安装步骤: 1.下载好后点击navicat12018_premium_cs_x ...

  2. 【收藏】免费开源好看的bootstrap后台模板

    1.ace admin github:https://github.com/bopoda/acedemo:http://ace.jeka.by/ 2.CoreUI jQuery.Angular.Rea ...

  3. CodeForces - 686D 【树的重心】

    传送门:http://codeforces.com/problemset/problem/686/D 题意:给你n个节点,其中1为根, 第二行给你2~n的节点的父亲节点编号. 然后是q个询问,求询问的 ...

  4. Nacos快速开始

    Nacos是一个服务发现.配置管理和服务管理的组件. 说到服务注册与发现,我想到Eureka.Zookeeper 说到服务治理,我想到Dubbo 说到配置管理,我想到Apollo 作为后起之秀的Nac ...

  5. Vue.js 之 过渡动画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 视频课程 | 云原生下的Serverless浅谈

    京东云开发者社区在3月底于北京举行了以"Cloud Native时代的应用之路与开源创新"为主题的技术沙龙,现场多位技术大咖与开发者们面对面就Cloud Native进行了深入交流 ...

  7. Swagger注解及参数细节的正确书写。

    今天新开了一个api文件,结果怎么搞也在swagger里显示不出来,浪费半天后,去问老员工了. 一般有俩原因, 1.idea缓存,重启idea即可. 2.注解和参数上的修饰有问题,或者请求method ...

  8. IOS下的safari不支持localStorage?

    同事在统计日志的时候,想用localStorag去记载一些什么,但是在各大浏览器都运行的良好的基础上,唯独IOS下的safari一直静静无声,没有任何反应.打印localStorage都是Object ...

  9. linux下ffmpeg环境搭建记录

    1.Linux下安装yasm 官网下载:http://yasm.tortall.net/Download.html tar -zvxf yasm-1.3.0.tar.gz cd yasm-1.3.0/ ...

  10. 17.3.12--urllib2模块

    1---urllib2是非常强大的Python网络资源访问模块,它的功能和urllib模块相似 python标准库中的urllib2模块可以说是urlib模块的一个升级的复杂版,不需要另外下载, 比如 ...