【bzoj 3172】单词(Fail树)

传送门biu~
把AC自动机中的Fail指针反向,可以形成一棵Fail树,树上任意一个点是它所有子节点的后缀。那么可以统计以某个点为根的子树的大小,就表示这个字符串在所有字符串的前缀中作为后缀出现的次数,即出现次数。

#include<bits/stdc++.h>
using namespace std;
char c[1000005];
struct Node{
    Node *ch[26],*nex;
    int cnt;
    Node(){
        nex=NULL; cnt=0;
        for(int i=0;i<26;++i) ch[i]=NULL;
    }
}*root=new Node,*tail[205];
inline void Insert(char *s,int id){
    Node *o=root;
    for(int i=1;s[i];++i){
        int to=s[i]-'a';
        if(!o->ch[to])  o->ch[to]=new Node;
        o=o->ch[to];
        ++o->cnt;
    }
    tail[id]=o;
}
queue<Node*>q;stack<Node*>s;
inline void GetFail(){
    root->nex=root;
    for(int i=0;i<26;++i){
        if(root->ch[i])  q.push(root->ch[i]),root->ch[i]->nex=root;
        else    root->ch[i]=root;
    }
    while(!q.empty()){
        Node *x=q.front();q.pop();
        s.push(x);
        for(int i=0;i<26;++i){
            if(x->ch[i])    q.push(x->ch[i]),x->ch[i]->nex=x->nex->ch[i];
            else    x->ch[i]=x->nex->ch[i];
        }
    }
    while(!s.empty()){
        Node *x=s.top();s.pop();
        x->nex->cnt+=x->cnt;
    }
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)   scanf("%s",c+1),Insert(c,i);
    GetFail();
    for(int i=1;i<=n;++i)   printf("%d\n",tail[i]->cnt);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zP1nG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值