题解:P10992 [蓝桥杯 2023 国 Python A] 最长同类子串

· · 题解

前言

好题。

Description

对于两个等长的字符串 A 和 B,如果对于任意 i, j,都有 A_i = A_j 和 B_i = B_j 同时满足或同时不满足,那么我们称 A 和 B 是一对同类串。

给定 S 和 T,求最大的 k 使得 S 含有子串 S',T 含有子串 T',满足 S' 和 T' 是一对同类串。

Solution

二分答案是显然的。考虑对于一个 len 如何 \operatorname{check}。

考虑刻画两个字符串是同类串的条件,设两个字符串分别为 s 和 t,长度为 n。

则对于任意一个 i\in[1,n],s_i 在 s 中出现的所有位置组成集合 \{spos_{s_i}\} 和 t_i 在 t 中出现的所有位置组成集合 \{tpos_{t_i}\} 相等。

不难证明这是充要条件。

我们发现如果设计一个如上的哈希函数,问题会变得非常简单。而上述刻画启示我们对每种字符分开考虑。

对于所有字符 c,我们定义 01 串 f_c,其中 f_{c,i}=1 当且仅当 S_i=c。

那么我们在滑动窗口的过程中,可以简单计算每个字符在窗口中的哈希值。

又因为字符类型不重要,所以我们可以把这些哈希值合起来看作一个无序的集合。

因此两个集合相等,就对应两个窗口是一对同类串。

同样对于 T 执行此操作,如果存在相等集合,则代表存在同类子串。

如此我们得到了一个常数巨大的 O((n\log n+n|\Sigma|)\log n) 做法。

其他

下面是一些卡正确性或常数的技巧。

代码

:::info

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define ull unsigned long long
#define mp(x,y) make_pair(x,y)
#define si static inline
using namespace std;

int n,m;
int a[100010],b[100010];

const ull mod1=998244353,mod2=1000000021;
const ull P1=911382323,P2=972663749;

ull pw1[100010],pw2[100010];

ull prehash1_a[26][100010],prehash2_a[26][100010];
ull prehash1_b[26][100010],prehash2_b[26][100010];

si ull gethash1_a(int v,int l,int r){return (prehash1_a[v][r]-prehash1_a[v][l-1]*pw1[r-l+1]%mod1+mod1)%mod1;}
si ull gethash2_a(int v,int l,int r){return (prehash2_a[v][r]-prehash2_a[v][l-1]*pw2[r-l+1]%mod2+mod2)%mod2;}
si ull gethash1_b(int v,int l,int r){return (prehash1_b[v][r]-prehash1_b[v][l-1]*pw1[r-l+1]%mod1+mod1)%mod1;}
si ull gethash2_b(int v,int l,int r){return (prehash2_b[v][r]-prehash2_b[v][l-1]*pw2[r-l+1]%mod2+mod2)%mod2;}

pair<ull,ull> vec[100010];

si bool check(int t){
    int cnt=0;
    for(int l=1,r=t;r<=n;l++,r++){
        ull nowhash1=1,nowhash2=1;
        for(int nowv=0;nowv<26;nowv++){
            ull w1=gethash1_a(nowv,l,r);
            ull w2=gethash2_a(nowv,l,r);
            (nowhash1*=w1)%=mod1;
            (nowhash2*=w2)%=mod2;
        }
        vec[++cnt]=mp(nowhash1,nowhash2);
    }
    sort(vec+1,vec+1+cnt);

    for(int l=1,r=t;r<=m;l++,r++){
        ull nowhash1=1,nowhash2=1;
        for(int nowv=0;nowv<26;nowv++){
            ull w1=gethash1_b(nowv,l,r);
            ull w2=gethash2_b(nowv,l,r);
            (nowhash1*=w1)%=mod1;
            (nowhash2*=w2)%=mod2;
        }
        int p=lower_bound(vec+1,vec+1+cnt,mp(nowhash1,nowhash2))-vec;
        if(vec[p]==mp(nowhash1,nowhash2)) return true;
    }
    return false;
}

int main(){
    cin.tie(0)->sync_with_stdio(false);

    string xa,xb;cin>>xa>>xb;
    n=xa.length(),xa=' '+xa;
    m=xb.length(),xb=' '+xb;
    for(int i=1;i<=n;i++) a[i]=xa[i]-'a';
    for(int i=1;i<=m;i++) b[i]=xb[i]-'a';

    pw1[0]=pw2[0]=1;
    for(int i=1;i<=max(n,m);i++) pw1[i]=pw1[i-1]*P1%mod1,pw2[i]=pw2[i-1]*P2%mod2;
    for(int nowv=0;nowv<26;nowv++){
        for(int i=1;i<=n;i++){
            prehash1_a[nowv][i]=(prehash1_a[nowv][i-1]*P1%mod1+(a[i]==nowv)+2)%mod1;
            prehash2_a[nowv][i]=(prehash2_a[nowv][i-1]*P2%mod2+(a[i]==nowv)+2)%mod2;
        }
        for(int i=1;i<=m;i++){
            prehash1_b[nowv][i]=(prehash1_b[nowv][i-1]*P1%mod1+(b[i]==nowv)+2)%mod1;
            prehash2_b[nowv][i]=(prehash2_b[nowv][i-1]*P2%mod2+(b[i]==nowv)+2)%mod2;
        }
    }

    int l=1,r=min(n,m);
    while(l<r){
        int mid=(l+r+1)>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    cout<<l<<"\n";

    # ifdef TakanashiHoshino
    cerr<<"\nUsed time: "<<clock()*1.0/CLOCKS_PER_SEC<<"s.\n";
    # endif
    return 0;
}

:::