【题解CF1276F】Asterisk Substrings

· · 题解

题意:给出一个小写字母组成的字符串 s,设 t_i 为把 s 的第 i 个字符改成字符 \text{*} 后得到的字符串,求 \{s,t_1,t_2,...,t_{|s|}\} 中的本质不同子串数量。 1\le n\le 10^5

把需要算的子串分四类:\text{abcd},\text{abc*},\text{*bcd},\text{a*cd}

首先计算不含 \text{*} 的子串,直接就是 s 中的本质不同子串个数。

然后对于开头为 \text{*} 的子串也很好算,就是去掉 s 第一个字符后的本质不同子串个数,结尾为 \text{*} 同理。

现在我们的问题便是如何求 \text{*} 在中间的子串个数。枚举 \text{*} 前面的子串,不难发现,对于枚举的同一个 endpos 集合内的子串,其所有结尾位置集合相同,那么在 \text{*} 后一个的位置集合也相同,这些位置的后缀集合也相同,后缀集合中所包含的本质不同前缀个数也相同,因此贡献也都相同。

说明我们可以直接枚举 endpos 等价类来计算答案。考虑建立 SAM,枚举每个 SAM 上的点,现在的问题是如何快速计算这些结尾位置后面的 \text{*} 的后面的后缀集合的本质不同前缀个数?

考虑 s 的一个前缀,位置为 x,他所被贡献的后缀位置为 x+2,我们建立 s 反串的 SAM,找到这个后缀在 SAM 上所对应的点。根据 SAM 的性质,发现这个点在 parent tree 到根路径长度(也就是这个后缀的长度)是这个位置的一个答案。

但问题是现在有多个位置,需要找到本质不同的前缀集合,仔细想想会发现就是这些点到根的路径并的边权之和。

那问题就很简单了,我们在 s 的 SAM 上枚举点,然后动态维护反串的 SAM 上的路径并。具体而言,对于第一个 SAM 的 parent tree 上的一个点 u 我们维护他的子树在第二个 SAM 上的路径并,这需要支持快速合并儿子字树的路径并。考虑 set 存储是哪些点到根的路径并,按照 dfn 排序,合并则启发式合并,每次往一个路径并里面插入一个从一个点到根的路径,根据经典方法,找到 set 中 dfn 的前驱后继更新即可。

时间复杂度 O(n\log ^2n)

#include<bits/stdc++.h>
#define ls(p) a[p].lc
#define rs(p) a[p].rc
#define fi first
#define se second
#define mkp make_pair
#define ll long long
#define pir pair<ll,ll>
#define pb push_back
using namespace std;
const ll maxn=4e5+10;
ll n,ans,las,pos[maxn],sum[maxn],t,w[20][maxn],dep[maxn],dfn[maxn],Log[maxn],res[maxn];
ll dfnt[maxn];
char s[maxn];
set<ll>st[maxn];
vector<ll>to1[maxn],to2[maxn];
struct SAM{
    ll tot=1, las=1;
    struct node{
        ll fa,len,ch[26];
    }a[maxn];
    void ins(ll c,ll id){
        ll p=las, np=las=++tot;
        a[np].len=a[p].len+1;
        if(!pos[id]) pos[id]=np;
        else if(id<n-1){
            st[np].insert(dfn[pos[id+2]]);
            res[np]=2*sum[pos[id+2]];
        }
        for(;p&&!a[p].ch[c];p=a[p].fa) a[p].ch[c]=np;
        if(!p) a[np].fa=1;
        else{
            ll q=a[p].ch[c];
            if(a[q].len==a[p].len+1) a[np].fa=q;
            else{
                ll nq=++tot; a[nq]=a[q];
                a[nq].len=a[p].len+1;
                a[q].fa=a[np].fa=nq;
                for(;p&&a[p].ch[c]==q;p=a[p].fa) a[p].ch[c]=nq;
            }
        }
    }
}p1,p2;
void dfs(ll u,ll fa){
    w[0][dfn[u]=++t]=fa; dep[u]=dep[fa]+1;
    dfnt[t]=u;
    sum[u]=sum[fa]+p2.a[u].len-p2.a[p2.a[u].fa].len;
    for(ll v:to2[u]){
        dfs(v,u);
    }
}
ll Min(ll u,ll v){
    return dep[u]<dep[v]? u:v;
}
ll dist(ll u,ll v){
    ll x=dfn[u], y=dfn[v];
    if(x>y) swap(x,y);
    ll k=Log[y-x]; ++x;
    ll c=Min(w[k][x],w[k][y-(1<<k)+1]);
    return sum[u]+sum[v]-2*sum[c];
}
void dfs2(ll u){
    for(ll v:to1[u]){
        dfs2(v);
        if(st[u].size()<st[v].size()) swap(st[u],st[v]), swap(res[u],res[v]);
        for(set<ll>::iterator it=st[v].begin(),it2;it!=st[v].end();it++){
            ll x=(*it), y;
            if(x<(*st[u].begin())){
                y=*st[u].begin();
                res[u]+=dist(1,dfnt[x])+dist(dfnt[x],dfnt[y])-dist(1,dfnt[y]);
            }
            else if(x>(*(--st[u].end()))){
                y=*(--st[u].end());
                res[u]+=dist(1,dfnt[x])+dist(dfnt[x],dfnt[y])-dist(1,dfnt[y]);
            }
            else{
                it2=st[u].lower_bound(x); y=*it2;
                res[u]+=dist(dfnt[x],dfnt[y]);
                ll z=y; y=*(--it2);
                res[u]+=dist(dfnt[x],dfnt[y])-dist(dfnt[y],dfnt[z]);
            }
            st[u].insert(x);
        }
    }
    ans+=(p1.a[u].len-p1.a[p1.a[u].fa].len)*(res[u]>>1);
}
int main(){ 
    scanf("%s",s+1); n=strlen(s+1);
    for(ll i=n;i>1;i--) p2.ins(s[i]-'a',i);
    for(ll i=2;i<=p2.tot;i++) ans+=p2.a[i].len-p2.a[p2.a[i].fa].len;
    p2.ins(s[1]-'a',1);
    for(ll i=2;i<=p2.tot;i++) to2[p2.a[i].fa].pb(i), Log[i]=Log[i>>1]+1;
    dfs(1,0);
    for(ll i=1;i<n;i++) p1.ins(s[i]-'a',i);
    for(ll i=1;i<=p1.tot;i++) ans+=p1.a[i].len-p1.a[p1.a[i].fa].len;
    p1.ins(s[n]-'a',n);
    for(ll i=1;i<=p1.tot;i++) ans+=p1.a[i].len-p1.a[p1.a[i].fa].len;
    for(ll i=1;(1<<i)<=p2.tot;i++)
        for(ll j=1;j+(1<<i)-1<=p2.tot;j++)
            w[i][j]=Min(w[i-1][j],w[i-1][j+(1<<i-1)]);
    for(ll i=1;i<=p1.tot;i++) to1[p1.a[i].fa].pb(i);
    dfs2(1);
    printf("%lld",ans+2);
    return 0;
}