이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
vector<int> suffix_array(string S) {
S += "$";
int N = (int)S.size();
vector<int> ord(N), comp(N);
for (int i = 0; i < N; i++) {
ord[i] = i;
comp[i] = S[i];
}
sort(ord.begin(), ord.end(), [&](int i, int j) { return S[i] < S[j]; });
vector<int> tmp(N), cnt(N + 1);
auto Update = [&](int d) {
int ptr = 0, x = -1, y = -1;
for (int i : ord) {
int j = i + d < N ? i + d : i + d - N;
tmp[i] = comp[i] != x || comp[j] != y ? ++ptr : ptr;
x = comp[i], y = comp[j];
}
comp = tmp;
};
auto Sort = [&](int d) {
cnt.assign(N + 1, 0);
for (int value : comp) {
cnt[value]++;
}
partial_sum(cnt.begin(), cnt.end(), cnt.begin());
for (int i : ord) {
int j = i + d < N ? i + d : i + d - N;
tmp[cnt[comp[j] - 1]++] = i;
}
ord = tmp;
};
Update(0);
for (int d = 1; d < N; d++) {
Sort(d);
Sort(0);
Update(d);
}
return ord;
}
vector<int> find_lcp(string S, vector<int> ord) {
S += "$";
int N = (int)S.size();
vector<int> pos(N);
for (int i = 0; i < N; i++) {
pos[ord[i]] = i;
}
vector<int> lcp(N);
int l = 0;
for (int i = 0; i < N; i++) {
if (pos[i] == N - 1) {
continue;
}
int j = ord[pos[i] + 1];
while (max(i, j) + l < N && S[i + l] == S[j + l]) {
l++;
}
lcp[pos[i]] = l;
l = max(0, l - 1);
}
return lcp;
}
vector<int> manacher_odd(string S) {
S += "$";
int N = (int)S.size();
vector<int> x(N, 1);
int l = 0, r = 0;
for (int i = 1; i < N; i++) {
if (i < r) {
x[i] = min(r - i + 1, x[l + r - i]);
}
while (i - x[i] >= 0 && i + x[i] < N && S[i - x[i]] == S[i + x[i]]) {
x[i]++;
}
if (i + x[i] - 1 > r) {
l = i - x[i] + 1;
r = i + x[i] - 1;
}
}
x.pop_back();
return x;
}
vector<int> manacher_even(string s) {
int n = (int)s.size();
vector<int> d(n, 0);
int l = -1, r = -1;
for (int i = 0; i < n - 1; i++) {
if (i < r) {
d[i] = min(r - i, d[l + r - i - 1]);
}
while (i - d[i] >= 0 && i + d[i] + 1 < n && s[i - d[i]] == s[i + d[i] + 1]) {
d[i]++;
}
if (i + d[i] > r) {
l = i - d[i] + 1, r = i + d[i];
}
}
rotate(d.rbegin(), d.rbegin() + 1, d.rend());
return d;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
string S;
cin >> S;
int N = (int)S.size();
vector<int> ord = suffix_array(S);
vector<int> lcp = find_lcp(S, ord);
vector<int> x_odd = manacher_odd(S), x_even = manacher_even(S);
vector<int> pos(N + 1);
for (int i = 0; i <= N; i++) {
pos[ord[i]] = i;
}
vector<int> max_pal(N + 1);
set<int> odd, even;
vector<vector<int>> del_odd(N), del_even(N);
for (int i = N - 1; i >= 0; i--) {
odd.insert(i);
even.insert(i);
del_odd[i - x_odd[i] + 1].push_back(i);
del_even[i - x_even[i]].push_back(i);
max_pal[pos[i]] = max((*odd.rbegin() - i) * 2 + 1, (*even.rbegin() - i) * 2);
for (int v : del_odd[i]) {
odd.erase(v);
}
for (int v : del_even[i]) {
even.erase(v);
}
}
vector<int> A;
for (int i = 0; i <= N; i++) {
A.push_back(max_pal[i]);
A.push_back(lcp[i]);
}
long long ans = 1;
for (int r = 0; r < 2; r++) {
stack<int> st;
for (int i = 0; i < 2 * (N + 1); i++) {
while (!st.empty() && A[i] <= A[st.top()]) {
st.pop();
}
int last = st.empty() ? -1 : st.top();
if ((i ^ r) % 2 == 0) {
ans = max(ans, 1LL * ((i - last + 1) / 2) * A[i]);
}
st.push(i);
}
reverse(A.begin(), A.end());
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |