作者:花落酒未醉 | 来源:互联网 | 2024-12-06 19:46
本文详细探讨了如何使用广义后缀自动机解决MemSQLStart[c]UP2.0竞赛第一轮E题——Threestrings。通过构建和分析后缀自动机,我们能够高效地统计多个字符串中子串出现的频率。
本文旨在深入解析广义后缀自动机(Generalized Suffix Automaton, GSA)在字符串处理问题中的应用,特别是在MemSQL Start[c]UP 2.0竞赛第一轮E题——Three strings中的具体实现。通过本题,我们可以学习到如何利用GSA来高效解决多字符串的子串匹配与计数问题。
问题描述:给定三个字符串,任务是计算每个可能长度的子串在这三个字符串中同时出现的次数,并输出这些计数结果。
#include
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair
#define PLI pair
#define ull unsigned long long
using namespace std;
const int N = 5e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
int n, ans[N], len[3];
char s[3][N];
struct SuffixAutomaton {
int cur, cnt, ch[N<<1][26], id[N<<1], fa[N<<1], dis[N<<1], sz[N<<1], c[N];
int num[3][N<<1];
SuffixAutomaton() : cur(1), cnt(1) {}
void init() {
for(int i = 1; i <= cnt; i++) {
memset(ch[i], 0, sizeof(ch[i]));
sz[i] = c[i] = dis[i] = fa[i] = 0;
}
cur = cnt = 1;
}
int extend(int p, int c) {
cur = ++cnt; dis[cur] = dis[p] + 1;
while(p && !ch[p][c]) ch[p][c] = cur, p = fa[p];
if(!p) fa[cur] = 1;
else {
int q = ch[p][c];
if(dis[q] == dis[p] + 1) fa[cur] = q;
else {
int nt = ++cnt; dis[nt] = dis[p] + 1;
memcpy(ch[nt], ch[q], sizeof(ch[q]));
fa[nt] = fa[q]; fa[q] = fa[cur] = nt;
while(ch[p][c] == q) ch[p][c] = nt, p = fa[p];
}
}
sz[cur] = 1;
return cur;
}
void topo(int n) {
for(int i = 1; i <= cnt; i++) c[dis[i]]++;
for(int i = 1; i <= n; i++) c[i] += c[i-1];
for(int i = cnt; i >= 1; i--) id[c[dis[i]]--] = i;
}
void solve() {
for(int i = 0; i <3; i++) {
scanf("%s", s[i]); len[i] = strlen(s[i]);
for(int j = 0, last = 1; j last = extend(last, s[i][j] - 'a');
}
for(int i = 0; i <3; i++) {
for(int j = 0, p = 1; j p = ch[p][s[i][j] - 'a'];
num[i][p]++;
}
}
topo(max(len[0], max(len[1], len[2])));
for(int i = cnt; i >= 1; i--)
for(int j = 0; j <3; j++)
num[j][fa[id[i]]] += num[j][id[i]];
for(int i = 2; i <= cnt; i++) {
int ret = 1ll * num[0][i] * num[1][i] % mod * num[2][i] % mod;
int mx = dis[i], mn = dis[fa[i]] + 1;
ans[mn] = (ans[mn] + ret) % mod;
ans[mx + 1] = (ans[mx + 1] - ret + mod) % mod;
}
int Len = min(len[0], min(len[1], len[2]));
for(int i = 1; i <= Len; i++) {
ans[i] = (ans[i] + ans[i-1]) % mod;
printf("%d ", ans[i]);
}
puts("");
}
} sam;
int main() {
sam.solve();
return 0;
}