题解:P4075 [SDOI2016] 模式字符串

· · 题解

考虑一下对于合法点对 (u,v) 在其 LCA 处统计贡献。

考虑 u \to LCA 的路径上的字符串一定形如串 S 不断重复,可能最后一次重复没有形成完整周期,我们发现合法的 LCA 是有单调性的,具体而言是 u \to 1 的链上一段前缀,考虑二分(字符串哈希去 check)出分界点后把 u 挂在自己这个节点上加入,在分界点处删除,v \to LCA 的情形是类似的,不过变成了 S 的反串不断重复。

然后考虑做树上启发式合并,当 u \to LCA,v \to LCA 均满足条件后,只需要再满足 dis(u,v) 是 m 的倍数即可,因此启发式合并的时候用一个桶将 dep_{u} \bmod m = i 的 u 的数量记下来即可在枚举轻子树某个 u,v 时计算另一个合法的端点数量。

时间复杂度 O(n \log n)。

#include<bits/stdc++.h>
using namespace std;
#define int unsigned int
const int base = 1331;
const int maxn = 1e5+114;
//#define lowbit(x) (x&(-x))
//#define bp push_back
//#define sz size
//#define cl clear
int n,m;
int preS[maxn],preT[maxn];
char c[maxn],S[maxn],T[maxn];
int _pow[maxn];
int dep[maxn];//反 
int depth[maxn];//正 
int pre[maxn];//祖先链哈希值 
int jumpS[maxn],jumpT[maxn];
vector<int> kfa;
vector<int> Ins[maxn][2],Del[maxn][2];
unordered_map<int,int> cnt[maxn][2];
unordered_set<int> s[maxn][2];
vector<int> E[maxn];
int nxt[maxn];
void dfs(int u,int fa){
    pre[u]=pre[fa]+_pow[dep[u]]*c[u];
    depth[u]=depth[fa]+1; 
    int l=0,r=min(m,depth[u])+1;
    kfa.push_back(u);
    while(l+1<r){
        int mid=(l+r)>>1;
        if((pre[u]-pre[kfa[depth[u]-mid]])==preS[mid]*_pow[dep[u]-1]) l=mid;
        else r=mid;
    } 
    if(l<m) jumpS[u]=kfa[depth[u]-l];
    else jumpS[u]=(jumpS[kfa[depth[u]-l]]);
    l=1,r=min(m,depth[u])+1;
    while(l+1<r){
        int mid=(l+r)>>1;
        if((pre[u]-pre[kfa[depth[u]-mid]])==preT[mid]*_pow[dep[u]-1]) l=mid;
        else r=mid;
    } 
    if(l<m) jumpT[u]=kfa[depth[u]-l];
    else jumpT[u]=(jumpT[kfa[depth[u]-l]]); 
    if(jumpS[u]!=u){
        Ins[u][0].push_back(depth[u]%m);
        Del[nxt[jumpS[u]]][0].push_back(depth[u]%m);        
    }
    if(jumpT[u]!=u){
        Ins[u][1].push_back(depth[u]%m);
        Del[nxt[jumpT[u]]][1].push_back(depth[u]%m);        
    }
    for(int v:E[u]){
        if(v!=fa){
            dep[v]=dep[u]-1;
            nxt[u]=v;
            dfs(v,u);
        }
    }
    kfa.pop_back();
}
//m | x+y-2*depth[u]+1
//y=2*depth[u]-1-x %m
long long ans;
void dsu(int u,int fa){
    for(int x:Ins[u][0]) s[u][0].insert(x),cnt[u][0][x]++;
    for(int x:Ins[u][1]) s[u][1].insert(x),cnt[u][1][x]++;
    for(int v:E[u]){
        if(v!=fa){
            dsu(v,u);
            if(s[u][0].size()<s[v][0].size()){
                swap(s[u][0],s[v][0]);
                swap(cnt[u][0],cnt[v][0]);
                swap(s[u][1],s[v][1]);
                swap(cnt[u][1],cnt[v][1]);
            }
            for(int x:s[v][0]){
                ans+=cnt[u][1][(2*depth[u]+m*(n+1)-1-x)%m]*cnt[v][0][x];
            }
            for(int x:s[v][1]){
                ans+=cnt[u][0][(2*depth[u]+m*(n+1)-1-x)%m]*cnt[v][1][x];
            }
            for(int x:s[v][0]){
                s[u][0].insert(x);
                cnt[u][0][x]+=cnt[v][0][x];
            }
            s[v][0].clear();
            cnt[v][0].clear();
            for(int x:s[v][1]){
                s[u][1].insert(x);
                cnt[u][1][x]+=cnt[v][1][x];
            }
            s[v][1].clear();
            cnt[v][1].clear();
        }
    }
    for(int x:Del[u][0]) cnt[u][0][x]--;
    for(int x:Del[u][1]) cnt[u][1][x]--;
}
void work(){
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>c[i];
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        E[u].push_back(v);
        E[v].push_back(u);
    }
    for(int i=1;i<=m;i++) cin>>S[i],T[m-i+1]=S[i];
    _pow[0]=1;
    for(int i=1;i<maxn;i++) _pow[i]=_pow[i-1]*base;
    for(int i=1;i<=m;i++) preS[i]=preS[i-1]+S[i]*_pow[i],preT[i]=preT[i-1]+T[i]*_pow[i];
    dep[1]=n;
    kfa.push_back(0);
    dfs(1,0);
    dsu(1,0);
    cout<<ans<<'\n';
    for(int i=0;i<maxn;i++){
        preS[i]=preT[i]=c[i]=S[i]=T[i]=_pow[i]=dep[i]=depth[i]=pre[i]=jumpS[i]=jumpT[i]=nxt[i]=0;
        kfa.clear();
        Ins[i][0].clear(),Ins[i][1].clear(),Del[i][0].clear(),Del[i][1].clear(),cnt[i][0].clear(),cnt[i][1].clear(),s[i][0].clear(),s[i][1].clear(),E[i].clear();
        ans=0;
    }
    return ;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--) work();
    return 0;
}