Submission #1356507

#TimeUsernameProblemLanguageResultExecution timeMemory
1356507xorshift회문 (APIO14_palindrome)C++20
23 / 100
1095 ms1008 KiB
#include <bits/stdc++.h>
using namespace std;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
using pint = pair<int, int>;
using vpint = vector<pint>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using pill = pair<int, ll>;
using vpill = vector<pill>;
using plli = pair<ll, int>;
using vplli = vector<plli>;

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

    string s; cin >> s;
    int n = s.size();

    int counter = 0;
    map<pint, int> mapper;
    vpint occsz;

    vpint blocks; blocks.reserve(n);
    for (int i = 0, lv = -1; i < n; i++) {
        int c = s[i]-'a';
        if (c != lv) blocks.push_back({c, 1});
        else blocks[blocks.size()-1].second++;
        lv = c;
    }
    sort(blocks.begin(), blocks.end());
    int bs = blocks.size();
    for (int i = 0; i < bs;) {
        int j = i;
        while (j < n && blocks[j].first == blocks[i].first) j++;
        int ms = blocks[j-1].second;
        vint cnt(ms);
        for (int k = i; k < j; k++) cnt[blocks[k].second-1]++;
        int mtv = 0; int tot = 0;
        for (int k = ms-1; k >= 0; k--) {
            mtv += (k+1)*cnt[k]; tot += cnt[k];
            cnt[k] = mtv - k*tot;
        }
        pint pointer = {blocks[i].first, -1};
        for (int k = 0; k < ms; k++) {
            if (k > 0) pointer.second = mapper[pointer];
            mapper[pointer] = counter++;
            occsz.emplace_back(cnt[k], k+1);
        }
        i = j;
    }
    
    set<pint> pstart;
    pstart.insert({0, mapper[{s[0]-'a', -1}]});

    for (int i = 1; i < n; i++) {
        int c = s[i] - 'a';
        int lv = s[i-1] - 'a';
        pint insval;
        if (c != lv) insval = {i, mapper[{c, -1}]};
        else {
            auto it = --pstart.end();
            pint nv = {c, it->second};
            insval = {it->first, mapper[nv]};
        }
        for (auto it = pstart.begin(); it != pstart.end(); it = pstart.erase(it)) {
            if (it->first == 0) continue;
            int lst = s[it->first-1] - 'a';
            if (lst == c) {
                pint nv = {c, it->second};
                if (!mapper.count(nv)) { mapper[nv] = counter++; occsz.emplace_back(0, 0); }
                int mnv = mapper[nv], mst = it->second;
                occsz[mnv] = {occsz[mnv].first+1, occsz[mst].second+2};
                pint nstart = {it->first-1, mnv};
                if (nstart.first != 0) pstart.insert(nstart);
            }
        }
        pstart.insert(insval);
    }
    int res = 0;
    for (auto& [ct, sz]: occsz) res = max(res, ct*sz);

    cout << res << 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...