首先比较明显的是我们可以将字符串组建立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. div、span绑定内容改变事件

    内容改变事件onchange只适用于form表单标签(input.select.textarea) 当需要对div.span标签进行内容改变监听则无法适用,查阅了一些资料发现jquery有针对的方法, ...

  2. 【bzoj4008】[HNOI2015]亚瑟王 概率dp

    题目描述 $n$ 张牌,$r$ 轮游戏,每轮从左向右操作,遇到第 $i$ 张牌有 $p_i$ 的概率选中,选中会产生 $d_i$ 的贡献,丢弃掉该牌并结束这一轮,否则继续下一张.问最终的期望贡献. 输 ...

  3. vdbench-自动化测试脚本

    #!/usr/bin/python # -*- coding:utf8 -*- import sys import commands TEST_CONF=""" hd=d ...

  4. Dom样式操作-属性操作

    1. 对样式进行操作: 1) 以样式(C1,C2等)为最小单位进行修改. className, classList, (以列表形式获得) classList.add("C2"), ...

  5. 在Windows*上编译Tensorflow教程

    背景介绍 最简单的 Tensorflow 的安装方法是在 pip 一键式安装官方预编译好的包 pip install tensorflow 通常这种预编译的包的编译参数选择是为了最大兼容性而不是为了最 ...

  6. 在局域网中基于Windows文件共享的git环境搭建

    本文的思想是在局域网中用一台电脑作为服务器,在其中建立一个文件夹,作为总的公开版本库.然后将这个文件夹共享,使其他客户机都可以访问,从而进行代码的管理. 一.下载安装文件 1.git核心: git-f ...

  7. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  8. c++11新特性之future

    std::future可以从异步任务中获取结果,一般与std::async配合使用,std::async用于创建异步任务,实际上就是创建一个线程执行相应任务. 先看段代码: #include < ...

  9. lightoj 1282 && uva 11029

    Leading and Trailing lightoj 链接:http://lightoj.com/volume_showproblem.php?problem=1282 uva 链接:http:/ ...

  10. Codeforces Round #407 (Div. 2) D,E

    图论 D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard ...