Submission #647897

# Submission time Handle Problem Language Result Execution time Memory
647897 2022-10-04T12:10:32 Z ghostwriter Palindromes (APIO14_palindrome) C++14
100 / 100
980 ms 41432 KB
#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

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 time Memory Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 3 ms 2636 KB Output is correct
3 Correct 3 ms 2644 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 3 ms 2644 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 3 ms 2644 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 3 ms 2644 KB Output is correct
12 Correct 3 ms 2644 KB Output is correct
13 Correct 3 ms 2644 KB Output is correct
14 Correct 3 ms 2644 KB Output is correct
15 Correct 3 ms 2644 KB Output is correct
16 Correct 3 ms 2644 KB Output is correct
17 Correct 4 ms 2644 KB Output is correct
18 Correct 3 ms 2644 KB Output is correct
19 Correct 3 ms 2644 KB Output is correct
20 Correct 3 ms 2644 KB Output is correct
21 Correct 3 ms 2644 KB Output is correct
22 Correct 3 ms 2644 KB Output is correct
23 Correct 3 ms 2644 KB Output is correct
24 Correct 3 ms 2644 KB Output is correct
25 Correct 3 ms 2644 KB Output is correct
26 Correct 3 ms 2644 KB Output is correct
27 Correct 3 ms 2644 KB Output is correct
28 Correct 3 ms 2644 KB Output is correct
29 Correct 3 ms 2644 KB Output is correct
30 Correct 3 ms 2644 KB Output is correct
31 Correct 3 ms 2644 KB Output is correct
32 Correct 3 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2772 KB Output is correct
2 Correct 4 ms 2772 KB Output is correct
3 Correct 4 ms 2812 KB Output is correct
4 Correct 4 ms 2772 KB Output is correct
5 Correct 4 ms 2772 KB Output is correct
6 Correct 4 ms 2772 KB Output is correct
7 Correct 4 ms 2772 KB Output is correct
8 Correct 4 ms 2772 KB Output is correct
9 Correct 4 ms 2760 KB Output is correct
10 Correct 4 ms 2644 KB Output is correct
11 Correct 4 ms 2644 KB Output is correct
12 Correct 4 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 3796 KB Output is correct
2 Correct 15 ms 3796 KB Output is correct
3 Correct 20 ms 3880 KB Output is correct
4 Correct 19 ms 3940 KB Output is correct
5 Correct 13 ms 3840 KB Output is correct
6 Correct 15 ms 3792 KB Output is correct
7 Correct 16 ms 3836 KB Output is correct
8 Correct 6 ms 2984 KB Output is correct
9 Correct 6 ms 3020 KB Output is correct
10 Correct 11 ms 3540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 174 ms 13692 KB Output is correct
2 Correct 182 ms 13656 KB Output is correct
3 Correct 244 ms 15184 KB Output is correct
4 Correct 238 ms 15240 KB Output is correct
5 Correct 134 ms 13660 KB Output is correct
6 Correct 112 ms 11796 KB Output is correct
7 Correct 162 ms 13428 KB Output is correct
8 Correct 28 ms 5524 KB Output is correct
9 Correct 70 ms 7500 KB Output is correct
10 Correct 123 ms 13440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 662 ms 37364 KB Output is correct
2 Correct 631 ms 37316 KB Output is correct
3 Correct 980 ms 41136 KB Output is correct
4 Correct 901 ms 41432 KB Output is correct
5 Correct 480 ms 37612 KB Output is correct
6 Correct 589 ms 35812 KB Output is correct
7 Correct 611 ms 33832 KB Output is correct
8 Correct 86 ms 12532 KB Output is correct
9 Correct 84 ms 12544 KB Output is correct
10 Correct 467 ms 33276 KB Output is correct
11 Correct 575 ms 37696 KB Output is correct
12 Correct 133 ms 13144 KB Output is correct