QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#357849#801. 回文自动机Isrothy0 0ms221260kbC++232.1kb2024-03-19 13:52:492024-03-19 13:52:50

Judging History

你现在查看的是最新测评结果

  • [2024-03-19 13:52:50]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:221260kb
  • [2024-03-19 13:52:49]
  • 提交

answer

#include <cstdio>
#include <deque>
#include <iostream>
#include <string_view>

constexpr int M = 1e6 + 10;

template<size_t M, size_t Sigma>
struct PalindromicAutomaton {
    size_t next[M][Sigma]{}, fail[M]{};
    int length[M]{};
    std::deque<char> str;
    size_t n, left, right;
    PalindromicAutomaton() : n(2), left(0), right(0) {
        length[1] = -1;
        fail[0] = 1;
    }
    void push_back(int c) {
        auto get_fail = [&](size_t p) {
            while (str.size() <= length[p] + 1 || str[str.size() - length[p] - 2] != str.back()) {
                p = fail[p];
            }
            return p;
        };
        str.push_back(c);
        auto p = get_fail(right);
        auto &q = next[p][c];
        if (!q) {
            auto r = ++n;
            length[r] = length[p] + 2;
            fail[r] = next[get_fail(fail[p])][c];
            q = r;
        }
        p = q;
        right = p;
        if (length[p] == str.size()) {
            left = p;
        }
    }
    void push_front(char c) {
        auto get_fail = [&](size_t p) {
            while (str.size() <= length[p] + 1 || str[length[p] + 1] != str.front()) {
                p = fail[p];
            }
            return p;
        };
        str.push_front(c);
        auto p = get_fail(left);
        auto &q = next[p][c];
        if (!q) {
            auto r = ++n;
            length[r] = length[p] + 2;
            fail[r] = next[get_fail(fail[p])][c];
            q = r;
        }
        p = q;
        left = p;
        if (length[p] == str.size()) {
            right = p;
        }
    }
};
PalindromicAutomaton<M, 26> pam;
int cnt[M];
char s[M];

int main() {
    scanf("%s", s);
    for (char *c = s; *c; ++c) {
        pam.push_back(*c - 'a');
        ++cnt[pam.right];
    }
    long long ans = 0;
    for (auto p = pam.n; p >= 2; --p) {
        for (auto q: pam.next[p]) {
            if (q) {
                cnt[p] += cnt[q];
            }
        }
        ans = std::max(ans, 1LL * pam.length[p] * pam.length[p] * cnt[p]);
    }
    printf("%lld\n", ans);
    return 0;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 221260kb

input:

bdgedcfadbabbfgeacdgbggaefddebabbfgeacdgbefaecfddffeddacbabfcgecedacbffeddacbabfebadggfafabcdfdeaabdeecgbcecegcgecedacbfgdagbgagafdegecadfebcdbgfacdecdegecadfebbcdfdeaabdbfgcbccfcaebcecfdfccagdafaeaacbggaefddebcbecdafageeaabcbdafadcbecdbcgcbdgedcfadbcaefbdfcbgfcdeceddaaffgcedfcdcgdcgbfdddfdadgagbbef...

output:

4856

result:

wrong answer expected '5594', found '4856'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%