题解: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
二分答案是显然的。考虑对于一个
考虑刻画两个字符串是同类串的条件,设两个字符串分别为
则对于任意一个
不难证明这是充要条件。
我们发现如果设计一个如上的哈希函数,问题会变得非常简单。而上述刻画启示我们对每种字符分开考虑。
对于所有字符
那么我们在滑动窗口的过程中,可以简单计算每个字符在窗口中的哈希值。
又因为字符类型不重要,所以我们可以把这些哈希值合起来看作一个无序的集合。
因此两个集合相等,就对应两个窗口是一对同类串。
同样对于
如此我们得到了一个常数巨大的
其他
下面是一些卡正确性或常数的技巧。
- 只计算
S 的所有窗口的哈希值并排序,对T 的每个窗口哈希值在其中二分判等。 - 底数、模数和哈希值用
unsigned long long。 - 玄学
static inline。 - 集合哈希部分不建议使用异或哈希。本人尝试了无数种底数和模数,没有一次过的。
- 建议使用乘哈希,再稍微优化一点常数。
- 把
f_c 中的 01 当成字符,而不是数。
代码
:::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;
}
:::