1 条题解
-
0
题目分析
题目要求计算字符串所有前缀的神秘度,即统计满足条件的三元组 ((i,j,\text{len})) 的贡献和。核心在于高效识别字符串中的重复子串(重复运行),并计算每个重复结构对神秘度的贡献。
解题思路
- 重复运行(Run)识别:利用后缀数组(SA)和Lyndon分解找到字符串中的所有重复运行(Run),每个Run表示一个具有最小周期 (p) 的最长重复区间 ([u, v))。
- 贡献计算:对于每个Run,计算其内部所有合法三元组的贡献。具体来说,对于周期为 (p)、长度为 (L) 的Run,枚举子串长度 (\text{len}),统计满足条件的 ((i,j)) 对数并累加贡献。
- 前缀和累加:将每个位置的贡献累加,得到所有前缀的神秘度。
代码实现(带注释)
#include <cassert> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using Int = long long; // 输出重载,方便调试 template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) os << ", "; os << as[i]; } return os << "]"; } template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") // 模数类,支持模运算 template <unsigned M_> struct ModInt { static constexpr unsigned M = M_; unsigned x; constexpr ModInt() : x(0U) {} constexpr ModInt(unsigned x_) : x(x_ % M) {} constexpr ModInt(unsigned long long x_) : x(x_ % M) {} constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {} constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {} ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; } ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; } ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; } ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); } ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b; } ModInt inv() const { unsigned a = M, b = x; int y = 0, z = 1; for (; b;) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; } assert(a == 1U); return ModInt(y); } ModInt operator+() const { return *this; } ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; } ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); } ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); } ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); } ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); } template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); } template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); } template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); } template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); } explicit operator bool() const { return x; } bool operator==(const ModInt &a) const { return (x == a.x); } bool operator!=(const ModInt &a) const { return (x != a.x); } friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; } }; constexpr unsigned MO = 1000000007; using Mint = ModInt<MO>; // SA-IS算法:构造后缀数组 template <class String> vector<int> suffixArrayRec(const String &as) { const int n = as.size(); if (n == 0) return {}; const auto minmaxA = minmax_element(as.begin(), as.end()); const auto minA = *minmaxA.first, maxA = *minmaxA.second; if (static_cast<unsigned long long>(maxA) - minA >= static_cast<unsigned long long>(n)) { vector<int> us(n); for (int u = 0; u < n; ++u) us[u] = u; std::sort(us.begin(), us.end(), [&](int u, int v) -> bool { return (as[u] < as[v]); }); int b = 0; vector<int> bs(n, 0); for (int i = 1; i < n; ++i) { if (as[us[i - 1]] != as[us[i]]) ++b; bs[us[i]] = b; } return suffixArrayRec(bs); } const int sigma = maxA - minA + 1; vector<int> pt(sigma + 1, 0), poss(sigma); for (int u = 0; u < n; ++u) ++pt[as[u] - minA + 1]; for (int a = 0; a < sigma; ++a) pt[a + 1] += pt[a]; vector<bool> cmp(n); cmp[n - 1] = false; for (int u = n - 1; --u >= 0;) { cmp[u] = (as[u] != as[u + 1]) ? (as[u] < as[u + 1]) : cmp[u + 1]; } int nn = 0; vector<int> ids(n, 0); int last = n; vector<int> nxt(n, 0); vector<int> us(n, 0); memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int u = n - 1; --u >= 1;) if (!cmp[u - 1] && cmp[u]) { ids[u] = ++nn; nxt[u] = last; last = u; us[--poss[as[u] - minA]] = u; } memcpy(poss.data(), pt.data(), sigma * sizeof(int)); us[poss[as[n - 1] - minA]++] = n - 1; for (int i = 0; i < n; ++i) { const int u = us[i]; if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1; } memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int i = n; --i >= 0;) { const int u = us[i]; if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1; } if (nn) { int vsLen = 0; vector<int> vs(nn); for (const int u : us) if (ids[u]) vs[vsLen++] = u; int b = 0; vector<int> bs(nn, 0); for (int j = 1; j < nn; ++j) { int v1 = vs[j - 1], v0 = vs[j]; const int w1 = nxt[v1], w0 = nxt[v0]; if (w1 - v1 != w0 - v0) { ++b; } else { for (; ; ++v1, ++v0) { if (v1 == n) { ++b; break; } if (as[v1] != as[v0]) { ++b; break; } if (v1 == w1) break; } } bs[nn - ids[vs[j]]] = b; } for (int u = 0; u < n; ++u) if (ids[u]) vs[nn - ids[u]] = u; const auto sub = suffixArrayRec(bs); memset(us.data(), 0, n * sizeof(int)); memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int j = nn; --j >= 0;) { const int u = vs[sub[j]]; us[--poss[as[u] - minA]] = u; } memcpy(poss.data(), pt.data(), sigma * sizeof(int)); us[poss[as[n - 1] - minA]++] = n - 1; for (int i = 0; i < n; ++i) { const int u = us[i]; if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1; } memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int)); for (int i = n; --i >= 0;) { const int u = us[i]; if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1; } } return us; } // 后缀数组结构体,包含SA、rank、LCP struct SuffixArray { int n; bool rmq; vector<int> us, su, hs; SuffixArray() : n(0), rmq(false) {} SuffixArray(const string &as, bool rmq_) : rmq(rmq_) { build(as); } SuffixArray(const vector<int> &as, bool rmq_) : rmq(rmq_) { build(as); } SuffixArray(const vector<long long> &as, bool rmq_) : rmq(rmq_) { build(as); } template <class String> void build(const String &as) { n = as.size(); us = suffixArrayRec(as); su.resize(n + 1); for (int i = 0; i < n; ++i) su[us[i]] = i; su[n] = -1; hs.assign(n, 0); for (int h = 0, u = 0; u < n; ++u) if (su[u]) { for (int v = us[su[u] - 1]; v + h < n && as[v + h] == as[u + h]; ++h) {} hs[su[u]] = h; if (h) --h; } if (rmq) { const int logN = n ? (31 - __builtin_clz(n)) : 0; hs.resize((logN + 1) * n - (1 << logN) + 1); for (int e = 0; e < logN; ++e) { int *hes = hs.data() + e * n; for (int i = 0; i <= n - (1 << (e + 1)); ++i) { hes[n + i] = min(hes[i], hes[i + (1 << e)]); } } } } // 查询两个后缀的最长公共前缀 inline int lcp(int u, int v) const { if (u == v) return n - u; int i = su[u], j = su[v]; if (i > j) swap(i, j); const int e = 31 - __builtin_clz(j - i); return min(hs[e * n + i + 1], hs[e * n + j + 1 - (1 << e)]); } }; // Lyndon分解:求最长Lyndon前缀 template <class String> vector<pair<int, int>> lyndonSuffix(const String &as) { const int n = as.size(); vector<pair<int, int>> pqs(n + 1); pqs[0] = make_pair(0, 0); for (int u = 0; u < n;) { for (int p = 1, q = 1, r = 0, v = u + 1; ; ++v) { pqs[v] = (r != 0) ? pqs[u + r] : make_pair(p, q); if (v == n || as[v - p] > as[v]) { u = v - r; break; } else if (as[v - p] < as[v]) { p = v + 1 - u; q = 1; r = 0; } else { if (++r == p) { ++q; r = 0; } } } } return pqs; } // 求最长Lyndon前缀 template <class String> vector<int> lyndonPrefix(const String &as, const SuffixArray &sa) { const int n = as.size(); int stackLen = 0; vector<int> stack(n + 1); vector<int> vs(n); for (int u = 0; u <= n; ++u) { for (; stackLen > 0 && sa.su[stack[stackLen - 1]] > sa.su[u]; --stackLen) { vs[stack[stackLen - 1]] = u; } stack[stackLen++] = u; } return vs; } // 反转字符串的最长Lyndon前缀 template <class String> vector<int> lyndonPrefixInverted(const String &as, const SuffixArray &sa) { assert(sa.rmq); const int n = as.size(); int stackLen = 0; vector<int> stack(n + 1); vector<int> vs(n); for (int u = 0; u <= n; ++u) { for (; stackLen > 0; --stackLen) { const int uu = stack[stackLen - 1]; const int l = sa.lcp(uu, u); if (u + l < n && as[uu + l] > as[u + l]) break; vs[uu] = u; } stack[stackLen++] = u; } return vs; } // 寻找所有重复运行(Run) template <class String> vector<pair<int, pair<int, int>>> repetitions(const String &as, const SuffixArray &sa) { assert(sa.rmq); const int n = as.size(); if (n == 0) return {}; String asRev = as; std::reverse(asRev.begin(), asRev.end()); const SuffixArray saRev(asRev, /*rmq=*/true); const vector<int> vs = lyndonPrefix(as, sa); const vector<int> vsInverted = lyndonPrefixInverted(as, sa); vector<pair<int, pair<int, int>>> runs; for (int u = 0; u < n; ++u) { { const int v = vs[u]; const int p = v - u, uu = u - saRev.lcp(n - u, n - v), vv = v + sa.lcp(u, v); if (vv - uu >= 2 * p) runs.emplace_back(p, make_pair(uu, vv)); } if (vs[u] != vsInverted[u]) { const int v = vsInverted[u]; const int p = v - u, uu = u - saRev.lcp(n - u, n - v), vv = v + sa.lcp(u, v); if (vv - uu >= 2 * p) runs.emplace_back(p, make_pair(uu, vv)); } } const int runsLen = runs.size(); auto runsWork = runs; vector<int> pt(n + 1, 0); for (int i = 0; i < runsLen; ++i) ++pt[runs[i].second.first]; for (int u = 0; u < n - 1; ++u) pt[u + 1] += pt[u]; for (int i = runsLen; --i >= 0;) runsWork[--pt[runs[i].second.first]] = runs[i]; memset(pt.data() + 1, 0, n * sizeof(int)); for (int i = 0; i < runsLen; ++i) ++pt[runsWork[i].first]; for (int p = 1; p < n; ++p) pt[p + 1] += pt[p]; for (int i = runsLen; --i >= 0;) runs[--pt[runsWork[i].first]] = runsWork[i]; runs.erase(std::unique(runs.begin(), runs.end()), runs.end()); return runs; } int N; char S[500'010]; int main() { for (; ~scanf("%s", S);) { N = strlen(S); vector<Mint> ans(N + 1, 0); const auto runs = repetitions(string(S)); // 找到所有重复运行 // 遍历每个重复运行,计算贡献 for (const auto &run : runs) { const int p = run.first; // 最小周期 const int i = run.second.first; // 起始位置 const int j = run.second.second; // 结束位置 Mint sum = 0; // 枚举子串长度l for (int l = 2 * p + 1; l <= j - i; ++l) { // 计算满足条件的k的数量及贡献 const Int limK = (l - 1) / (2 * p); ans[i + l] += sum += (l * limK - p * (limK * (limK + 1) / 2)); } } // 前缀和累加,得到每个前缀的神秘度 for (int i = 1; i <= N; ++i) ans[i] += ans[i - 1]; // 输出结果 for (int i = 1; i <= N; ++i) printf("%u\n", ans[i].x); } return 0; }代码解释
- 模数类:封装模运算操作,支持加法、乘法、逆元等,确保计算过程中数值不溢出。
- 后缀数组:使用SA-IS算法构造后缀数组,高效计算后缀排序、rank值和LCP(最长公共前缀)。
- Lyndon分解:找到字符串中的Lyndon前缀,用于识别重复运行。
- 重复运行识别:通过后缀数组和Lyndon分解找到所有重复运行(Run),每个Run表示一个具有最小周期的最长重复区间。
- 贡献计算:对每个Run,枚举子串长度,统计满足条件的三元组贡献,并累加到对应前缀位置。
- 前缀和输出:将每个位置的贡献累加,输出所有前缀的神秘度。
- 1
信息
- ID
- 5667
- 时间
- 3000ms
- 内存
- 512MiB
- 难度
- 10
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者