完全不会啊,看题解还看了好久,我是蒟蒻$QAQ$

$zyf$的题解挺好的:http://blog.csdn.net/clove_unique/article/details/51059425

$fail树$的性质完全不知道啊,只好现学,,,

根据读入的顺序来扫$Trie树$并更新树状数组真的好神!我一辈子都想不出来$TwT$

还有$dfs序$那里我还想了好久,,,聪哥说我的表情一脸懵逼

最终看(抄)题解做出来了,用了$4h+$,我好制杖

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 200003
#define lowbit(x) (x & (-x))
#define read(x) x=getint()
using namespace std;
inline int getint() {
int k = 0, fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = k * 10 + c - '0';
return k * fh;
}
struct node {
int nxt, to;
} E[N * 26];
int fa[N], watch[N], c[N][26], cnt = 0, tot = 0, point[N];
int fail[N], L[N], R[N], C[N], m, ans[N];
inline void _(char *s) {
int len = strlen(s), now = 0;
for(int i = 0; i < len; ++i){
if (s[i] >= 'a' && s[i] <= 'z') {
int t = s[i] - 'a';
if (!c[now][t])
c[now][t] = ++cnt;
fa[cnt] = now;
now = cnt;
}else
if (s[i] == 'P')
watch[++tot] = now;
else
now = fa[now];
}
}
inline void ins(int x, int y) {E[++cnt].nxt = point[x]; E[cnt].to = y; point[x] = cnt;}
inline void __() {
cnt = 0;
queue <int> q;
for(int t = 0; t < 26; ++t)
if (c[0][t])
q.push(c[0][t]), ins(0, c[0][t]);
while (!q.empty()) {
int now = q.front();
q.pop();
for(int t = 0; t < 26; ++t)
if (!c[now][t])
c[now][t] = c[fail[now]][t];
else {
int u = c[now][t];
fail[u] = c[fail[now]][t];
ins(fail[u], u);
q.push(u);
}
}
}
inline void ___(int x) {
L[x] = ++cnt;
for(int tmp = point[x]; tmp; tmp = E[tmp].nxt)
___(E[tmp].to);
R[x] = cnt;
} inline void inc(int pos, int num) {
for(; pos <= cnt; pos += lowbit(pos))
C[pos] += num;
}
inline int sum(int pos) {
int s = 0;
for(; pos > 0; pos -= lowbit(pos))
s += C[pos];
return s;
} struct nnode {
int x, y, id;
} A[N];
inline bool cmp(nnode n1, nnode n2) {return n1.y < n2.y;}
int main() {
char s[N << 1];
scanf("%s", s);
_(s);
__(); cnt = 0;
___(0); read(m);
for(int i = 1; i <= m; ++i)
read(A[i].x), read(A[i].y), A[i].id = i;
sort(A + 1, A + m + 1, cmp); int len = strlen(s), now = 0, con =1;
tot = 0;
for(int i = 0; i < len; ++i) {
if (s[i] >= 'a' && s[i] <= 'z')
now = c[now][s[i] - 'a'], inc(L[now], 1);
else
if (s[i] == 'P') {
if (++tot == A[con].y) {
for(; tot == A[con].y; ++con)
ans[A[con].id] = sum(R[watch[A[con].x]]) - sum(L[watch[A[con].x]] - 1);
}
} else
inc(L[now], -1), now = fa[now];
} for(int i = 1; i <= m; ++i)
printf("%d\n", ans[i]);
return 0;
}

进度这么慢,省选爆零节奏(=゚ω゚)ノ

