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的更多相关文章

  1. 2434: [Noi2011]阿狸的打字机 - BZOJ

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

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

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

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

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

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

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

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

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

  6. [NOI2011]阿狸的打字机 --- AC自动机 + 树状数组

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

  7. BZOJ2434 [Noi2011]阿狸的打字机 【AC自动机 + fail树 + 树状数组】

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 3610  Solved: 1960 [Submit][S ...

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

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

  9. 【bzoj2434】: [Noi2011]阿狸的打字机 字符串-AC自动机-BIT

    [bzoj2434]: [Noi2011]阿狸的打字机 x串在y串上的匹配次数就是y在自动机所有节点上能够通过fail走到x最后一个节点的个数 (就是y串任意一个前缀的后缀能匹配到x的个数)和[bzo ...

随机推荐

  1. gdb查看内存中所有的信息

    他们会把做内核的人当成无所不能的,认为你们对反编译啥的都应该会. 俗话说的好,人要活成别人想要的样子嘛: 看下如何停止进程,让大家看到内存中到底是啥样子; 简单的print globalA当然能输出来 ...

  2. php静态文件缓存示例

    //开始缓冲区 ob_start(); $cache_file = "./cache/4.3-static.html"; $cache_time = 1; //设置缓存更新时间 i ...

  3. 【bzoj4750】密码安全 单调栈

    题目描述 模10^9+61 输入 第一行包含一个正整数 T ,表示有 T 组测试数据. 接下来依次给出每组测试数据.对于每组测试数据: 第一行包含一个正整数 n . 第二行包含 n 个非负整数,表示 ...

  4. BZOJ4484 JSOI2015最小表示(拓扑排序+bitset)

    考虑在每个点的出边中删除哪些.如果其出边所指向的点中存在某点能到达另一点,那么显然指向被到达点的边是没有用的.于是拓扑排序逆序处理,按拓扑序枚举出边,bitset维护可达点集合即可. #include ...

  5. Linux相关——关于文件调用

    本文主要记录几个常见文件调用(表示为了造数据试了n种方法,,,发现了一些神奇的东西,会在下面一一说明. 首先在程序中我们可以打开和关闭程序. 常见的freopen用法简单,但是只能使用一次,如果在程序 ...

  6. json 串转成 java 对象再拼接成前台 html 元素

    获取商品参数 json 串,转成 java 对象,再拼接成前台 html 的Service方法 @Override public String getItemParam(Long itemId) { ...

  7. python3处理pdf

    https://github.com/1049451037/pdfminer3k 使用pdfminer3k,如果是python2的话直接用pdfminer就行了. python setup.py in ...

  8. Codeforces Global Round 1 (A-E题解)

    Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...

  9. codeforces 110E Lucky Tree

    传送门:https://codeforces.com/contest/110/problem/E 题意:给你一颗树,节点与节点之间的边有一个边权,定义只由4和7组成的数字是幸运数字,现在要你求一共有多 ...

  10. 通俗解释IOC原理

    1. IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机 ...