首先比较明显的是我们可以将字符串组建立ac自动机,那么对于询问s1字符串在s2字符串中出现的次数,就是在以s1结尾为根的fail tree中,子树有多少个节点是s2的节点,这样我们处理fail tree的dfs序,然后用BIT维护,但是如果只是在线处理询问的话会tle,因为每个询问需要将节点的每一位在BIT中都修改,那么我们就浪费了好多性质,因为对于好多字符串拥有较长的LCP,那么我们可以模拟建trie的过程在自动机上跑,每遇到一个添加的字符,就解决所有询问为某字符串在该字符串中出现的次数,这样就可以了。

/**************************************************************
    Problem: 2434
    User: BLADEVIL
    Language: C++
    Result: Accepted
    Time:852 ms
    Memory:39128 kb
****************************************************************/
 
//By BLADEVIL
#include <cstdio>
#include <cstring>
#define maxn 200010
 
using namespace std;
 
struct node{
    int cnt,last,left,right;
    node *child[],*fail,*father;
    node(){
        cnt=last=left=right=;
        fail=father=NULL;
        memset(child,,sizeof child);
    }
}nodepool[maxn],*totnode,*root,*que[maxn],*adr[maxn],*other[maxn],*adrans[maxn];
char c[maxn];
int len,tot,l,sum;
int pre[maxn],bit[maxn];
int ll,preans[maxn],otherans[maxn],lastans[maxn],sizeans[maxn];
int ans[maxn];
 
void add(int x,int y){
    while (x<=sum){
        bit[x]+=y;
        x+=x&(-x);
    }
}
 
int ask(int x){
    int ans=;
    while (x){
        ans+=bit[x];
        x-=x&(-x);
    }
    return ans;
}
 
void connect(node *x,node *y){
    pre[++l]=x->last;
    x->last=l;
    other[l]=y;
    //printf("%d %d\n",x,y);
}
 
void connectans(int x,int y,int z){
    preans[++l]=lastans[x];
    lastans[x]=l;
    otherans[l]=y;
    sizeans[l]=z;
}
 
void build_trie(){
    totnode=nodepool; root=totnode++;
    scanf("%s",&c); len=strlen(c);
    node *t=root;
    for (int i=;i<len;i++){
        if (c[i]=='P') adrans[++tot]=t; else
        if (c[i]=='B') t=t->father; else {
            if (!t->child[c[i]-'a'])
                t->child[c[i]-'a']=totnode++,t->child[c[i]-'a']->father=t;
            t=t->child[c[i]-'a'];
            adr[i]=t;
        };
        //printf("%d ",t);
    }
    //printf("\n");
    //for (int i=0;i<len;i++) printf("%d ",adr[i]);
    //for (node *i=nodepool;i!=totnode;i++) printf("%d %d\n",i,i->father);
}
 
void build_ac(){
    int h=,t=;
    que[]=root; root->fail=root;
    for (int i=;i<;i++) if (!root->child[i]) root->child[i]=root;
    while (h<t){
        node *v=que[++h];
        for (int i=;i<;i++) if (v->child[i]&&v->child[i]!=root){
            que[++t]=v->child[i];
            node *u=v->fail;
            que[t]->fail=u->child[i]!=que[t]?u->child[i]:root;
        } else v->child[i]=v->fail->child[i];
    }
    //for (int i=1;i<=t;i++) printf("%d %d ",que[i],que[i]->fail); printf("\n");
    //for (int i=1;i<=tot;i++) printf("%d ",adr[i]); printf("\n");
}
 
void dfs(node *x,node *fa){
    //printf("%d %d %d\n",x,fa,sum);
    x->left=++sum;
    for (int p=x->last;p;p=pre[p]){
        if (other[p]==fa) continue;
        dfs(other[p],x);
    }
    x->right=sum;
}
 
/*void work(){
    for (node *i=nodepool;i!=totnode;i++) if (i!=root) connect(i->fail,i);
    dfs(root,NULL);
    //for (node *i=nodepool;i!=totnode;i++) printf("%d %d %d %d\n",i,i->left,i->right,i->father);
    int m;
    scanf("%d",&m);
    while (m--){
        int x,y;
        scanf("%d %d",&x,&y);
        for (node *i=adr[y];i!=root;i=i->father) add(i->left,1);//printf("%d ",i->left);
        //printf("%d %d",adr[x]->left,adr[x]->right);
        //printf("%d ",ask(adr[x]->right));
        printf("%d\n",ask(adr[x]->right)-ask(adr[x]->left-1));
        for (node *i=adr[y];i!=root;i=i->father) add(i->left,-1);
    }
}*/
 
