bzoj3473 字符串

Description


给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串?

对于 100% 的数据,1<=n,k<=10^5 所有字符串总长不超过10^5 字符串只包含小写字母

Solution


感觉还有很多后缀自动机的玩法不太懂

可以建广义sam,每个节点用set记录包含哪些给定字符串的子串,然后就可以启发式合并求right集惹
询问的话就是把询问串扔到sam上跑,每次统计以当前节点为结束点的串的贡献就ok

记得开LL

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define drp(i,st,ed) for (int i=st;i>=ed;--i)
#define copy(x,t) memcpy(x,t,sizeof(x))

typedef long long LL;
const int N=1000005;

int rec[N][26],last,tot;
int rank[N],bct[N],pts[N];
int size[N],len[N],fa[N];
int st[N],ed[N],n,m;

char str[N],a[N];

std:: set <int> set[N];
std:: set <int>:: iterator it;

void extend(int ch,int pos) {
    int p,q,np,nq;
    if (rec[last][ch]) {
        p=last; q=rec[last][ch];
        if (len[p]+1==len[q]) {
            last=rec[last][ch];
            set[last].insert(pos);
        } else {
            nq=++tot; len[nq]=len[p]+1;
            set[nq].insert(pos);
            copy(rec[nq],rec[q]);
            fa[nq]=fa[q];
            fa[q]=last=nq;
            for (;p&&rec[p][ch]==q;p=fa[p]) rec[p][ch]=nq;
            set[last].insert(pos);
        }
        return ;
    }
    p=last; last=np=++tot;
    len[np]=len[p]+1;
    set[np].insert(pos);
    for (;p&&!rec[p][ch];p=fa[p]) rec[p][ch]=np;
    if (!p) fa[np]=1;
    else {
        q=rec[p][ch];
        if (len[p]+1==len[q]) fa[np]=q;
        else {
            nq=++tot; len[nq]=len[p]+1;
            copy(rec[nq],rec[q]);
            fa[nq]=fa[q];
            fa[q]=fa[np]=nq;
            for (;p&&rec[p][ch]==q;p=fa[p]) rec[p][ch]=nq;
        }
    }
}

void merge(int x,int y) {
    if (set[x].empty()||set[y].empty()) return ;
    for (it=set[x].begin();it!=set[x].end();it++) set[y].insert(*it);
}

void sort() {
    rep(i,1,tot) bct[len[i]]++;
    rep(i,1,ed[n]) bct[i]+=bct[i-1];
    rep(i,1,tot) rank[bct[len[i]]--]=i;
    drp(i,tot,1) {
        size[rank[i]]=set[rank[i]].size();
        if (!fa[rank[i]]) continue;
        if (set[rank[i]].size()<set[fa[rank[i]]].size()) {
            merge(rank[i],fa[rank[i]]);
        } else {
            merge(fa[rank[i]],rank[i]);
            std:: swap(set[rank[i]],set[fa[rank[i]]]);
        }
    }
    rep(i,1,tot) {
        if (size[fa[rank[i]]]>=m) pts[rank[i]]=fa[rank[i]];
        else pts[rank[i]]=pts[fa[rank[i]]];
    }
}

void solve(int x,int y) {
    int now=1,cnt=0; LL ans=0;
    rep(i,x,y) {
        int ch=str[i]-'a';
        if (rec[now][ch]) {
            now=rec[now][ch]; cnt++;
        } else {
            for (;now&&!rec[now][ch];now=fa[now]);
            if (!now) {
                now=1; cnt=0;
            } else {
                cnt=len[now]+1;
                now=rec[now][ch];
            }
        }
        if (size[now]>=m) ans+=cnt;
        else ans+=len[pts[now]];
    }
    printf("%lld ", ans);
}

int main(void) {
    freopen("data.in","r",stdin);
    freopen("myp.out","w",stdout);
    scanf("%d%d",&n,&m);
    last=tot=1;
    rep(i,1,n) {
        scanf("%s",a+1); int l=strlen(a+1);
        st[i]=ed[i-1]+1;
        ed[i]=st[i]+l-1;
        rep(j,1,l) str[st[i]+j-1]=a[j];
    }
    rep(i,1,n) {
        last=1;
        rep(j,st[i],ed[i]) extend(str[j]-'a',i);
    }
    sort();
    rep(i,1,n) solve(st[i],ed[i]);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值