BZOJ3277:串(后缀自动机+乱搞)

题面
题意:n个串,问每个串有多少个子串在至少k个串中出现

建出广义sam,对于每个状态
求出parent树上的子树有多少个来自不同串的节点
大于k的可以贡献答案

具体实现就是乱搞

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
#define mmst(a, b) memset(a, b, sizeof(a))
#define mmcp(a, b) memcpy(a, b, sizeof(b))

typedef long long LL;

const int N=400400;

int son[N][26],pre[N],dep[N],cnt=1,w[N],last;
int n,k;
int head[N],nex[N],to[N],cur;
int l[N],r[N],siz[N],a[N],f[N],val[N],by[N],times;
int st[N];
int bit[N],all;
char cc[N];

void update(int x,int ad)
{
    for(;x<=cnt+10;x+=x&-x)
    bit[x]+=ad;
    all+=ad;
}

int sum(int x)
{
    int res=0;
    for(;x;x-=x&-x)
    res+=bit[x];
    return res;
}

void add(int u,int v)
{
    to[++cur]=v;
    nex[cur]=head[u];
    head[u]=cur;
}

void dfs(int x)
{
    l[x]=++times;
    if(a[w[x]])
    f[a[w[x]]]=times;
    if(w[x])
    a[w[x]]=times;

    by[times]=w[x];
    for(int h=head[x];h;h=nex[h])
    dfs(to[h]);
    r[x]=times;
}

void dfs2(int x)
{
    siz[x]-=(all-sum(r[x]));
    if(f[l[x]])
    update(f[l[x]],1);
    for(int h=head[x];h;h=nex[h])
    dfs2(to[h]);
    siz[x]+=(all-sum(r[x]));
}

void dfs3(int x)
{
    if(siz[x]>=k)
    val[x]+=dep[x]-dep[pre[x]];
    for(int h=head[x];h;h=nex[h])
    {
        val[to[h]]+=val[x];
        dfs3(to[h]);
    }
}

void Insert(int x,int ops)
{
    dep[++cnt]=dep[last]+1;
    int np=cnt,p=last;
    last=np;
    w[np]=ops;
    for(;!son[p][x];p=pre[p])
    son[p][x]=np;
    if(!p)
    pre[np]=1;
    else
    {
        int q=son[p][x];
        if(dep[q]==dep[p]+1)
        pre[np]=q;
        else
        {
            dep[++cnt]=dep[p]+1;
            int nq=cnt;
            pre[nq]=pre[q];
            pre[q]=pre[np]=nq;
            mmcp(son[nq],son[q]);
            for(;son[p][x]==q;p=pre[p])
            son[p][x]=nq;
        }
    }
}

int main()
{
    cin>>n>>k;
    st[1]=1;
    for(int i=1;i<=n;i++)
    {
        last=1;
        scanf("%s",cc+st[i]);
        st[i+1]=strlen(cc+1)+1;
        for(int j=st[i];j<st[i+1];j++)
        Insert(cc[j]-'a',i);
    }
    for(int i=2;i<=cnt;i++)
    add(pre[i],i);

    dfs(1);
    for(int i=1;i<=n;i++)
    if(a[i])
    f[a[i]]=cnt+1;
    dfs2(1);
    dfs3(1);

    for(int i=1;i<=n;i++)
    {
        int now=1;
        LL ans=0;
        for(int j=st[i];j<st[i+1];j++)
        {
            now=son[now][cc[j]-'a'];
            ans+=val[now];
        }
        printf("%d ",ans);
    }
    cout<<endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值