QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#357849 | #801. 回文自动机 | Isrothy | 0 | 0ms | 221260kb | C++23 | 2.1kb | 2024-03-19 13:52:49 | 2024-03-19 13:52:50 |
Judging History
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%