#pragma GCC optimize("Ofast,unroll-loops")
#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 += 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;
vector<int> L(2 * (N + 1)), R(2 * (N + 1));
stack<int> st;
for (int i = 0; i < 2 * (N + 1); i++) {
while (!st.empty() && A[i] <= A[st.top()]) {
st.pop();
}
L[i] = st.empty() ? -1 : st.top();
st.push(i);
}
st = stack<int>();
for (int i = 2 * (N + 1) - 1; i >= 0; i--) {
while (!st.empty() && A[i] <= A[st.top()]) {
st.pop();
}
R[i] = st.empty() ? 2 * (N + 1) : st.top();
if (i % 2 == 0) {
ans = max(ans, 1LL * ((R[i] - L[i] + 1) / 2) * A[i]);
}
st.push(i);
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
408 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
452 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2140 KB |
Output is correct |
2 |
Correct |
5 ms |
1884 KB |
Output is correct |
3 |
Correct |
6 ms |
1884 KB |
Output is correct |
4 |
Correct |
6 ms |
2028 KB |
Output is correct |
5 |
Correct |
4 ms |
1884 KB |
Output is correct |
6 |
Correct |
5 ms |
1692 KB |
Output is correct |
7 |
Correct |
5 ms |
1884 KB |
Output is correct |
8 |
Correct |
5 ms |
2396 KB |
Output is correct |
9 |
Correct |
4 ms |
1884 KB |
Output is correct |
10 |
Correct |
5 ms |
1740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
17736 KB |
Output is correct |
2 |
Correct |
54 ms |
16976 KB |
Output is correct |
3 |
Correct |
65 ms |
17236 KB |
Output is correct |
4 |
Correct |
66 ms |
16636 KB |
Output is correct |
5 |
Correct |
90 ms |
15280 KB |
Output is correct |
6 |
Correct |
80 ms |
15444 KB |
Output is correct |
7 |
Correct |
63 ms |
15440 KB |
Output is correct |
8 |
Correct |
56 ms |
16464 KB |
Output is correct |
9 |
Correct |
65 ms |
16208 KB |
Output is correct |
10 |
Correct |
96 ms |
15508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
54468 KB |
Output is correct |
2 |
Correct |
167 ms |
48676 KB |
Output is correct |
3 |
Correct |
235 ms |
57388 KB |
Output is correct |
4 |
Correct |
197 ms |
46384 KB |
Output is correct |
5 |
Correct |
348 ms |
47048 KB |
Output is correct |
6 |
Correct |
232 ms |
46884 KB |
Output is correct |
7 |
Correct |
233 ms |
45252 KB |
Output is correct |
8 |
Correct |
191 ms |
49188 KB |
Output is correct |
9 |
Correct |
183 ms |
49208 KB |
Output is correct |
10 |
Correct |
437 ms |
46524 KB |
Output is correct |
11 |
Correct |
175 ms |
52160 KB |
Output is correct |
12 |
Correct |
180 ms |
50408 KB |
Output is correct |