This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
r = i + x[i] - 1;
}
}
x.pop_back();
return x;
}
vector<int> manacher_even(string S) {
string new_s;
for (auto v : S) {
new_s += '#';
new_s += v;
}
new_s += '#';
int N = (int)S.size();
vector<int> x = manacher_odd(new_s);
vector<int> new_x(N);
for (int i = 0; i < N; i++) {
new_x[i] = (x[i * 2] + 1) / 2;
}
return new_x;
}
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] + 1].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... |