AC日记——[Noi2011]阿狸的打字机 bzoj 2434
思路:
构建ac自动机;
抽离fail树;
根据字符串建立主席树;
在线处理询问;
询问x在y中出现多少次,等同于y有多少字母的fail能走到x;
1a,hahahahah;
代码:
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 100005 struct TreeNodeType {
int lc,rc,dis;
};
struct TreeNodeType tree[maxn*]; int n,ch[maxn][],ff[maxn],fail[maxn],tot;
int bel[maxn],bel_[maxn],pn,pla[maxn],head[maxn];
int E[maxn<<],V[maxn<<],cnt,id[maxn],end[maxn];
int root[maxn],tot_,m,times[maxn],sta[maxn],top; char str[maxn]; queue<int>que; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} void dfs1(int now,int fa)
{
id[now]=++cnt;
for(int i=head[now];i;i=E[i])
{
if(V[i]==fa) continue;
dfs1(V[i],now);
}
end[now]=cnt;
} void tree_build(int &now,int l,int r)
{
now=++tot_;
if(l==r) return ;
int mid=l+r>>;
tree_build(tree[now].lc,l,mid);
tree_build(tree[now].rc,mid+,r);
} void tree_add(int pre,int &now,int l,int r,int to)
{
now=++tot_;
tree[now].dis=tree[pre].dis+;
if(l==r) return ;
int mid=l+r>>;
if(to<=mid) tree_add(tree[pre].lc,tree[now].lc,l,mid,to),tree[now].rc=tree[pre].rc;
else tree_add(tree[pre].rc,tree[now].rc,mid+,r,to),tree[now].lc=tree[pre].lc;
} int tree_query(int now,int l,int r,int ll,int rr)
{
if(l==ll&&rr==r) return tree[now].dis;
int mid=l+r>>;
if(ll>mid) return tree_query(tree[now].rc,mid+,r,ll,rr);
else if(rr<=mid) return tree_query(tree[now].lc,l,mid,ll,rr);
else return tree_query(tree[now].lc,l,mid,ll,mid)+tree_query(tree[now].rc,mid+,r,mid+,rr);
} int main()
{
scanf("%s",str),ff[]=,tot=;
int len=strlen(str),now=,pos,temp;n=len;
for(int i=;i<len;i++)
{
if(str[i]=='P')
{
bel[i]=now,pla[++pn]=now;
continue;
}
if(str[i]=='B')
{
now=ff[now];
bel[i]=now;
continue;
}
pos=str[i]-'a',bel_[i]=now;
if(!ch[now][pos]) ch[now][pos]=++tot,ff[ch[now][pos]]=now;
now=ch[now][pos],bel[i]=now;
}
que.push();int u,v;
while(!que.empty())
{
now=que.front(),que.pop();
for(int i=;i<;i++)
{
if(!ch[now][i]) continue;
que.push(ch[now][i]);
if(now==) fail[ch[now][i]]=;
else
{
temp=fail[now];
while(temp)
{
if(ch[temp][i])
{
fail[ch[now][i]]=ch[temp][i];
break;
}
temp=fail[temp];
}
if(!temp) fail[ch[now][i]]=;
}
u=ch[now][i],v=fail[ch[now][i]];
E[++cnt]=head[u],V[cnt]=v,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,head[v]=cnt;
}
}
cnt=,dfs1(,),tree_build(root[],,n),now=,pn=;
for(int i=;i<len;i++)
{
if(str[i]=='B') top--;
else if(str[i]=='P') times[++pn]=sta[top];
else
{
tree_add(root[sta[top]],root[i+],,n,id[bel[i]]);
sta[++top]=i+;
}
}
in(m);
for(int i=;i<=m;i++)
{
in(u),in(v);
printf("%d\n",tree_query(root[times[v]],,n,id[pla[u]],end[pla[u]]));
}
return ;
}
AC日记——[Noi2011]阿狸的打字机 bzoj 2434的更多相关文章
- 2434: [Noi2011]阿狸的打字机 - BZOJ
Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )
一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...
- bzoj 2434 [Noi2011]阿狸的打字机 AC自动机
[Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4001 Solved: 2198[Submit][Status][D ...
- 【BZOJ2434】[NOI2011]阿狸的打字机 AC自动机+DFS序+树状数组
[BZOJ2434][NOI2011]阿狸的打字机 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P ...
- [NOI2011]阿狸的打字机 --- AC自动机 + 树状数组
[NOI2011] 阿狸的打字机 题目描述: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机. 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现, ...
- BZOJ2434 [Noi2011]阿狸的打字机 【AC自动机 + fail树 + 树状数组】
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MB Submit: 3610 Solved: 1960 [Submit][S ...
- BZOJ2434: [NOI2011]阿狸的打字机(AC自动机+dfs序+树状数组)
[NOI2011]阿狸的打字机 题目链接:https://www.luogu.org/problemnew/show/P2414 题目背景 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机. ...
- 【bzoj2434】: [Noi2011]阿狸的打字机 字符串-AC自动机-BIT
[bzoj2434]: [Noi2011]阿狸的打字机 x串在y串上的匹配次数就是y在自动机所有节点上能够通过fail走到x最后一个节点的个数 (就是y串任意一个前缀的后缀能匹配到x的个数)和[bzo ...
随机推荐
- winspool.drv
public partial class Form1 : Form{ [System.Runtime.InteropServices.DllImportAttribute("winspool ...
- RadioGroup 的使用
//获取 RadioGroup 项目名称 procedure TForm1.RadioGroup1Click(Sender: TObject); begin Text := RadioGroup1 ...
- doget,doPost在底层走的是service
doget,doPost在底层走的是service 因为在源码上 先执行service方法 然后再调用doget,doPost方法
- P2671 求和
题目描述 一条狭长的纸带被均匀划分出了 nn 个格子,格子编号从 11 到 nn .每个格子上都染了一种颜色 color\_icolor_i 用 [1,m][1,m] 当中的一个整数表示),并且写了一 ...
- [NOIP2017 TG D2T3]列队
题目大意:有一个$n \times m$的方阵,第$i$行第$j$列的人的编号是$(i-1) \times m + j$. 现在有$q$个出列操作,每次让一个人出列,然后让这个人所在行向左看齐,再让最 ...
- BZOJ1857 [Scoi2010]传送带 【三分法】
题目链接 BZOJ1857 题解 画画图就发现实际上是在\(AB\)上和\(CD\)上分别选两个点\(E\),\(F\),使得\(t_{AE} + t_{EF} + t_{FD}\)最小 然后猜想到当 ...
- uva10884 Persephone
题目戳这里. 找规律. 每一列占据的格子一定是一段区间: 相邻列之间的区间有交. 上界先增后减,下界先减后增. \(f_{i,j,k,0/1,0/1}\)表示考虑前\(i\)列,第\(i\)列,上界为 ...
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- poj3683 2-sat Priest John's Busiest Day
Description John is the only priest in his town. September 1st is the John's busiest day in a year b ...
- namesilo注册域名用来做域名邮箱
重要的话说三遍: (一定不要再国内注册域名,不要买国内的空间) (一定不要再国内注册域名,不要买国内的空间) (一定不要再国内注册域名,不要买国内的空间) 使用的是腾讯企业邮箱,有一个缺点:不支持自定 ...