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. java csv list cant not repeat

    require: /** * before: * file A1.csv {1,2,3,4,5} * file A2.csv {2,3,9,10,11} * file B1.csv {5,12,13, ...

  2. ob_flush()和flush()的区别

    最近写定时任务,遇到ob_flush()和flush()混淆的问题... ob_flush/flush在手册中的描述, 都是刷新输出缓冲区, 并且还需要配套使用, 所以会导致很多人迷惑- 其实, 他们 ...

  3. CHM格式的电子书打开是空白的解决办法

    CHM是英语“Compiled Help Manual”的简写,即“已编译的帮助文件”.CHM是微软新一代的帮助文件格式,利用HTML作源文,把帮助内容以类似数据库的形式编译储存.

  4. 【题解】SCOI2010幸运数字

    最近在学习容斥相关,于是就看到了这个题.一开始以为是补集转化,但是观察一下马上发现不可行,好像直接做会比较容易一些.一个数满足要求的充要条件即为是一个幸运数字的倍数,那么容斥可以轻松搞定,只要枚举是一 ...

  5. [Leetcode] Merge two sorted lists 合并两已排序的链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  6. 【CF MEMSQL 3.0 D. Third Month Insanity】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. 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 ...

  8. Ubuntu1604 install netease-cloud music

    Two issue: 1. There is no voice on my computer, and the system was mute and cannot unmute. eric@E641 ...

  9. JAVASCRIPT和JSP计算闰年

    0x01:JAVASCRIPT 实现 <h1 align="left">求闰年</h1> 开始年份: <input type="text&q ...

  10. 51Nod-1586-约数和

    #include <cstdio> using namespace std; typedef long long ll; ; int n, q; int cnt[MAXN]; ll a[M ...