题意

给你一些字符串。\(m\)次询问,每一次询问第\(x\)个字符串在\(y\)字符串中出现了多少次。(输入总长$ \le 10^5$, \(M \le 10^5\))

分析

在ac自动机上,\(x\)字符串出现的所有位置就是其它节点的fail树上有这个节点的节点。即fail树中,\(x\)字符串终止节点的子树。

题解

根据分析,我们只要构造ac自动机和fail树,按dfs序依次便历ac自动机,将节点到根的路径打上标记。每到一个终止节点,就更新以这个节点为模板的匹配串的答案,即这些节点子树的和。

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
char buf[20*N], *is=buf;
int ihead[N], cnt, last[N], c[N][26], fail[N], pos[N], FF[N], LL[N], tot, su[N], ans[N];
inline bool isdig(const char &c) {
return c>='0'&&c<='9';
}
inline bool isok(const char &c) {
return (c>='a'&&c<='z')||c=='P'||c=='B';
}
inline int getint() {
register int x=0;
for(; !isdig(*is); ++is);
for(; isdig(*is); ++is) {
x=x*10+*is-48;
}
return x;
}
inline void putint(int x) {
if(x==0) {
*is++='0';
}
else {
static int s[10], top;
for(top=0; x; x/=10) s[++top]=x%10;
while(top) {
*is++=s[top--]+'0';
}
}
*is++='\n';
}
struct E {
int next, to, id;
}e[N];
inline void add(int x, int y, int id=-1) {
e[++cnt]=(E){ihead[x], y, id}; ihead[x]=cnt;
}
inline void adds(int x, int s) {
for(; x<=tot; x+=x&-x) {
su[x]+=s;
}
}
inline int sum(int x) {
int y=0;
for(; x; x-=x&-x) {
y+=su[x];
}
return y;
}
inline void bfs() {
static int q[N], fr, ta;
fr=ta=0;
q[ta++]=0;
while(fr!=ta) {
int x=q[fr++];
for(int ch=0; ch<26; ++ch) {
if(c[x][ch]) {
int y=c[x][ch];
q[ta++]=y;
if(x==0) {
add(0, y);
continue;
}
fail[y]=c[fail[x]][ch];
add(fail[y], y);
}
else {
c[x][ch]=c[fail[x]][ch];
}
}
}
}
inline void dfs(int x) {
static int fid=0;
FF[x]=++fid;
for(int i=ihead[x]; i; i=e[i].next) {
dfs(e[i].to);
}
LL[x]=fid;
}
inline void getans(int x) {
adds(FF[x], 1);
for(int i=ihead[x]; i; i=e[i].next) {
int y=e[i].to;
ans[e[i].id]=sum(LL[y])-sum(FF[y]-1);
}
for(int ch=0; ch<26; ++ch) {
if(c[x][ch] && (x==0 || c[x][ch]!=c[fail[x]][ch])) {
getans(c[x][ch]);
}
}
adds(FF[x], -1);
}
void init() {
fread(buf, 1, sizeof buf, stdin);
for(; !isok(*is); ++is);
int now=0, n=0, len=0;
for(; isok(*is); ++is) {
if(*is=='P') {
pos[++n]=now;
}
else if(*is=='B') {
now=last[len--];
}
else {
int ch=*is-'a';
if(!c[now][ch]) {
c[now][ch]=++tot;
}
last[++len]=now;
now=c[now][ch];
}
}
bfs();
dfs(0);
}
int main() {
init();
memset(ihead, 0, sizeof(int)*(tot+1));
cnt=0;
int m=getint();
for(int i=0; i<m; ++i) {
int x, y;
x=getint();
y=getint();
add(pos[y], pos[x], i);
}
++tot;
getans(0);
is=buf;
for(int i=0; i<m; ++i) {
putint(ans[i]);
}
fwrite(buf, 1, sizeof(char)*(is-buf), stdout);
return 0;
}