【BZOJ 2434】【NOI 2011】阿狸的打字机 fail树的更多相关文章

  1. 【BZOJ 2434】 [Noi2011]阿狸的打字机 fail树+树状数组

    就是考了一个fail树的神奇应用我们建出fail树之后,发现我们就是在求y到根的路径上所有的点在以x为根的子树里的个数,这个我们离线后用树状数组+dfs序即可解决 #include <cstdi ...

  2. bzoj 2434: 阿狸的打字机 fail树+离线树状数组

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=2434 题解: 首先我们可以发现这个打字的过程本身就是在Trie上滚来滚去的过程 所以我们 ...

  3. bzoj 2434 阿狸的打字机 fail树的性质

    如果a串是另b串的后缀,那么在trie图上沿着b的fail指针走一定可以走到a串. 而a串在b串里出现多少次就是它是多少个前缀的后缀. 所以把fail边反向建树维护个dfs序就行了. 并不是很难... ...

  4. [NOI 2011]阿狸的打字机

    Description 题库链接 给你 \(n\) 个单词, \(m\) 组询问,每组询问形同 \((x,y)\) ,询问 \(x\) 串在 \(y\) 串中出现多少次. \(1\leq n,m\le ...

  5. NOI 2011 阿狸的打字机(AC自动机+主席树)

    题意 https://loj.ac/problem/2444 思路 ​多串匹配,考虑 \(\text{AC}\) 自动机.模拟打字的过程,先建出一棵 \(\text{Trie}\) 树,把它变成自动机 ...

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

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

  7. BZOJ2434 [NOI2011] 阿狸的打字机 【树链剖分】【线段树】【fail树】【AC自动机】

    题目分析: 画一下fail树,就会发现就是x的子树中属于y路径的,把y剖分一下,用线段树处理 $O(n*log^2 n)$. 代码: #include<bits/stdc++.h> usi ...

  8. BZOJ 3172 [Tjoi2013]单词 AC自己主动机(fail树)

    题意:链接 方法:AC自己主动机与fail树性质 解析:复习AC自己主动机的第一道题?(真正的第一题明明是又一次写了遍hdu2222! ) 这题说实话第一眼看上去就是个sb题,仅仅要建出来自己主动机. ...

  9. 「BZOJ 2434」「NOI 2011」阿狸的打字机「AC自动机」

    题意 有一个打字机,支持三种操作: 字符串末尾加一个小写字母 字符串末尾减一个字符 输出这个字符串 经过不超过\(n\)次操作后有\(m\)组询问:\((x,y)\),表示第\(x\)次输出第字符串在 ...

随机推荐

  1. Codeforces 549C. The Game Of Parity[博弈论]

    C. The Game Of Parity time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. AC日记——回文子串 openjudge 1.7 34

    34:回文子串 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个字符串,输出所有长度至少为2的回文子串. 回文子串即从左往右输出和从右往左输出结果是一样的字符串,比如:abb ...

  3. Unity 5 WebGL vs Web Player

    起原 Unity5.3中看到Web Player未来将到被取消,根据Unity官方blog中称Unity5.4中将会移除web player. 本文从我知道的知识比较一下webPlayer和WebGL ...

  4. 给ros安装arbotix simulator仿真环境

    首先下载程序包.编译.安装. cd ~/catkin_ws/src git clone https://github.com/pirobot/rbx1.git cd rbx1 git checkout ...

  5. poj1573 Robot Motion

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12507   Accepted: 6070 Des ...

  6. 关于ubuntu的sources.list总结

    一.作用 文件/etc/apt/sources.list是一个普通可编辑的文本文件,保存了ubuntu软件更新的源服务器的地址.和sources.list功能一样的是/etc/apt/sources. ...

  7. zlog学习笔记(mdc)

    mdc.h #ifndef __zlog_mdc_h #define __zlog_mdc_h #include "zc_defs.h" typedef struct zlog_m ...

  8. codevs 2801 LOL-盖伦的蹲草计划

     时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题目描述 Description 众所周知,LOL这款伟大的游戏,有个叫盖伦的英雄.他的伟大之处在于他特别喜欢蹲 ...

  9. git push ERROR: missing Change-Id in commit message footer

    今天上传代码时候报告错误:$ git push origin HEAD:refs/for/branch*Counting objects: 7, done.Delta compression usin ...

  10. jboss的时区问题

    默认情况下,jboss启动时,使用的时区是“+0:00”区,而中国所在的时间为"+8:00"区(所谓的东8区),最终java取当前时间时,总比北京时间慢8个小时 解决办法: 新建一 ...