Submission #134977

#TimeUsernameProblemLanguageResultExecution timeMemory
134977fredbrPalindromes (APIO14_palindrome)C++17
0 / 100
733 ms1400 KiB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int const p = 117;
int const mod = 1e9 + 9;
int const maxn = 10010;

char s[maxn];
int h[maxn];
int pw[maxn];

vector<int> hs(int n) {
    vector<int> res(n+1);

    for (int i = 1; i <= n; i++) {
        res[i] = (ll(res[i-1])*p + s[i] ) % mod;
    }

    return res;
}

void init() {
    pw[0] = 1;
    for (int i = 1; i < maxn; i++)
        pw[i] = ll(pw[i-1])*p%mod;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);

    init();

    int n;

    string si;
    cin >> si;

    n = si.size();
    copy(si.begin(), si.end(), s+1);

    auto fh = hs(n);

    reverse(s+1, s+n+1);

    auto rh = hs(n);

    auto comp = [&] (int l, int r) -> pair<int, int> {
        int x = (ll(fh[r]) -     ll(fh[l-1])*pw[r-l+1]) % mod;
        int y = (ll(rh[n-l+1]) - ll(rh[n-r])*pw[r-l+1]) % mod;

        if (x < 0) x += mod;
        if (y < 0) y += mod;

        return {x == y, x};
    };

    ll ans = 0;

    for (int lenght = 1; lenght <= n; lenght++) {
        vector<int> v;
        for (int l = 1; l + lenght - 1 <= n; l++) {
            int r = l + lenght - 1;

            auto x = comp(l, r);
            if (x.first) {
                v.push_back(x.second);
            }
        }

        sort(v.begin(), v.end());

        int best = 0;
        for (int i = 0; i < (int)v.size(); i++) {
            int l = i;
            while (i < (int)v.size() and (i == (int)v.size()-1 or v[i] == v[i+1])) i++;
            best = max(best, i - l);
        }

        // cout << lenght << " " << best << "\n";
        ans = max(ans, ll(lenght)*best);

        if (ll(best)*lenght >= ll(n-lenght+1)*(lenght+1)) break;
    }

    cout << ans << "\n";
}
#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...