BZOJ2754: [SCOI2012]喵星球上的点名(AC自动机)
2024-09-04 01:55:13
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
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
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 + , B = , mod = ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
const int root = ;
int N, M;
int fail[MAXN], tot = , 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 = ; 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 = ; 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 = ; 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 = ; i <= N; i++) {
for(int j = ; j <= ; j++) {
int num = read();
for(int k = ; k <= num; k++)
mi[i].push_back(read());
mi[i].push_back(-);
}
}
for(int i = ; i <= M; i++) {
vector<int> t;
int n = read();
for(int j = ; j <= n; j++)
t.push_back(read());
insert(t, i);
}
GetFail();
for(int i = ; i <= N; i++)
work(mi[i], i);
for(int i = ; i <= M; i++) printf("%d\n", ans1[i]);
for(int i = ; i <= N; i++) printf("%d ", ans2[i]);
}
最新文章
- Matlab
- SUI分页组件和avalon搞定ajax无刷新分页
- Eclipse中Jquery报错
- NOI模拟赛Day5
- mysql source命令超大文件导入方法总结
- MVC4.0中项目发布遇到IE11时session存入URL中,导致记不住密码的问题
- 有关eclipse连接SQL Server 2008的问题
- 夺命雷公狗---DEDECMS----1dedecms的安装过程
- digitalocean
- scrapy wiki资料汇总
- Maximum Product Subarray JAVA实现
- 【转】Github入门教程
- good bye 2015 B - New Year and Old Property
- KEEPALIVED 检测RS原理
- Xcode7.1与iOS9之坑
- webdriver设置浏览器全屏及设置浏览器窗口为特定大小的方法
- RabbitMQ消息的交换
- 基于git命令的代码统计方法
- string Type
- python之tkinter使用-复选框操作