[JZOJ5129][SDOI省队集训2017]字符串

题目大意

从n个字符串中依次选出一个子串拼接,求本质不同的数量。

SAM

我们尽量长化前面的。
于是倒着做,对每个串都要建SAM。
只需要处理开头是某个字符的方案数,SAM的一个节点没有该转移边即可。
大概就这样,懒得写清楚了。

#include<cstdio>
#include<algorithm>
#include<cstring>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
const int maxn=1000000+10,mo=1000000007;
int g[maxn*2][26],step[maxn*2],fail[maxn*2],size[maxn*2],a[maxn],b[maxn];
char s[maxn],h[maxn];
int L[maxn],R[maxn];
int f[26];
int i,j,k,l,t,n,m,last,tot,top,ans,mx;
void add(int x){
    int p=last,np=++tot;
    step[np]=step[p]+1;
    while (p&&g[p][x]==0){
        g[p][x]=np;
        p=fail[p];
    }
    if (!p) fail[np]=1;
    else{
        int q=g[p][x];
        if (step[q]==step[p]+1) fail[np]=q;
        else{
            int i,nq=++tot;
            fo(i,0,25) g[nq][i]=g[q][i];
            fail[nq]=fail[q];
            fail[q]=nq;
            fail[np]=nq;
            step[nq]=step[p]+1;
            while (p&&g[p][x]==q){
                g[p][x]=nq;
                p=fail[p];
            }
        }
    }
    last=np;
}
void work(){
    int i;
    mx=0;
    fo(i,1,tot) mx=max(mx,step[i]);
    fo(i,0,mx) b[i]=0;
    fo(i,1,tot) b[step[i]]++;
    fo(i,1,mx) b[i]+=b[i-1];
    fd(i,tot,1) a[b[step[i]]--]=i;
}
void clear(){
    int i,j;
    fo(i,1,tot)
        fo(j,0,25)
            g[i][j]=0;
    fo(i,1,tot) size[i]=0;
    tot=0;
}
int main(){
    freopen("str.in","r",stdin);freopen("str.out","w",stdout);
    scanf("%d",&n);
    top=1;
    fo(i,1,n){
        L[i]=top;
        scanf("%s",h+1);
        m=strlen(h+1);
        fo(j,1,m) s[top++]=h[j];
        R[i]=top-1;
    }
    fd(i,n,1){
        last=tot=1;
        fo(j,L[i],R[i]) add(s[j]-'a');
        work();
        /*fd(j,tot,2){
            fo(k,0,25)
                if (!g[a[j]][k]){
                    size[a[j]]+=f[k];
                    if (size[a[j]]>=mo) size[a[j]]-=mo;
                }
            size[fail[a[j]]]+=size[a[j]];
            if (size[fail[a[j]]]>=mo) size[fail[a[j]]]-=mo;
        }*/
        fd(j,tot,2){
            size[a[j]]=1;
            fo(k,0,25)
                if (!g[a[j]][k]){
                    size[a[j]]+=f[k];
                    if (size[a[j]]>=mo) size[a[j]]-=mo;
                }
                else{
                    size[a[j]]+=size[g[a[j]][k]];
                    if (size[a[j]]>=mo) size[a[j]]-=mo;
                }
        }
        fo(j,0,25)
            if (g[1][j]) f[j]=size[g[1][j]];
        clear();
    }
    fo(i,0,25) (ans+=f[i])%=mo;
    ans++;
    ans%=mo;
    printf("%d\n",ans);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值