【BZOJ】2434: [Noi2011]阿狸的打字机的更多相关文章

  1. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  2. BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )

    一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...

  3. bzoj 2434 [Noi2011]阿狸的打字机 AC自动机

    [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4001  Solved: 2198[Submit][Status][D ...

  4. 【刷题】BZOJ 2434 [Noi2011]阿狸的打字机

    Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...

  5. BZOJ 2434: [Noi2011]阿狸的打字机 AC自动机+fail树+线段树

    Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...

  6. bzoj 2434 [Noi2011]阿狸的打字机(fail树+离线处理+BIT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2434 [题意] 按照一定规则生成n个字符串,回答若干个询问:(x,y),问第x个字符串 ...

  7. BZOJ 2434 [Noi2011]阿狸的打字机(AC自动机)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2434 [题目大意] 给出一个打印的过程,'a'-'z'表示输入字母,P表示打印该字符串 ...

  8. bzoj 2434 [Noi2011]阿狸的打字机——AC自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2434 dfs AC自动机,走过的点权值+1,回溯的时候权值-1:走到询问的 y 串的节点,看 ...

  9. bzoj 2434: [Noi2011]阿狸的打字机

    #include<cstdio> #include<iostream> #include<cstring> #define M 100008 using names ...

  10. ●BZOJ 2434: [Noi2011]阿狸的打字机

    ●赘述题目 (题意就不赘述了) ●解法: ●我先想的一个比较暴力的方法(要TLE): (ac自动机)先求出last数组(参见刘汝佳的解释:last[j]:表示j节点沿着失配指针往回走时,遇到的下一个单 ...

随机推荐

  1. Linux系统启动过程分析

    [原创]Linux系统启动过程分析-wjlkoorey258-ChinaUnix博客http://blog.chinaunix.net/uid-23069658-id-3142047.html 经过对 ...

  2. Delphi集合的用法

    参考:http://www.cnblogs.com/doit8791/archive/2012/08/17/2644859.html 集合是Pascal特有的数据类型,在Visual Basic.C/ ...

  3. GoLang搞一个基本的HTTP服务

    慢慢和python的对应一下看看. package main import ( "fmt" "net/http" "strings" &qu ...

  4. C#屏幕截图

    今天通过C#来实现一个简单的屏幕截图功能.实现思路,获取鼠标按下去的位置和鼠标左键释放的位置,计算这个区域的宽度和高度.然后通过 Graphics.CopyFromScreen 方法便可以获取到屏幕截 ...

  5. Fragment详解之三——管理Fragment(1)

    相关文章: 1.<Fragment详解之一--概述>2.<Fragment详解之二--基本使用方法>3.<Fragment详解之三--管理Fragment(1)>4 ...

  6. HDU 5875 Function (2016年大连网络赛 H 线段树+gcd)

    很简单的一个题的,结果后台数据有误,自己又太傻卡了3个小时... 题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对 ...

  7. loadrunner资源过滤器

    通过该功能可以实现排除某个资源,很实用 Download Filters功能 帮助在回放脚本的时候对某些特定的访问进行屏蔽,解决页面读取中跨服务器带来数据影响的问题. 过滤规则中有3中策略,即URL. ...

  8. linux查看和修改系统时间

    设置日期:date -s 20091112 设置时间:date -s 18:30:50 日期和时间一起设置: date 111218302009 (月日时分年) date -s "20091 ...

  9. C#的Enum——枚举

    枚举 枚举类型声明为一组相关的符号常数定义了一个类型名称.枚举用于“多项选择”场合,就是程序运行时从编译时已经设定的固定数目的“选择”中做出决定. 枚举类型(也称为枚举)为定义一组可以赋给变量的命名整 ...

  10. Attribute在.net编程中的应用

    Attribute FYI Link: Attribute在.net编程中的应用(一) Attribute在.net编程中的应用(二) Attribute在.net编程中的应用(三) Attribut ...