void work(){
    for (node *i=nodepool;i!=totnode;i++) if (i!=root) connect(i->fail,i);
    dfs(root,NULL);
    int m;
    scanf("%d",&m);
    for (int i=;i<=m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        connectans(y,x,i);
    }
    int stack[maxn],tot=,ansy=;
    memset(stack,,sizeof stack);
    for (int i=;i<len;i++){
        if (c[i]=='P'){
            ansy++;
            for (int p=lastans[ansy];p;p=preans[p]){
                ans[sizeans[p]]=ask(adrans[otherans[p]]->right)-ask(adrans[otherans[p]]->left-);
            }
        } else
        if (c[i]=='B') {
            add(adr[stack[tot--]]->left,-);
        } else {
            stack[++tot]=i;
            add(adr[i]->left,);
        }
    }
    for (int i=;i<=m;i++) printf("%d\n",ans[i]);
}
 
int main(){
    build_trie();
    build_ac();
    work();
    return ;
}

bzoj 2434 fail tree+dfs序的更多相关文章

  1. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  2. BZOJ 2905: 背单词 AC自动机+fail树+dfs序+线段树

    Description 给定一张包含N个单词的表,每个单词有个价值W.要求从中选出一个子序列使得其中的每个单词是后一个单词的子串,最大化子序列中W的和. Input 第一行一个整数TEST,表示数据组 ...

  3. bzoj2434 fail树 + dfs序 + 树状数组

    https://www.lydsy.com/JudgeOnline/problem.php?id=2434 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现, ...

  4. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  5. BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)

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

  6. NOI2011阿狸的打字机(fail树+DFS序)

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

  7. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  8. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  9. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

随机推荐

  1. OSG学习:移动/缩放/旋转模型

    移动和缩放以及旋转都是对矩阵进行操作,这些操作如果要叠加直接矩阵相乘就可以了. 下面的示例代码中,加入了四个bignathan,一个是默认加入在最中间,一个向上移2单位,一个是向下移2单位且缩放0.5 ...

  2. linux下安装多个jdk版本的切换问题

    下载地址: https://www.azul.com/downloads/zulu/ 解压: [root@localhost java]# tar -zxvf /usr/java/zulu8.38.0 ...

  3. VS2010中的sln,suo分别是什么含义

    我们通过双击.sln加载出我们的工程. Visual Studio.NET采用两种文件类型(.sln和.suo)来存储特定于解决方案的设置,它们总称为解决方案文件.为解决方案资源管理器提供显示管理文件 ...

  4. /proc/meminfo中meminfo的计算方法

    /proc/meminfo里的可使用内存的计算没有那么简单,并不是简单的free和page cache的加和 free + pagecache 以此为基准 但是需要减去一些内存:首先要减去系统预留的内 ...

  5. [剑指Offer] 54.字符流中的第一个不重复的字符

    题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...

  6. WPF绑定到父元素的属性的方法

    应用:绑定到父元素的属性上的方法,看图.

  7. JQuery UI的拖拽功能实现方法小结

    JQuery UI提供的API极大简化了拖拽功能的开发.只需要分别在拖拽源(source)和目标(target)上调用draggable和droppable两个函数即可. 拖拽原理 首先要明确几个概念 ...

  8. BZOJ 1045 糖果传递(思维)

    设第i个人给了第i+1个人mi个糖果(可以为负),因为最后每个人的糖果都会变成sum/n. 可以得到方程组 mi-mi+1=a[i+1]-sum/n.(1<=i<=n). 把方程组化为mn ...

  9. BZOJ4736 温暖会指引我们前行(LCT+最大生成树)

    类似于瓶颈路,满足条件的路径一定在温度的最大生成树上,那么就是一个LCT维护MST的裸题了. #include<iostream> #include<cstdio> #incl ...

  10. CenOS shell脚本

    1.先查看脚本解释器 [es@bigdata-senior01 ~]$ echo $SHELL /bin/bash 2.编写最简单的脚本 vi test.sh#第一行的脚本声明(#!)用来告诉系统使用 ...