bzoj2434
利用了bzoj3172提到的性质,x串在y串中的出现的次数即为在fail树上以x结尾节点为根的子树中有多少个节点在y串上
所以很明显我们要离线解决,我们先把询问按y分类存起来
然后我们顺着操作顺序来,出现一个字符就把fail树上对应节点标为1,删除之后就改为0;
当一个串输出之后,我们就统计跟他有关的询问(查询x串结尾节点子树和)
这种问题显然用dfs序+树状数组解决
const maxn=;
type node=record
y,next:longint;
end; var i,j,n,m,len,tot,all,k,ll,rr,num,x,y,p:longint;
t:array[..maxn,'a'..'z'] of longint;
q,pre,f,v,l,d,h,r,h2,ans,st,sum:array[..maxn] of longint;
g,g2:array[..maxn] of node;
c:char;
s:array[..maxn] of char; procedure add(x,y:longint);
begin
inc(num);
g[num].y:=y;
g[num].next:=h[x];
h[x]:=num;
end; procedure ac;
begin
fillchar(q,sizeof(q),);
ll:=;
rr:=;
for c:='a' to 'z' do
if t[,c]> then
begin
add(,t[,c]);
inc(rr);
q[rr]:=t[,c];
end;
while ll<>rr do
begin
inc(ll);
i:=q[ll];
for c:='a' to 'z' do
if t[i,c]> then
begin
k:=t[i,c];
inc(rr);
q[rr]:=k;
j:=f[i];
while (j>) and (t[j,c]=) do j:=f[j];
f[k]:=t[j,c];
add(t[j,c],k);
end;
end;
end; procedure dfs(x:longint);
var p:longint;
begin
inc(tot);
l[x]:=tot;
p:=h[x];
while p<> do
begin
dfs(g[p].y);
p:=g[p].next;
end;
r[x]:=tot;
end; procedure ins(x,y:longint);
begin
g2[i].y:=y;
g2[i].next:=h2[x];
h2[x]:=i;
end; procedure change(x,y:longint);
begin
while x<=tot do
begin
inc(sum[x],y);
inc(x,x and -x);
end;
end; function get(x:longint):longint;
begin
get:=;
while x> do
begin
inc(get,sum[x]);
dec(x,x and -x);
end;
end; procedure main;
begin
readln(n);
for i:= to n do
begin
readln(x,y);
ins(y,x);
end;
j:=;k:=;m:=;
for i:= to len do
begin
case s[i] of
'B':begin change(l[st[k]],-);dec(k);j:=pre[j];end;
'P':
begin
inc(m);p:=h2[m];
while p<> do
begin
ans[p]:=get(r[d[g2[p].y]])-get(l[d[g2[p].y]]-);
p:=g2[p].next;
end;
end;
else begin
j:=t[j,s[i]];
inc(k);st[k]:=j;
change(l[j],);
end;
end;
end;
for i:= to n do writeln(ans[i]);
end; begin
j:=;
while not eoln do
begin
inc(len);
read(s[len]);
case s[len] of
'B':begin j:=pre[j];end;
'P':begin inc(tot);d[tot]:=j;v[j]:=;end;
else begin
if t[j,s[len]]= then
begin
inc(all);
t[j,s[len]]:=all;
pre[all]:=j;
end;
j:=t[j,s[len]];
end;
end;
end;
readln;
ac;
tot:=;
dfs();
main;
end.
bzoj2434的更多相关文章
- 【BZOJ2434】阿狸的打字机(AC自动机,树状数组)
[BZOJ2434]阿狸的打字机(AC自动机,树状数组) 先写个暴力: 每次打印出字符串后,就插入到\(Trie\)树中 搞完后直接搭\(AC\)自动机 看一看匹配是怎么样的: 每次沿着\(AC\)自 ...
- 【BZOJ2434】【NOI2011】阿狸的打字机(AC自动机,树状数组)
[BZOJ2434]阿狸的打字机(AC自动机,树状数组) 先写个暴力: 每次打印出字符串后,就插入到\(Trie\)树中 搞完后直接搭\(AC\)自动机 看一看匹配是怎么样的: 每次沿着\(AC\)自 ...
- 【bzoj2434】 Noi2011—阿狸的打字机
http://www.lydsy.com/JudgeOnline/problem.php?id=2434 (题目链接) 题意 给出一个字符串,$P$表示输出,$B$表示退格.$m$组询问$(x,y)$ ...
- 【BZOJ2434】[NOI2011]阿狸的打字机 AC自动机+DFS序+树状数组
[BZOJ2434][NOI2011]阿狸的打字机 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P ...
- 【bzoj2434】: [Noi2011]阿狸的打字机 字符串-AC自动机-BIT
[bzoj2434]: [Noi2011]阿狸的打字机 x串在y串上的匹配次数就是y在自动机所有节点上能够通过fail走到x最后一个节点的个数 (就是y串任意一个前缀的后缀能匹配到x的个数)和[bzo ...
- 【BZOJ2434】阿狸的打字机(fail树,DFS序)
题意: 1<=N<=10^5 1<=M<=10^5 输入总长<=10^5 思路: From http://blog.csdn.net/lych_cys/article ...
- Bzoj2434 [Noi2011]阿狸的打字机
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2536 Solved: 1415 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到 ...
- BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)
题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...
- 【BZOJ-2434】阿狸的打字机 AC自动机 + Fail树 + DFS序 + 树状数组
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2022 Solved: 1158[Submit][Sta ...
随机推荐
- jdk在windows中的配置
1.下载jdk(java developer kit),其内部包含jre(java runtime environment): 安装解压缩到一盘内,如:G:\Program Files\Java: 2 ...
- 学习笔记_过滤器应用(粗粒度权限控制(拦截是否登录、拦截用户名admin权限))
RBAC ->基于角色的权限控制 l tb_user l tb_role l tb_userrole l tb_menu(增.删.改.查) l tb_rolemenu 1 说明 我们给 ...
- LINQ2EF-LINQ2SQL-LINQ笔记
例1:In SQL: AND dep_all_code LIKE '" + depAll + "' AND dep_code in (SELECT DISTINCT DEP3 FR ...
- bzoj3571: [Hnoi2014]画框 最小乘积匹配+最小乘积XX总结,
思路大概同bzoj2395(传送门:http://www.cnblogs.com/DUXT/p/5739864.html),还是将每一种匹配方案的Σai看成x,Σbi看成y,然后将每种方案转化为平面上 ...
- Andriod 中常见错误
1.Open quote is expected for attribute "android:name" associated with an element type &quo ...
- github 查看单个文件的历史记录命令
gitk 安装: apt-get install gitk 点击打开链接http://stackoverflow.com/questions/278192/view-the-change-histor ...
- linux ptheard 生产者消费者
; { { printf( pthread_mutex_lock(&mutex); != g_iBufSiz ...
- 设置Android程序的默认安装位置
修改 AndroidManifest.xml 文件: <manifest xmlns:android="http://schemas.android.com/apk/res/andro ...
- Poco版信号槽
#include "Poco/BasicEvent.h"#include "Poco/Delegate.h"#include <iostream> ...
- App 性能分析
关键因素: ---- Instrument 性能分析神器 1. 启动时间 应用启动时间长短对用户第一次体验至关重要,同时系统对应用的启动.恢复等状态的运行时间也有严格的要求,在应用超时的情况下系统会直 ...