首先比较明显的是我们可以将字符串组建立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. 关于命令行参数argv(《学习OpenCV》)

    在<学习OpenCV>这本书中,很多示例代码都用到了命令行参数.作为新手,之前总是很困扰,不知道怎么用.偶然的机会终于略知一二了. 在Visual Studio中,我们可以自行设置命令行参 ...

  2. [剑指Offer] 51.构建乘积数组

    题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不 ...

  3. js 复制到剪贴板 兼容还得自己想办法

    clipboard.js https://github.com/zenorocha/clipboard.js/ 主要问题还是ie8, 可以使用ie 特有的方法 if (window.clipboard ...

  4. 【MVC】ASP.Net MVC 4项目升级MVC 5的方法

    1.备份你的项目 2.从Web API升级到Web API 2,修改global.asax,将 ? 1 WebApiConfig.Register(GlobalConfiguration.Config ...

  5. 【bzoj1131】[POI2008]Sta 树形dp

    题目描述 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 输入 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. 输出 输出你所找到的点,如果具有 ...

  6. 【题解】APIO2018 Duathlon 铁人两项

    首先对于给出的图建立圆方树,然后我们分类讨论每一个点作为中间的中转站出现的情况有多少种,累积到 \(ans\) 中. 对于圆点:在任意两个子树内分别选出一个节点都是合法的. 对于方点:连接向方点的点均 ...

  7. [SCOI2013]摩托车交易 kruskal重构树(最大生成树) 倍增

    ---题面--- 题解: 这题想法简单,,,写起来真的是失智,找了几个小时的错误结果是inf没开到LL范围.... 首先我们需要找到任意两点之间能够携带黄金的上限值,因为是在经过的道路权值中取min, ...

  8. 运动员最佳匹配问题 KM算法:带权二分图匹配

    题面: 羽毛球队有男女运动员各n人.给定2 个n×n矩阵P和Q.P[i][j]是男运动员i和女运动员j配对组成混合双打的男运动员竞赛优势:Q[i][j]是女运动员i和男运动员j配合的女运动员竞赛优势. ...

  9. [bzoj] 2049 洞穴勘探 || LCT

    原题 这是一道LCT的板子题. 至于LCT--link cut tree,也叫动态树,用splay实现动态连边的树. 预备知识: 实边:一个非叶节点,向它的儿子中的一个连一条特殊的边,称为实边;该非叶 ...

  10. [Leetcode] minimum window substring 最小字符窗口

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...