跳到正文

题解

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

AeeE5x约 8 分钟
原文
文章信息与版本

文章信息

收录
2026年2月13日
更新
2026年2月13日
篇幅
约 8 分钟
题目
P10992

版本

  1. v2当前
  2. v1

前言

好题。

Description

对于两个等长的字符串 AA 和 BB,如果对于任意 i,ji, j,都有 Ai=AjA_i = A_j 和 Bi=BjB_i = B_j 同时满足或同时不满足,那么我们称 AA 和 BB 是一对同类串。

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

1≤∣S∣,∣T∣≤1051\le |S|,|T|\le 10^5。

Solution

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

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

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

不难证明这是充要条件。


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

对于所有字符 cc,我们定义 01 串 fcf_c,其中 fc,i=1f_{c,i}=1 当且仅当 Si=cS_i=c。

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

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

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

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

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

其他

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

  • 只计算 SS 的所有窗口的哈希值并排序,对 TT 的每个窗口哈希值在其中二分判等。
  • 底数、模数和哈希值用 unsigned long long。
  • 玄学 static inline。
  • 集合哈希部分不建议使用异或哈希。本人尝试了无数种底数和模数,没有一次过的。
  • 建议使用乘哈希,再稍微优化一点常数。
  • 把 fcf_c 中的 01 当成字符,而不是数。

代码

INFO
C++
#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;
}
原文