Submission #647897

#TimeUsernameProblemLanguageResultExecution timeMemory
647897ghostwriterPalindromes (APIO14_palindrome)C++14
100 / 100
980 ms41432 KiB
#pragma GCC optimize ("Ofast") #pragma GCC target ("avx2") #include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include <debug.h> #else #define debug(...) #endif #define ft front #define bk back #define st first #define nd second #define ins insert #define ers erase #define pb push_back #define pf push_front #define _pb pop_back #define _pf pop_front #define lb lower_bound #define ub upper_bound #define mtp make_tuple #define bg begin #define ed end #define all(x) (x).bg(), (x).ed() #define sz(x) (int)(x).size() typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair<int, int> pi; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll; typedef string str; template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); } template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i)) #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i)) #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i)) #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i)) #define EACH(i, x) for (auto &(i) : (x)) #define WHILE while #define file "TEST" mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); } /* ---------------------------------------------------------------- END OF TEMPLATE ---------------------------------------------------------------- Tran The Bao - ghostwriter Training for VOI23 gold medal ---------------------------------------------------------------- DIT ME CHUYEN BAO LOC ---------------------------------------------------------------- */ const pi M = {1e9 + 7, 1e9 + 9}; const pi base = {37, 127}; const int N = 3e5 + 5; int n, d[N]; str s; pi p[N], h[N], h1[N]; vpi v, v1, v2; map<pi, bool> d1; ll rs = 0; pi geth(const int &l, const int &r) { pi ans; ans.st = (h[r].st - 1LL * h[l - 1].st * p[r - l + 1].st % M.st + M.st) % M.st; ans.nd = (h[r].nd - 1LL * h[l - 1].nd * p[r - l + 1].nd % M.nd + M.nd) % M.nd; return ans; } pi geth1(const int &l, const int &r) { pi ans; ans.st = (h1[l].st - 1LL * h1[r + 1].st * p[r - l + 1].st % M.st + M.st) % M.st; ans.nd = (h1[l].nd - 1LL * h1[r + 1].nd * p[r - l + 1].nd % M.nd + M.nd) % M.nd; return ans; } bool cmp(const pi &a, const pi &b) { return a.nd - a.st + 1 < b.nd - b.st + 1; } int getpos(const pi &x) { if (x > v1.bk()) return -1; int ans = lb(all(v1), x) - v1.bg(); return x == v1[ans]? ans : -1; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); // freopen(file".inp", "r", stdin); // freopen(file".out", "w", stdout); p[0] = {1, 1}; FOR(i, 1, N - 1) { p[i].st = 1LL * p[i - 1].st * base.st % M.st; p[i].nd = 1LL * p[i - 1].nd * base.nd % M.nd; } cin >> s; n = sz(s); s = "#" + s + "#"; FOR(i, 1, n) { h[i].st = (1LL * h[i - 1].st * base.st % M.st + s[i] - 'a' + 1) % M.st; h[i].nd = (1LL * h[i - 1].nd * base.nd % M.nd + s[i] - 'a' + 1) % M.nd; } FOS(i, n, 1) { h1[i].st = (1LL * h1[i + 1].st * base.st % M.st + s[i] - 'a' + 1) % M.st; h1[i].nd = (1LL * h1[i + 1].nd * base.nd % M.nd + s[i] - 'a' + 1) % M.nd; } FOR(i, 1, n) { int l = 1, r = min(i - 1, n - i), ans = 0; WHILE(l <= r) { int mid = l + (r - l) / 2; if (geth(i - mid, i + mid) == geth1(i - mid, i + mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } l = i - ans; r = i + ans; pi curh = geth(l, r); v2.pb(curh); WHILE(!d1[curh]) { v.pb({l, r}); d1[curh] = 1; ++l; --r; if (l > r) break; curh = geth(l, r); } } FOR(i, 1, n - 1) { if (s[i] != s[i + 1]) continue; int l = 1, r = min(i - 1, n - i - 1), ans = 0; WHILE(l <= r) { int mid = l + (r - l) / 2; if (geth(i - mid, i + 1 + mid) == geth1(i - mid, i + 1 + mid)) { ans = mid; l = mid + 1; } else r = mid - 1; } l = i - ans; r = i + 1 + ans; pi curh = geth(l, r); v2.pb(curh); WHILE(!d1[curh]) { v.pb({l, r}); d1[curh] = 1; ++l; --r; if (l > r) break; curh = geth(l, r); } } sort(all(v), cmp); EACH(i, v) v1.pb(geth(i.st, i.nd)); sort(all(v1)); EACH(i, v2) ++d[getpos(i)]; FSN(i, sz(v)) { int ln = v[i].st, rn = v[i].nd, len = rn - ln + 1; int posh = getpos(geth(ln, rn)); rs = max(rs, 1LL * d[posh] * len); if (ln + 1 <= rn - 1) d[getpos(geth(ln + 1, rn - 1))] += d[posh]; } cout << rs; return 0; } /* abbabaaa ---------------------------------------------------------------- From Benq: stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH ---------------------------------------------------------------- */

Compilation message (stderr)

palindrome.cpp: In function 'int main()':
palindrome.cpp:34:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   34 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
palindrome.cpp:81:5: note: in expansion of macro 'FOR'
   81 |     FOR(i, 1, N - 1) {
      |     ^~~
palindrome.cpp:34:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   34 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
palindrome.cpp:88:5: note: in expansion of macro 'FOR'
   88 |     FOR(i, 1, n) {
      |     ^~~
palindrome.cpp:35:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   35 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
palindrome.cpp:92:5: note: in expansion of macro 'FOS'
   92 |     FOS(i, n, 1) {
      |     ^~~
palindrome.cpp:34:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   34 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
palindrome.cpp:96:5: note: in expansion of macro 'FOR'
   96 |     FOR(i, 1, n) {
      |     ^~~
palindrome.cpp:34:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   34 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
palindrome.cpp:119:5: note: in expansion of macro 'FOR'
  119 |     FOR(i, 1, n - 1) {
      |     ^~~
palindrome.cpp:38:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   38 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
palindrome.cpp:144:5: note: in expansion of macro 'EACH'
  144 |     EACH(i, v) v1.pb(geth(i.st, i.nd));
      |     ^~~~
palindrome.cpp:38:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   38 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
palindrome.cpp:146:5: note: in expansion of macro 'EACH'
  146 |     EACH(i, v2) ++d[getpos(i)];
      |     ^~~~
palindrome.cpp:37:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   37 | #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
      |                            ^
palindrome.cpp:147:5: note: in expansion of macro 'FSN'
  147 |     FSN(i, sz(v)) {
      |     ^~~
#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...