Submission #561409

#TimeUsernameProblemLanguageResultExecution timeMemory
561409Ooops_sorryPalindromes (APIO14_palindrome)C++14
23 / 100
1096 ms38748 KiB
#include<bits/stdc++.h> using namespace std; #define pb push_back #define ld long double #define ll long long mt19937 rnd(51); const int INF = 1e9, MOD1 = 1e9 + 7, p1 = 31, MOD2 = 1e9 + 33, p2 = 37; bool is_palindrome(string s) { return s == (string){s.rbegin(), s.rend()}; } int solve1(string s) { int n = s.size(), ans = -INF; map<string, int> cnt; for (int i = 0; i < n; i++) { for (int j = 1; j + i <= n; j++) { cnt[s.substr(i, j)]++; } } for (int i = 0; i < n; i++) { for (int j = n - i; j >= 1; j--) { if (is_palindrome(s.substr(i, j))) { ans = max(ans, cnt[s.substr(i, j)] * j); } } } return ans; } struct Hash { ll MOD, p; vector<ll> po, h; Hash(string s, ll MOD_, ll p_) { MOD = MOD_, p = p_; int n = s.size(); po.resize(n + 1); h.resize(n + 1); po[0] = 1; for (int i = 1; i <= n; i++) { po[i] = (po[i - 1] * p) % MOD; } for (int i = 0; i < n; i++) { h[i + 1] = (h[i] * p + s[i] - 'a' + 1) % MOD; } } int get(int l, int r) { r++; return (((h[r] - h[l] * po[r - l]) % MOD) + MOD) % MOD; } }; struct SegTree { vector<int> t; SegTree(int n) { t.resize(4 * n, -1); } void update(int v, int tl, int tr, int l, int r, int val) { if (l > r) return; if (tl == l && tr == r) { t[v] = max(t[v], val); return; } int tm = (tl + tr) / 2; update(2 * v, tl, tm, l, min(r, tm), val), update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, val); } int get(int v, int l, int r, int pos) { if (l == r) { return t[v]; } int m = (l + r) / 2; if (pos <= m) { return max(t[v], get(2 * v, l, m, pos)); } else { return max(t[v], get(2 * v + 1, m + 1, r, pos)); } } }; int solve2(string s) { int n = s.size(), ans = -INF; map<int, int> cnt; Hash h(s, MOD1, p1), hrev((string){s.rbegin(), s.rend()}, MOD1, p1); for (int i = 0; i < n; i++) { for (int j = 1; j + i <= n; j++) { cnt[h.get(i, i + j - 1)]++; } } SegTree odd(n), even(n); for (int i = 0; i < n; i++) { int l = 0, r = n; while (r - l > 1) { int mid = (r + l) / 2; if (i - mid >= 0 && i + mid < n && h.get(i - mid, i + mid) == hrev.get(n - i - mid - 1, n - i + mid - 1)) { l = mid; } else { r = mid; } } odd.update(1, 0, n - 1, i - l, i, i); if (i + 1 < n && s[i] == s[i + 1]) { int l = 0, r = n; while (r - l > 1) { int mid = (r + l) / 2; if (i - mid >= 0 && i + mid + 1 < n && h.get(i - mid, i + mid + 1) == hrev.get(n - i - mid - 2, n - i + mid - 1)) { l = mid; } else { r = mid; } } even.update(1, 0, n - 1, i - l, i, i); } } for (int i = 0; i < n; i++) { int j = odd.get(1, 0, n - 1, i), k = even.get(1, 0, n - 1, i), pos = -1; if (j != -1) { pos = max(pos, i + 2 * (j - i)); } if (k != -1) { pos = max(pos, i + 1 + 2 * (k - i)); } if (pos != -1) { ans = max(ans, cnt[h.get(i, pos)] * (pos - i + 1)); } } return ans; } signed main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif // LCOAL ios_base::sync_with_stdio(0); cin.tie(0); while (0) { int n = rnd() % 10 + 1; string s = ""; for (int i = 0; i < n; i++) { s += rnd() % 5 + 'a'; } auto res1 = solve1(s), res2 = solve2(s); if (res1 != res2) { cout << s << endl << res1 << ' ' << res2 << endl; return 0; } cout << "ok" << endl; } string s; cin >> s; cout << solve2(s) << endl; return 0; }
#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...