Submission #375874

# Submission time Handle Problem Language Result Execution time Memory
375874 2021-03-10T07:09:24 Z KoD Palindromes (APIO14_palindrome) C++17
100 / 100
624 ms 53792 KB
#include <bits/extc++.h>

using ll = long long;

template <class T>
using Vec = std::vector<T>;

template <class T, class U>
using HashTable = __gnu_pbds::gp_hash_table<T, U>;

ll rand_ll(const ll low, const ll high) {
    static std::random_device dev;
    static std::default_random_engine gen(dev() ^ std::clock());
    return std::uniform_int_distribution<ll>(low, high)(gen);
}

constexpr ll MOD = (1ll << 61) - 1;
const ll BASE = rand_ll(0, MOD - 1);

constexpr ll sum(ll x, const ll y) {
    x += y;
    return (x >= MOD ? x - MOD : x);
}

constexpr ll prod(const ll x, const ll y) {
    return ((__int128_t) x * y) % MOD;
}


struct RollingHash {
    Vec<ll> pow, dp;
    RollingHash(const std::string &str) {
        const int size = (int) str.size();
        pow.resize(size + 1);
        dp.resize(size + 1);
        pow[0] = 1;
        dp[1] = 0;
        for (int i = 0; i < size; ++i) {
            pow[i + 1] = prod(pow[i], BASE);
            dp[i + 1] = sum(prod(dp[i], BASE), (ll) str[i]);
        }
    }
    ll hash(const int l, const int r) const {
        return sum(dp[r], MOD - prod(dp[l], pow[r - l]));
    }
};


struct Range {
    int l, r;
    Range(const int l, const int r): l(l), r(r) { }
    bool operator < (const Range &other) const {
        return r - l < other.r - other.l;
    }
};

int main() {
    std::string S;
    std::cin >> S;
    const int N = (int) S.size();
    RollingHash rh(S);
    RollingHash rh_rev(std::string(S.rbegin(), S.rend()));
    const auto is_palindrome = [&](const int l, const int r) {
        if (l < 0 || r > N) {
            return false;
        }
        return rh.hash(l, r) == rh_rev.hash(N - r, N - l);
    };
    std::priority_queue<Range> heap;
    HashTable<ll, int> count;
    const auto add = [&](const int l, const int r, const int x) {
        const auto hash = rh.hash(l, r);
        if (count[hash] == 0) {
            heap.emplace(l, r);
        }
        count[hash] += x;
    };
    for (int i = 0; i < N; ++i) {
        int ok = 0, ng = N + 1;
        while (ng - ok > 1) {
            const auto md = (ok + ng) / 2;
            if (is_palindrome(i - md, i + md + 1)) {
                ok = md;
            }   
            else {
                ng = md;
            }
        }
        add(i - ok, i + ok + 1, 1);
    }   
    for (int i = 1; i < N; ++i) {
        if (S[i - 1] != S[i]) {
            continue;
        }
        int ok = 1, ng = N + 1;
        while (ng - ok > 1) {
            const auto md = (ok + ng) / 2;
            if (is_palindrome(i - md, i + md)) {
                ok = md;
            }   
            else {
                ng = md;
            }
        }
        add(i - ok, i + ok, 1);
    }
    ll ans = 0;
    while (!heap.empty()) {
        const auto [l, r] = heap.top();
        heap.pop();
        const auto c = count[rh.hash(l, r)];
        ans = std::max(ans, (ll) c * (r - l));
        if (r - l > 2) {
            add(l + 1, r - 1, c);
        }
    }
    std::cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 2056 KB Output is correct
2 Correct 9 ms 1932 KB Output is correct
3 Correct 10 ms 2056 KB Output is correct
4 Correct 12 ms 2056 KB Output is correct
5 Correct 8 ms 1908 KB Output is correct
6 Correct 10 ms 1932 KB Output is correct
7 Correct 11 ms 2056 KB Output is correct
8 Correct 6 ms 748 KB Output is correct
9 Correct 6 ms 748 KB Output is correct
10 Correct 9 ms 1336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 14740 KB Output is correct
2 Correct 110 ms 14608 KB Output is correct
3 Correct 121 ms 14736 KB Output is correct
4 Correct 155 ms 14736 KB Output is correct
5 Correct 90 ms 13212 KB Output is correct
6 Correct 100 ms 13340 KB Output is correct
7 Correct 158 ms 13848 KB Output is correct
8 Correct 65 ms 3948 KB Output is correct
9 Correct 84 ms 6432 KB Output is correct
10 Correct 108 ms 13084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 308 ms 50260 KB Output is correct
2 Correct 420 ms 50748 KB Output is correct
3 Correct 458 ms 53792 KB Output is correct
4 Correct 624 ms 53664 KB Output is correct
5 Correct 316 ms 47844 KB Output is correct
6 Correct 457 ms 51004 KB Output is correct
7 Correct 484 ms 32316 KB Output is correct
8 Correct 217 ms 10764 KB Output is correct
9 Correct 223 ms 10764 KB Output is correct
10 Correct 342 ms 29268 KB Output is correct
11 Correct 360 ms 48188 KB Output is correct
12 Correct 244 ms 13276 KB Output is correct