一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状数组维护, DFS到的查询点就回答询问.时间复杂度O(|ACAM|+QlogQ)

-------------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cctype>
 
using namespace std;
 
#define chk(c) ((c <= 'z' && c >= 'a') || (c =='P') || (c == 'B'))
#define C(c) (c - 'a')
 
const int maxn = 100009;
const int c = 26;
 
inline int read() {
char c = getchar();
for(; !isdigit(c); c = getchar());
int ret = 0;
for(; isdigit(c); c = getchar())
ret = ret * 10 + c - '0';
return ret;
}
 
struct Node {
Node *ch[c], *fail, *par;
int v, d;
Node() : v(0) {
memset(ch, 0, sizeof ch);
fail = par = 0;
}
} pool[maxn], *V[maxn], *pt = pool, *Rt;
 
struct Q {
int d, x, y;
inline void Read(int _d) {
d = _d;
x = read();
y = read();
}
bool operator < (const Q &o) const {
return y < o.y;
}
} q[maxn];
 
int n, dfsn, qn;
int L[maxn], R[maxn], ql[maxn], qr[maxn], ans[maxn];
 
void Init() {
int cnt = dfsn = n = 0;
pt->d = n++;
Rt = pt++;
Node* t = Rt;
char c = getchar();
for(; !chk(c); c = getchar());
for(; chk(c); c = getchar()) {
if(c == 'P') {
V[t->v = ++cnt] = t;
} else if(c == 'B') {
t = t->par;
} else {
if(!t->ch[C(c)]) {
pt->par = t;
pt->d = n++;
t->ch[C(c)] = pt++;
}
t = t->ch[C(c)];
}
}
scanf("%d", &qn);
for(int i = 0; i < qn; i++) 
q[i].Read(i);
sort(q, q + qn);
memset(ql, 0, sizeof(int) * (cnt + 1));
memset(qr, -1, sizeof(int) * (cnt + 1));
for(int i = 0; i < qn; i++) {
if(!i || q[i - 1].y != q[i].y)
ql[q[i].y] = i;
if(i + 1 == qn || q[i + 1].y != q[i].y)
qr[q[i].y] = i;
}
}
 
struct edge {
int to;
edge* next;
} E[maxn << 1], *Pt = E, *head[maxn];
 
inline void AddEdge(int u, int v) {
Pt->to = v;
Pt->next = head[u];
head[u] = Pt++;
}
 
queue<Node*> que;
 
void buildFail() {
que.push(Rt);
while(!que.empty()) {
Node* t = que.front(); que.pop();
if(t->fail)
AddEdge(t->fail->d, t->d);
for(int i = 0; i < c; i++) if(t->ch[i]) {
Node* f = t->fail;
while(f && !f->ch[i])
f = f->fail;
t->ch[i]->fail = f ? f->ch[i] : Rt;
que.push(t->ch[i]);
}
}
}
 
struct BIT {
int b[maxn];
BIT() {
memset(b, 0, sizeof b);
}
inline void Add(int x, int v) {
for(; x <= n; x += x & -x)
b[x] += v;
}
inline int Sum(int x) {
int ret = 0;
for(; x; x -= x & -x)
ret += b[x];
return ret;
}
inline int Query(int l, int r) {
return Sum(r) - Sum(l - 1);
}
} Bit;
 
void DFS(int x) {
L[x] = ++dfsn;
for(edge* e = head[x]; e; e = e->next) DFS(e->to);
R[x] = dfsn;
}
 
void dfsAC(Node* t) {
Bit.Add(L[t->d], 1);
if(t->v) {
for(int i = ql[t->v]; i <= qr[t->v]; i++)
ans[q[i].d] = Bit.Query(L[V[q[i].x]->d], R[V[q[i].x]->d]);
}
for(int i = 0; i < c; i++)
if(t->ch[i]) dfsAC(t->ch[i]);
Bit.Add(L[t->d], -1);
}
 
int main() {
Init();
buildFail();
DFS(0);
dfsAC(Rt);
for(int i = 0; i < qn; i++)
printf("%d\n", ans[i]);
return 0;
}

-------------------------------------------------------------------------------------------

2434: [Noi2011]阿狸的打字机

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 1706  Solved: 974
[Submit][Status][Discuss]

Description

阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机。打字机上只有28个按键,分别印有26个小写英文字母和'B'、'P'两个字母。

经阿狸研究发现,这个打字机是这样工作的:

l 输入小写字母,打字机的一个凹槽中会加入这个字母(这个字母加在凹槽的最后)。

l 按一下印有'B'的按键,打字机凹槽中最后一个字母会消失。

l 按一下印有'P'的按键,打字机会在纸上打印出凹槽中现有的所有字母并换行,但凹槽中的字母不会消失。

