Submission #1106228

#TimeUsernameProblemLanguageResultExecution timeMemory
1106228ducanh1234Palindromes (APIO14_palindrome)C++17
100 / 100
680 ms36268 KiB
#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; } } int f[N][19], lg[N]; void init() { for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1; for (int i = 1; i <= n; ++i) f[i][0] = lcp[sa[i]]; for (int j = 1; j <= 18; ++j) { for (int i = 1; i <= n - (1 << j) + 1; ++i) f[i][j] = min(f[i][j - 1], f[i + (1 << j - 1)][j - 1]); } } inline int que(int l, int r) { int j = lg[r - l + 1]; return min(f[l][j], f[r - (1 << j) + 1][j]); } 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(); init(); auto count = [&](int l, int r) { int sz = r - l + 1; l = pos[l]; int value = 0; { // binarySearch left int le = 1, ri = l - 1, ret = l; while (le <= ri) { int mid = (le + ri) >> 1; if (que(mid, l - 1) >= sz) ri = (ret = mid) - 1; else le = mid + 1; } value -= ret - 1; } { //binarySearch right int le = l, ri = n - 1, ret = l; while (le <= ri) { int mid = (le + ri) >> 1; if (que(l, mid) >= sz) le = ret = mid + 1; else ri = mid - 1; } value += ret; } return value; }; long long answer = 0; for (int mid1 = 1; mid1 <= n; ++mid1) { for (int mid2 = mid1; mid2 <= min(n, mid1 + 1); ++mid2) { if (!chk(mid1, mid2)) continue; int sz = min(mid1, n - mid2); int l = 0, r = sz, ret = 0; while (l <= r) { int mid = (l + r) >> 1; if (chk(mid1 - mid, mid2 + mid)) l = (ret = mid) + 1; else r = mid - 1; } answer = max(answer, 1ll * count(mid1 - ret, mid2 + ret) * (mid2 - mid1 + 2 * ret + 1)); } } cout << answer << "\n"; }

Compilation message (stderr)

palindrome.cpp: In function 'void init()':
palindrome.cpp:72:48: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   72 |       f[i][j] = min(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
      |                                              ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...