题面

求多个串的最长公共子串

Sol

套路,拼在一起,二分答案+后缀数组判定

把大于答案的\(height\)分组,然后计算出一个组内是否有所有串的后缀

由于串只有\(5\)个开个桶就好了

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(100010); int T, n, a[_], sa[_], rk[_], y[_], height[_], t[_], vis[_];
char s[_]; IL bool Cmp(RG int i, RG int j, RG int k){ return y[i] == y[j] && y[i + k] == y[j + k]; } IL void Sort(){
RG int m = 30;
for(RG int i = 1; i <= n; ++i) ++t[rk[i] = a[i]];
for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
for(RG int i = n; i; --i) sa[t[rk[i]]--] = i;
for(RG int k = 1; k <= n; k <<= 1){
RG int l = 0;
for(RG int i = n - k + 1; i <= n; ++i) y[++l] = i;
for(RG int i = 1; i <= n; ++i) if(sa[i] > k) y[++l] = sa[i] - k;
for(RG int i = 0; i <= m; ++i) t[i] = 0;
for(RG int i = 1; i <= n; ++i) ++t[rk[y[i]]];
for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
for(RG int i = n; i; --i) sa[t[rk[y[i]]]--] = y[i];
swap(rk, y); rk[sa[1]] = l = 1;
for(RG int i = 2; i <= n; ++i) rk[sa[i]] = Cmp(sa[i - 1], sa[i], k) ? l : ++l;
if(l >= n) break;
m = l;
}
for(RG int i = 1, j = 0; i <= n; ++i){
j = max(0, j - 1);
while(a[j + i] == a[sa[rk[i] - 1] + j]) ++j;
height[rk[i]] = j;
}
} IL bool Check(RG int x){
RG int v[6], cnt = 0; Fill(v, 0);
for(RG int i = 2; i <= n; ++i){
if(height[i] >= x){
if(vis[sa[i]] && !v[vis[sa[i]]]) ++cnt;
++v[vis[sa[i]]];
}
else{
Fill(v, 0); cnt = 0;
if(vis[sa[i]] && !v[vis[sa[i]]]) ++cnt;
++v[vis[sa[i]]];
}
if(cnt == T) return 1;
}
return 0;
} int main(RG int argc, RG char* argv[]){
scanf("%d", &T);
for(RG int id = 1; id <= T; ++id){
scanf(" %s", s); RG int l = strlen(s);
for(RG int i = 0; i < l; ++i) a[++n] = s[i] - 'a' + 1, vis[n] = id;
a[++n] = 27;
}
Sort();
RG int l = 0, r = n, ans = 0;
while(l <= r){
RG int mid = (l + r) >> 1;
if(Check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf("%d\n", ans);
return 0;
}
05-07 10:36