zoukankan      html  css  js  c++  java
  • BZOJ2754: [SCOI2012]喵星球上的点名(AC自动机)

    Time Limit: 20 Sec  Memory Limit: 128 MB
    Submit: 2816  Solved: 1246
    [Submit][Status][Discuss]

    Description

    a180285幸运地被选做了地球到喵星球的留学生。他发现喵星人在上课前的点名现象非常有趣。   假设课堂上有N个喵星人,每个喵星人的名字由姓和名构成。喵星球上的老师会选择M个串来点名,每次读出一个串的时候,如果这个串是一个喵星人的姓或名的子串,那么这个喵星人就必须答到。 然而,由于喵星人的字码过于古怪,以至于不能用ASCII码来表示。为了方便描述,a180285决定用数串来表示喵星人的名字。
    现在你能帮助a180285统计每次点名的时候有多少喵星人答到,以及M次点名结束后每个喵星人答到多少次吗?  

    Input

     
    现在定义喵星球上的字符串给定方法:
    先给出一个正整数L,表示字符串的长度,接下来L个整数表示字符串的每个字符。
    输入的第一行是两个整数N和M。
    接下来有N行,每行包含第i 个喵星人的姓和名两个串。姓和名都是标准的喵星球上的
    字符串。
    接下来有M行,每行包含一个喵星球上的字符串,表示老师点名的串。

    Output

     
    对于每个老师点名的串输出有多少个喵星人应该答到。
    然后在最后一行输出每个喵星人被点到多少次。

    Sample Input

    2 3
    6 8 25 0 24 14 8 6 18 0 10 20 24 0
    7 14 17 8 7 0 17 0 5 8 25 0 24 0
    4 8 25 0 24
    4 7 0 17 0
    4 17 0 8 25

    Sample Output


    2
    1
    0
    1 2
    【提示】
    事实上样例给出的数据如果翻译成地球上的语言可以这样来看
    2 3
    izayoi sakuya
    orihara izaya
    izay
    hara
    raiz

    HINT



    【数据范围】 

     对于30%的数据,保证: 

    1<=N,M<=1000,喵星人的名字总长不超过4000,点名串的总长不超过2000。

    对于100%的数据,保证:

    1<=N<=20000,1<=M<=50000,喵星人的名字总长和点名串的总长分别不超过100000,保证喵星人的字符串中作为字符存在的数不超过10000。

    Source

    这题貌似有$n$多种搞法啊。。

    AC自动机+unordered_map?

    后缀数组乱搞?

    AC自动机+莫队/树状数组??

    我只会第一个qwq。。。

    正经一点的可以看这里https://blog.csdn.net/clover_hxy/article/details/52502544

    暴力代码真难写

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<ext/pb_ds/assoc_container.hpp>
    #include<ext/pb_ds/hash_policy.hpp>
    using namespace __gnu_pbds;
    #define mit cc_hash_table<int, int>::iterator 
    using namespace std;
    const int MAXN = 1e5 + 10, B = 26, mod = 10007;
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    const int root = 0;
    int N, M;
    int fail[MAXN], tot = 0, val[MAXN];
    cc_hash_table<int, int>ch[MAXN];
    vector<int> mi[MAXN], sum[MAXN];
    void insert(vector<int> v, int ID) {
        int len = v.size();
        int now = root;
        for(int i = 0; i < len; i++) {
            int x = v[i];
            if(!ch[now][x]) ch[now][x] = ++tot;
            now = ch[now][x];
        }
        sum[now].push_back(ID);
    }
    void GetFail() {
        queue<int> q; 
        for(mit i = ch[root].begin(); i != ch[root].end(); i++) q.push(i -> second);
        while(!q.empty()) {
            int p = q.front(); q.pop();
            //if(p == 0) continue;
            for(mit i = ch[p].begin(); i != ch[p].end(); i++) {
                int x = i -> first, pos = i -> second, t = fail[p];
                while(t && !ch[t][x]) t = fail[t];
                if(ch[t][x]) fail[pos] = ch[t][x];
                //printf("%d %d
    ", pos, fail[pos]);
                q.push(pos);
            }
        }
    }
    int ans1[MAXN], ans2[MAXN], happen[MAXN];
    void work(vector<int> v, int id) {
        int now = root;
        int len = v.size();
        for(int i = 0; i < len; i++) {
            int x = v[i];
            if(ch[now][x]) now = ch[now][x];
            else {
                while(now && !ch[now][x]) now = fail[now];
                if(ch[now][x]) now = ch[now][x];
            }
            for(int cur = now; cur; cur = fail[cur])
                for(int k = 0; k < sum[cur].size(); k++) {
                    int add = sum[cur][k];
                    if(happen[add] != id) {
                        ans1[add]++;
                        happen[add] = id, ans2[id]++;                    
                    }
                }
        }
    }
    main() {
    #ifdef WIN32
        freopen("a.in", "r", stdin);
        freopen("b.out", "w", stdout);
    #endif
        N = read(); M = read();
        for(int i = 1; i <= N; i++) {
            for(int j = 0; j <= 1; j++) {
                int num = read();
                for(int k = 1; k <= num; k++)
                    mi[i].push_back(read());
                mi[i].push_back(-1);
            }
        }
        for(int i = 1; i <= M; i++) {
            vector<int> t;
            int n = read();
            for(int j = 1; j <= n; j++)
                t.push_back(read());
            insert(t, i);
        }
        GetFail();
        for(int i = 1; i <= N; i++) 
            work(mi[i], i);
        for(int i = 1; i <= M; i++) printf("%d
    ", ans1[i]);
        for(int i = 1; i <= N; i++) printf("%d ", ans2[i]);
    }
  • 相关阅读:
    android之PackageManager简单介绍
    西门子PLC学习笔记二-(工作记录)
    node.js第十课(HTTPserver)
    ubuntu ???????????? no permissions 问题解决
    Web API 设计摘要
    公共 DNS server IP 地址
    用Unicode迎接未来
    vs2010公布时去除msvcp100.dll和msvcr100.dll图讲解明
    linux串口驱动分析
    C++ 中dynamic_cast&lt;&gt;的用法
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9250577.html
Copyright © 2011-2022 走看看