# 【BZOJ4650】【UOJ219】【NOI2016】优秀的拆分

xiaoxiao2021-03-01  2

【题目链接】

BZOJUOJ

【思路要点】

【代码】

#include<bits/stdc++.h> using namespace std; const int MAXN = 60005; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } namespace SuffixArray { const int MAXN = 60005; const int MAXLOG = 20; const int MAXC = 256; int sa[MAXN], rank[MAXN], height[MAXN]; int Min[MAXN][MAXLOG], bit[MAXN], N; void init(char *a, int n) { memset(sa, 0, sizeof(sa)); memset(rank, 0, sizeof(rank)); memset(height, 0, sizeof(height)); memset(Min, 0, sizeof(Min)); N = n; static int x[MAXN], y[MAXN], cnt[MAXN], rk[MAXN]; memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= n; i++) cnt[(int) a[i]]++; for (int i = 1; i <= MAXC; i++) cnt[i] += cnt[i - 1]; for (int i = n; i >= 1; i--) sa[cnt[(int) a[i]]--] = i; rank[sa[1]] = 1; for (int i = 2; i <= n; i++) rank[sa[i]] = rank[sa[i - 1]] + (a[sa[i]] != a[sa[i - 1]]); for (int k = 1; rank[sa[n]] != n; k <<= 1) { for (int i = 1; i <= n; i++) { x[i] = rank[i]; y[i] = (i + k <= n) ? rank[i + k] : 0; } memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= n; i++) cnt[y[i]]++; for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1]; for (int i = n; i >= 1; i--) rk[cnt[y[i]]--] = i; memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= n; i++) cnt[x[i]]++; for (int i = 1; i <= n; i++) cnt[i] += cnt[i - 1]; for (int i = n; i >= 1; i--) sa[cnt[x[rk[i]]]--] = rk[i]; rank[sa[1]] = 1; for (int i = 2; i <= n; i++) rank[sa[i]] = rank[sa[i - 1]] + (x[sa[i]] != x[sa[i - 1]] || y[sa[i]] != y[sa[i - 1]]); } int now = 0; for (int i = 1; i <= n; i++) { if (now) now--; while (a[i + now] == a[sa[rank[i] + 1] + now]) now++; height[rank[i]] = now; } for (int i = 1; i <= n; i++) Min[i][0] = height[i]; for (int p = 1; p < MAXLOG; p++) { int tmp = 1 << (p - 1); for (int i = 1, j = tmp + 1; j <= n; i++, j++) Min[i][p] = min(Min[i][p - 1], Min[i + tmp][p - 1]); } for (int i = 1; i <= n; i++) { bit[i] = bit[i - 1]; if (i >= 1 << (bit[i] + 1)) bit[i]++; } } int lcp(int x, int y) { if (x == y) return N - x + 1; x = rank[x], y = rank[y]; if (x > y) swap(x, y); int tmp = bit[y - x]; return min(Min[x][tmp], Min[y - (1 << tmp)][tmp]); } } char s[MAXN]; int pre[MAXN], suf[MAXN]; int main() { int T; read(T); while (T--) { scanf("%s", s + 1); int n = strlen(s + 1); int tmp = n; s[++tmp] = '#'; for (int i = n; i >= 1; i--) s[++tmp] = s[i]; s[tmp + 1] = 0; SuffixArray::init(s, tmp); tmp = 2 * n + 2; memset(pre, 0, sizeof(pre)); memset(suf, 0, sizeof(suf)); for (int len = 1; len <= n; len++) { for (int i = 1, j = len + 1; j <= n; i += len, j += len) { int s = SuffixArray::lcp(i, j); int p = SuffixArray::lcp(tmp - i, tmp - j); chkmin(s, len), chkmin(p, len); int l = i - p + len * 2, r = j + s - 1; if (l <= r) { pre[l]++; pre[r + 1]--; } l = i - p + 1, r = j + s - len * 2; if (l <= r) { suf[l]++; suf[r + 1]--; } } } for (int i = 1; i <= n; i++) { pre[i] += pre[i - 1]; suf[i] += suf[i - 1]; } long long ans = 0; for (int i = 1; i <= n - 1; i++) ans += 1ll * pre[i] * suf[i + 1]; writeln(ans); } return 0; }