BZOJ 2754: [SCOI2012]喵星球上的点名
2754: [SCOI2012]喵星球上的点名
Time Limit: 20 Sec Memory Limit: 128 MB
Submit: 649 Solved: 305
[Submit][Status]
Description
Input
Output
Sample Input
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
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
很裸很裸的AC自动机。只不过这道题让我基本搞懂了stl map的用法。
map.insert(pair) 插入映射关系
map.find(x) 查询x映射的关系,返回一个迭代器,可用first,second查找
map<int,int>::iterator it1; 迭代器
另外AC自动机的一个小细节,让我Wa了两遍:
fail指针如果需要指向深度为1的节点时要特判,而且对于已经确定fail指针的节点,切忌重新复制(其实就是if语句条件要严密)。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
#define MAXT 201000
#define MAXN MAXT
vector<int> query[MAXN];
int query_gets[MAXN];
int cat_gets[MAXN];
int qur_vis[MAXN];
int topq=-;
struct aaa
{
int id;
int fail;
int qur_id;
map<int,int> next;
aaa()
{
qur_id=id=fail=-;
}
};
int root;
aaa tree[MAXT];
int topt=;
int name[MAXN],topn=-;
int str[MAXN];
int n,m; void add_str(int * str,int qur)
{
int ind;
int now=root;
map<int,int>::iterator it1;
while (*str!=-)
{
ind=*(str++);
it1=tree[now].next.find(ind);
if (it1==tree[now].next.end())
{
tree[now].next.insert(make_pair(ind,++topt));
tree[topt].id=ind;
now=topt;
}else
{
now=it1->second;
}
}
if (tree[now].qur_id==-)
{
tree[now].qur_id=++topq;
query[topq].push_back(qur);
}else
{
query[tree[now].qur_id].push_back(qur);
}
}
int q[MAXT];
void build_tree()
{
int ope=,clo=,now,temp,x,ind;
map<int,int>::iterator it1,it2;
tree[].fail=;
for (it1=tree[].next.begin();it1!=tree[].next.end();it1++)
{
now=it1->second;
q[++clo]=now;
tree[now].fail=;
}
while (ope<clo)
{
now=q[++ope];
for (it1=tree[now].next.begin();it1!=tree[now].next.end();it1++)
{
x=it1->second;
ind=it1->first;
q[++clo]=x;
temp=tree[now].fail;
while (temp!=root)
{
it2=tree[temp].next.find(ind);
if (it2!=tree[temp].next.end())
{
tree[x].fail=it2->second;
break;
}
temp=tree[temp].fail;
}
it2=tree[root].next.find(ind);
if (tree[x].fail==-&&it2!=tree[root].next.end())//Oh, I forgot this part again
{
tree[x].fail=it2->second;
}
if (tree[x].fail==-)
{
tree[x].fail=;
}
}
}
} int count_str(int *str,int cat_id)
{
int ret=;
int now=;
int ind,x,temp;
int i;
map<int,int>::iterator it1;
while (*str!=-)
{
ind=*(str++);
it1=tree[now].next.find(ind);
while (now!=root&&it1==tree[now].next.end())
{
now=tree[now].fail;
it1=tree[now].next.find(ind);
}
if (it1==tree[now].next.end())
{
now=root;
}else
{
now=it1->second;
}
temp=now;
while (temp!=root)
{
if (tree[temp].qur_id!=-)
{
x=tree[temp].qur_id;
for (i=;i<query[x].size();i++)
{
if(qur_vis[query[x][i]]!=cat_id)
{
query_gets[query[x][i]]++;
ret++;
qur_vis[query[x][i]]=cat_id;
}
}
}
temp=tree[temp].fail;
} }
return ret;
}
void init()
{
topt=;
tree[].id=-;
root=;
int i,j,x,y;
scanf("%d%d",&n,&m);
for (i=;i<n*;i++)
{
scanf("%d",&x);
name[++topn]=x;
for (j=;j<x;j++)
{
scanf("%d",&y);
name[++topn]=y;
}
name[++topn]=-;
}
for (i=;i<m;i++)
{
scanf("%d",&x);
for (j=;j<x;j++)
{
scanf("%d",&y);
str[j]=y;
}
str[x]=-;
add_str(str,i);
}
build_tree();
memset(qur_vis,-,sizeof(qur_vis));
}
void work()
{
int i;
int now=;
for (i=;i<n*;i++)
{
now++;
cat_gets[i/]+=count_str(&name[now],i/);
now+=name[now-];
now++;
}
for (i=;i<m;i++)
{
printf("%d\n",query_gets[i]);
}
for (i=;i<n-;i++)
{
printf("%d ",cat_gets[i]);
}
printf("%d\n",cat_gets[n-]);
}
int main()
{
init();
work();
return ;
}
BZOJ 2754: [SCOI2012]喵星球上的点名的更多相关文章
- BZOJ 2754: [SCOI2012]喵星球上的点名 [后缀数组+暴力]
2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1906 Solved: 839[Submit][St ...
- BZOJ 2754: [SCOI2012]喵星球上的点名 [AC自动机+map+暴力]
2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1902 Solved: 837[Submit][St ...
- 【刷题】BZOJ 2754 [SCOI2012]喵星球上的点名
Description a180285幸运地被选做了地球到喵星球的留学生.他发现喵星人在上课前的点名现象非常有趣. 假设课堂上有N个喵星人,每个喵星人的名字由姓和名构成.喵星球上的老师会选择M个串来点 ...
- bzoj 2754 [SCOI2012]喵星球上的点名(后缀数组)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2754 [题意] 每只喵有名姓,如果被老师点到名或姓的子串都要答道,但每只喵一次点名只答 ...
- BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机、树状数组)
吐槽: 为啥很多人用AC自动机暴力跳都过了?复杂度真的对么? 做法一: AC自动机+树状数组 姓名的问题,中间加个特殊字符连起来即可. 肯定是对点名串建AC自动机(map存儿子),然后第一问就相当于问 ...
- BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机+map维护Trie树)
题目大意:略 由于字符集大,要用map维护Trie树 并不能用AC自动机的Trie图优化,不然内存会炸 所以我用AC自动机暴跳fail水过的 显然根据喵星人建AC自动机是不行的,所以要根据问题建 然而 ...
- bzoj 2754: [SCOI2012]喵星球上的点名【AC自动机】
洛谷90,最后一个点死活卡不过去(也可能是我写的有问题? 比较暴力的做法,把询问带着标号建立AC自动机,用map存儿子. 然后用名字串在自动机上跑,以为是名或姓的子串就行所以把名和姓中间加个特殊字符拼 ...
- 2754. [SCOI2012]喵星球上的点名【后缀数组】
Description a180285幸运地被选做了地球到喵星球的留学生.他发现喵星人在上课前的点名现象非常有趣. 假设课堂上有N个喵星人,每个喵星人的名字由姓和名构成.喵星球上的老师会选择M个串 ...
- BZOJ2754: [SCOI2012]喵星球上的点名
2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 680 Solved: 314[Submit][Sta ...
随机推荐
- Java用链表实现栈和队列
1.用链表实现栈 package stack; /** * * @author denghb * */ class Link { public long dData; public Link next ...
- [TypeScript] Using Lodash in TypeScript with Typings and SystemJS
One of the most confusing parts of getting started with TypeScript is figuring out how to use all th ...
- [D3] 6. Color Scale
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Android菜单详解(四)——使用上下文菜单ContextMenu
之前在<Android菜单详解(二)——创建并响应选项菜单>和<Android菜单详解(三)——SubMenu和IconMenu>中详细讲解了选项菜单,子菜单和图标菜单.今天接 ...
- LAMP环境部署总结
linux+apche+mysql+php 此次用到的工具有:/etc/init.d/iptables , selinux, useradd, yum,chkconfig,sshd,visudo,cr ...
- Notification与多线程
来源:南峰子(@南峰子_老驴 ) 链接:http://t.cn/R5swQcR 前几天与同事讨论到Notification在多线程下的转发问题,所以就此整理一下. 先来看看官方的文档,是这样写的: I ...
- GUI编程笔记(java)10:GUI实现一级菜单
1.首先:菜单组件 MenuBar,Menu,MenuItem 先创建菜单条,再创建菜单,每一个菜单中建立菜单项. 也可以菜单添加到菜单中,作为子菜 ...
- Kali Linux 2.0: 安装之后的操作
1. 添加官方软件库 1) 编辑/etc/apt/sources.list leafpad /etc/apt/sources.list 2) 用#注释掉原有的内容,并添加下述内容: # Regular ...
- android开发:点击缩略图查看大图
android中点击缩略图查看大图的方法一般有两种,一种是想新浪微博list页面那样,弹出一个窗口显示大图(原activity为背景).另一种就是直接打开一个新的activity显示大图. 1.第一种 ...
- 用Module元素实现SharePoint Webpart Page的自动生成
最近研发的项目中开发了很多的WebPart,每次部署这些WebPart到新环境中总是很麻烦,因为自己要新创建WebpartPage,同时还要把这些WebPart放到指定的WebPart页中去: 为了方 ...