例如,阿狸输入aPaPBbP,纸上被打印的字符如下:

a

aa

ab

我们把纸上打印出来的字符串从1开始顺序编号,一直到n。打字机有一个非常有趣的功能,在打字机中暗藏一个带数字的小键盘,在小键盘上输入两个数(x,y)(其中1≤x,y≤n),打字机会显示第x个打印的字符串在第y个打印的字符串中出现了多少次。

阿狸发现了这个功能以后很兴奋,他想写个程序完成同样的功能,你能帮助他么?

Input

输入的第一行包含一个字符串,按阿狸的输入顺序给出所有阿狸输入的字符。

第二行包含一个整数m,表示询问个数。

接下来m行描述所有由小键盘输入的询问。其中第i行包含两个整数x, y,表示第i个询问为(x, y)。

Output

输出m行,其中第i行包含一个整数,表示第i个询问的答案。

Sample Input

aPaPBbP

3

1 2

1 3

2 3

Sample Output

2

1

0

HINT

1<=N<=10^5

1<=M<=10^5

输入总长<=10^5

Source

BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )的更多相关文章

  1. 【BZOJ2434】[NOI2011]阿狸的打字机 AC自动机+DFS序+树状数组

    [BZOJ2434][NOI2011]阿狸的打字机 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P ...

  2. BZOJ2434[Noi2011]阿狸的打字机——AC自动机+dfs序+树状数组

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

  3. NOI 2011 阿狸的打字机 (AC自动机+dfs序+树状数组)

    题目大意:略(太长了不好描述) 良心LOJ传送门 先对所有被打印的字符串建一颗Trie树 观察数据范围,并不能每次打印都从头到尾暴力建树,而是每遍历到一个字符就在Trie上插入这个字符,然后记录每次打 ...

  4. BZOJ 2434 阿狸的打字机(ac自动机+dfs序+树状数组)

    题意 给你一些串,还有一些询问 问你第x个串在第y个串中出现了多少次 思路 对这些串建ac自动机 根据fail树的性质:若x节点是trie中root到t任意一个节点的fail树的祖先,那么x一定是y的 ...

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

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

  6. BZOJ_2434_[NOI2011]_阿狸的打字机_(AC自动机+dfs序+树状数组)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=2434 给出\(n\)个字符串,\(m\)个询问,对于第\(i\)个询问,求第\(x_i\)个字 ...

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

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

  8. BZOJ2434: [NOI2011]阿狸的打字机(AC自动机+dfs序+树状数组)

    [NOI2011]阿狸的打字机 题目链接:https://www.luogu.org/problemnew/show/P2414 题目背景 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机. ...

  9. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

随机推荐

  1. 固定cell.imageView.image的大小

    cell.imageView.image的大小 会随着Cell的高度而变化,不同的图片显示的也不一样,在网上找了几种方法,简单方便的是下面这种: UIImage *icon = [UIImage im ...

  2. UITabBarController 笔记(二) ViewController中加UITabBarController

    新建一个简单视图iOS工程,在ViewController的viewDidLoad中代码如下 - (void)viewDidLoad { [super viewDidLoad]; // Do any ...

  3. 扩展第三方DropDownMenu

    找工作之际,静下心总结工作中的想法. 我的简书 原来的效果 Paste_Image.png #解析结构 导读 想要扩展首先我需要执行下面几个步骤 1.fork DropDownMenu到自己的gith ...

  4. Windows Server 2008 安装好之后的简单配置

    1.禁用密码复杂度 在运行中输入GPEDIT.MSC 打开组策略,找到计算机配置->Windows设置->安全设置->账户策略中的密码策略,将“密码必须符合复杂性要求”设置为禁用即可 ...

  5. Mac浏览器全屏设置

    在 mac 升级之后,以往点击放大的按钮,现在显示的效果是全屏:

  6. FPGA开发(3)

    转载 榨干FPGA片上存储资源 记得Long long time ago,特权同学写过一篇简短的博文<M4K使用率>,文章中提到了Cyclone器件的内嵌存储块M4K的配置问题.文中提到了 ...

  7. C#中一个问号和两个问号(a ?? b)的作用

    不卖关子,直接开门见山: C#中两个问号的作用是判断??左边的对象是否为null,如果不为null则使用??左边的对象,如果为null则使用??右边的对象. 比如:a = b ?? c,如果b为nul ...

  8. 在webstrorm中配置好es6 babel

    第一步,新建一个项目,我这里建立了基于express 的node项目 第二步:将JavaScript语言版本切换为ECMAScript6 点击File —>settings,弹出设置框.把js的 ...

  9. caret彻底的理解css的三角形【通过border】

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Struts2配置问题java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

    方法一:右键点击项目--->build path-->configure build path-->左侧菜单栏就会看到Deployment Assembly-->右侧点击add ...