MemSQL Start[c]UP 2.0 - Round 1 E - Three strings 广义后缀自动机

时间:2024-10-11 20:35:56

E - Three strings

将三个串加进去,看每个节点在三个串中分别出现了多少次。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std; const int N = 5e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, ans[N], len[];
char s[][N]; struct SuffixAutomaton {
int cur, cnt, ch[N<<][], id[N<<], fa[N<<], dis[N<<], sz[N<<], c[N];
int num[][N<<];
SuffixAutomaton() {cur = cnt = ;}
void init() {
for(int i = ; i <= cnt; i++) {
memset(ch[i], , sizeof(ch[i]));
sz[i] = c[i] = dis[i] = fa[i] = ;
}
cur = cnt = ;
}
int extend(int p, int c) {
cur = ++cnt; dis[cur] = dis[p]+;
for(; p && !ch[p][c]; p = fa[p]) ch[p][c] = cur;
if(!p) fa[cur] = ;
else {
int q = ch[p][c];
if(dis[q] == dis[p]+) fa[cur] = q;
else {
int nt = ++cnt; dis[nt] = dis[p]+;
memcpy(ch[nt], ch[q], sizeof(ch[q]));
fa[nt] = fa[q]; fa[q] = fa[cur] = nt;
for(; ch[p][c]==q; p=fa[p]) ch[p][c] = nt;
}
}
sz[cur] = ;
return cur;
}
void topo(int n) {
for(int i = ; i <= cnt; i++) c[dis[i]]++;
for(int i = ; i <= n; i++) c[i] += c[i-];
for(int i = cnt; i >= ; i--) id[c[dis[i]]--] = i;
}
void solve() {
for(int i = ; i < ; i++) {
scanf("%s", s[i]); len[i] = strlen(s[i]);
for(int j = , last = ; j < len[i]; j++)
last = extend(last, s[i][j]-'a');
}
for(int i = ; i < ; i++) {
for(int j = , p = ; j < len[i]; j++) {
p = ch[p][s[i][j]-'a'];
num[i][p]++;
}
}
topo(max(len[], max(len[], len[])));
for(int i = cnt; i >= ; i--)
for(int j = ; j < ; j++)
num[j][fa[id[i]]] += num[j][id[i]];
for(int i = ; i <= cnt; i++) {
int ret = 1ll*num[][i]*num[][i]%mod*num[][i]%mod;
int mx = dis[i], mn = dis[fa[i]]+;
ans[mn] = (ans[mn] + ret) % mod;
ans[mx+] = (ans[mx+]-ret+mod)%mod;
}
int Len = min(len[], min(len[], len[]));
for(int i = ; i <= Len; i++) {
ans[i] = (ans[i] + ans[i-]) % mod;
printf("%d ", ans[i]);
}
puts("");
}
} sam; int main() {
sam.solve();
return ;
} /*
*/