This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 300'000 + 10;
string s;
int n;
struct Hash {
int base, M;
vector<int> h, rh, pw;
Hash() : base(0), M(0) {}
Hash(string s, int base, int M) : base(base), M(M), h(s.size() + 1), rh(s.size() + 1), pw(s.size() + 1) {
pw[0] = 1;
for (int i = 1; i <= n; ++i) {
pw[i] = 1ll * pw[i - 1] * base % M;
h[i] = (1ll * h[i - 1] * base % M + s[i - 1]) % M;
rh[i] = (1ll * rh[i - 1] * base % M + s[n - i]) % M;
}
}
int get(int l, int r) {
return (h[r] - 1ll * h[l - 1] * pw[r - l + 1] % M + M) % M;
}
int rget(int l, int r) {
return (rh[r] - 1ll * rh[l - 1] * pw[r - l + 1] % M + M) % M;
}
} T[2];
using HASH = pair<int, int>;
inline HASH get(int l, int r) {
return make_pair(T[0].get(l, r), T[1].get(l, r));
}
inline HASH rget(int l, int r) {
tie(l, r) = make_pair(n - r + 1, n - l + 1);
return make_pair(T[0].rget(l, r), T[1].rget(l, r));
}
inline bool chk(int l, int r) { return get(l, r) == rget(l, r); }
int pos[N], tmp[N], sa[N];
int lcp[N];
void initSA() {
for (int i = 1; i <= n; ++i) pos[i] = s[i] - 'a' + 1, sa[i] = i;
for (int gap = 1; ; gap *= 2) {
auto cmp = [&](int i, int j) {
if (pos[i] != pos[j]) return pos[i] < pos[j];
i += gap; j += gap;
return i <= n && j <= n ? pos[i] < pos[j] : i > j;
};
sort(sa + 1, sa + n + 1, cmp);
for (int i = 2; i <= n; ++i) tmp[i] = tmp[i - 1] + cmp(sa[i - 1], sa[i]);
for (int i = 1; i <= n; ++i) pos[sa[i]] = tmp[i] + 1;
if (tmp[n] == n - 1) break;
}
for (int i = 1, k = 0; i <= n; ++i) {
if (pos[i] == n) continue;
for (int j = sa[pos[i] + 1]; s[i + k] == s[j + k]; ) k += 1;
lcp[i] = k;
if (k) k -= 1;
}
}
long long value[N];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> s;
n = s.size();
T[0] = Hash(s, 19937, 1e9 + 7);
T[1] = Hash(s, 10007, 1e9 + 9);
s = '@' + s;
initSA();
long long answer = 0;
for (int i = 1; i <= n; ++i) {
for (int j = lcp[sa[i - 1]]; j > lcp[sa[i]]; --j)
if (chk(sa[i - 1], sa[i - 1] + j - 1)) value[j] = 0;
for (int j = 1; j <= lcp[sa[i]]; ++j)
if (chk(sa[i], sa[i] + j - 1)) value[j] += j;
for (int j = 1; j <= n - sa[i] + 1; ++j)
if (chk(sa[i], sa[i] + j - 1)) answer = max(answer, value[j] + j);
}
cout << answer << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |