前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BZOJ2754: [SCOI2012]喵星球上的点名(AC自动机)

BZOJ2754: [SCOI2012]喵星球上的点名(AC自动机)

作者头像
attack
发布2018-07-04 11:52:12
2680
发布2018-07-04 11:52:12
举报

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

暴力代码真难写

代码语言:javascript
复制
#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\n", 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\n", ans1[i]);
    for(int i = 1; i <= N; i++) printf("%d ", ans2[i]);
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-07-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • HINT
  • Source
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档