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. 编译程序提示配置PKG_CONFIG_PATH

    http://blog.csdn.net/langeldep/article/details/6804331 在安装开源软件的过程中, 经常会碰到提示配置PKG_CONFIG_PATH路径, 或者直接 ...

  2. 【bzoj2287】[POJ Challenge]消失之物 背包dp

    题目描述 ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. “要使用剩下的 N - 1 物品装满容积为 x 的背包,有几种方法呢? ...

  3. 图片和byte[]的互相转换

    //图片的"读"操作 //①参数是图片路径:返回Byte[]类型: //参数是图片的路径 public byte[] GetPictureData(string imagePath ...

  4. BZOJ4563 HAOI2016放棋子(高精度)

    没看清题还以为是要求数最大匹配数量……注意到任意障碍不在同一行同一列,且恰好有n个障碍,不妨通过交换列使得第i行第i列均有障碍.那么就是个错排了.居然wa了一发简直没救. #include<io ...

  5. perl的Sys::Syslog模块(openlog,syslog,closelog函数,setlogsock)-自定义日志

    perl的Sys::Syslog模块(openlog,syslog,closelog函数,setlogsock)-自定义日志 http://blog.chinaunix.net/xmlrpc.php? ...

  6. IIS 发布后无法连接数据库(应用池问题)

    查找网站对应的  应用池,修改为 .net4.0  然后设置启动32位应用程序为 True

  7. 洛谷P3806 【模板】点分治1 【点分治】

    题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入输出格式 输入格式: n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径 接 ...

  8. [NOIP2012] 文化之旅 dfs

    这道题就体现了聪明的搜索策略的重要性,如果我们正着搜,判断效率会明显下滑,所以我们就采用倒着搜索.(其实很玄学.....) #include <cstdio> #include <b ...

  9. mmall项目之问题一(mavenplugin问题)

    在进行mybatis逆向工程到时候,报错,提示maven plugin 错误,提示missing..... 解决办法: 因为之前到pom中忘记了加版本信息,添加后错误消失:

  10. 7月23号day15总结

    数据清洗完成之后开始编写前端,通过spring框架将清洗后数据库中的数据显示在页面中. 框架的搭建和js的使用都在学习阶段,