题解:P13531 [OOI 2023] A task for substrings / 字符串问题

· · 题解

省流:区间多模匹配。

用 w_i 表示 t_{1\dots i} 的答案,那么 t_{l\dots r} 的答案就是 w_r-w_{l-1} 再减去多算的贡献。

此时你可以选择对 s_i 根分然后做到 O(|T|\sqrt{|S|}),太丑陋了。

发现减去多算的贡献是比较困难的,限制太强了。

设 st_i 为以 i 结尾的的匹配中,左端点最靠左的匹配的左端点。

找到最大的 k 使得 k\in[l,r] 且 st_k<l,此时 [k+1,r] 之间的贡献不会算多,为 w_r-w_k。

现在只需要求出 t_{l\dots k} 的答案,看上去限制并没有变松?

分析一下,根据定义来看 t_{l\dots k} 一定是某个 s 串的后缀,所以把所有 s 反一下预处理就好了。

所以对于 st_i,我们还要知道是哪个 s 串贡献的,将之称作 id_i。

分析一下整个过程,明显 w_i,st_i,id_i 可以对正着的 s_i 建 AC 机,然后让 t 在上面游走一下。

求出 k 可以用一棵线段树,在上面二分一下,记得特判 k 不存在的情况。

为减小常数,应该在处理完 w_i,st_i,id_i 之后将 AC 机清空,再插入 s 的反串。

注意开 long long,很好写,连写带调半个多小时吧。

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

bool Mst;
const int N=1e6+10,Max=5e6+10,M=5e5+10;
struct AC{
    int ch[26],fail,len,id,ct;
    #define ch(k,i) tr[k].ch[i]
    #define fail(k) tr[k].fail
    #define len(k) tr[k].len
    #define id(k) tr[k].id
    #define ct(k) tr[k].ct
}tr[N];
int n,m,q,tot,st[Max],ids[Max];string s[M],t;
vector < ll > res[M];ll w[Max];

inline void chkmax(int &x,int y){(x<y)&&(x=y);}
inline void chkmin(int &x,int y){(x>y)&&(x=y);}

inline void ins(string &s,int d){
    int now=0;
    for(char x:s){
        int to=x-'a';
        if(!ch(now,to)) ch(now,to)=++tot;
        now=ch(now,to);
    }
    if(len(now)<(int)s.size()){len(now)=s.size();id(now)=d;}
    ++ct(now);
}
void build(){
    queue < int > Q;
    for(int i=0;i<26;++i) if(ch(0,i)) Q.push(ch(0,i));
    while(!Q.empty()){
        int x=Q.front();Q.pop();
        for(int i=0;i<26;++i)
            if(!ch(x,i)) ch(x,i)=ch(fail(x),i);
            else {int to=ch(x,i);fail(to)=ch(fail(x),i);Q.push(to);}
        int y=fail(x);ct(x)+=ct(y);
        if(len(x)<len(y)){len(x)=len(y);id(x)=id(y);}
    }
}
void clear(){
    for(int i=0;i<=tot;++i){
        for(int j=0;j<26;++j) ch(i,j)=0;
        fail(i)=len(i)=ct(i)=id(i)=0;
    }
    tot=0;
}
struct Segment{
    #define ls now<<1
    #define rs now<<1|1
    #define mid ((l+r)>>1)
    int mn[Max<<2];
    inline void pushup(int now){mn[now]=min(mn[ls],mn[rs]);}
    void build(int now,int l,int r){
        if(l==r){mn[now]=st[l];return ;}
        build(ls,l,mid);build(rs,mid+1,r);pushup(now);
    }
    int ask(int now,int l,int r,int lim,int R){//p>=lim,p<=R,mn[p]<lim
        if(r<lim||l>R||mn[now]>=lim) return -1;
        if(l==r) return l;
        int res=ask(rs,mid+1,r,lim,R);
        if(~res) return res;
        return ask(ls,l,mid,lim,R);
    }
    inline void build(){build(1,1,n);}
    inline int ask(int lim,int R){return ask(1,1,n,lim,R);}
}trs;

bool Med;
int main(){
//  cerr<<(&Mst-&Med)/1048576.0<<" MB\n";
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>m>>q;cin>>t;n=t.size();t=' '+t;
    for(int i=1;i<=m;++i) cin>>s[i];
    for(int i=1;i<=m;++i) ins(s[i],i);
    build();
    for(int i=1,now=0;i<=n;++i){
        now=ch(now,t[i]-'a');w[i]=ct(now)+w[i-1];
        st[i]=i-len(now)+1;ids[i]=id(now);
//      cout<<i<<' '<<w[i]<<' '<<st[i]<<' '<<ids[i]<<' '<<now<<'\n';
    }
    trs.build(1,1,n);
    clear();
    for(int i=1;i<=m;++i){
        reverse(s[i].begin(),s[i].end());
        ins(s[i],i);
    }
    build();
    for(int i=1;i<=m;++i){
        int now=0,sz=0;ll val=0;
        res[i].resize(s[i].size());
        for(char x:s[i]){
            now=ch(now,x-'a');
            res[i][sz++]=(val+=ct(now));
        }
    }
    while(q--){
        int l,r;cin>>l>>r;
        int k=trs.ask(l,r);
//      cout<<"------- "<<k<<'\n';
        if(!~k) cout<<w[r]-w[l-1]<<' ';
        else {
            ll ans=w[r]-w[k];
            ans+=res[ids[k]][k-l];
            cout<<ans<<' ';
        }
    }
    return 0;
}