This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//In the name of God
#include <bits/stdc++.h>
#pragma optimize("O2")
using namespace std;
typedef int ll;
typedef pair<ll, ll> pll;
const ll maxn = 3e5 + 100;
const ll lg = 20;
const ll mod = 1e9 + 7;
const ll inf = 1e9;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
#define lc (id << 1)
#define rc (lc | 1)
ll n, p[maxn], rnk[maxn], r2[maxn], lcp[maxn], f[maxn];
string s;
ll h;
vector<ll> vec[maxn];
bool cmp(ll i, ll j){
if(rnk[i] < rnk[j]) return 1;
if(rnk[i] > rnk[j]) return 0;
i += h;
j += h;
if(i >= n) i -= n;
if(j >= n) j -= n;
return (rnk[i] < rnk[j]);
}
ll mn[lg][maxn];
void bld(){
for(ll i = 0; i < n; i++){
mn[0][i] = lcp[i];
}
for(ll i = 1; i < lg; i++){
for(ll j = 0; j + (1 << i) <= n; j++){
mn[i][j] = min(mn[i - 1][j], mn[i - 1][j + (1 << (i - 1))]);
}
}
}
ll get(ll l, ll r){
if(l == r) return inf;
ll t = r - l;
ll i = f[t];
return min(mn[i][l], mn[i][r - (1 << i)]);
}
int main(){
fast_io;
for(ll i = 2; i < maxn; i++){
f[i] = f[i / 2] + 1;
}
cin >> s;
s += '#';
n = Sz(s);
for(ll i = 0; i < n; i++){
rnk[i] = ll(s[i] - 'a');
p[i] = i;
}
h = 0;
sort(p, p + n, cmp);
r2[p[0]] = 0;
for(ll i = 1; i < n; i++){
r2[p[i]] = r2[p[i - 1]];
if(cmp(p[i - 1], p[i])) r2[p[i]]++;
}
for(ll i = 0; i < n; i++){
rnk[i] = r2[i];
}
h = 1;
while(h < n){
sort(p, p + n, cmp);
r2[p[0]] = 0;
for(ll i = 1; i < n; i++){
r2[p[i]] = r2[p[i - 1]];
if(cmp(p[i - 1], p[i])) r2[p[i]]++;
}
for(ll i = 0; i < n; i++){
rnk[i] = r2[i];
}
h <<= 1;
}
for(ll i = 0, k = 0; i < n; i++){
if(rnk[i] == n - 1){
k = 0;
continue;
}
ll j = p[rnk[i] + 1];
while(i + k < n && j + k < n && s[i + k] == s[j + k]) k++;
lcp[rnk[i]] = k;
if(k) k--;
}
bld();
n--;
vector<int> d1(n);
for (int i = 0, l = 0, r = -1; i < n; i++) {
int k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1);
while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) {
k++;
}
d1[i] = k--;
if (i + k > r) {
l = i - k;
r = i + k;
}
}
vector<int> d2(n);
for (int i = 0, l = 0, r = -1; i < n; i++) {
int k = (i > r) ? 0 : min(d2[l + r - i + 1], r - i + 1);
while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) {
k++;
}
d2[i] = k--;
if (i + k > r) {
l = i - k - 1;
r = i + k;
}
}
set<ll> st;
long long ans = 0;
for(ll i = n; i--;){
for(ll j : vec[i]){
st.erase(j);
}
vec[i].clear();
st.insert(i);
vec[i - d1[i]].pb(i);
auto it = st.rbegin();
while(it != st.rend()){
ll t = (*it - i) * 2 + 1;
if(t <= lcp[rnk[i] - 1]) break;;
ll l = rnk[i], r = n + 1;
while(r - l > 1){
ll mid = (l + r) >> 1;
if(get(rnk[i], mid) >= t) l = mid;
else r = mid;
}
ans = max(ans, ((long long)t) * (r - rnk[i]));
it++;
}
}
st.clear();
for(ll i = n - 1; i--;){
for(ll j : vec[i]){
st.erase(j);
}
if(d2[i + 1]) st.insert(i);
if(d2[i + 1]) vec[i - d2[i + 1]].pb(i);
if(st.empty()) continue;
auto it = st.rbegin();
while(it != st.rend()){
ll t = (*it - i) * 2 + 2;
if(t <= lcp[rnk[i] - 1]) break;
ll l = rnk[i], r = n + 1;
while(r - l > 1){
ll mid = (l + r) >> 1;
if(get(rnk[i], mid) >= t) l = mid;
else r = mid;
}
ans = max(ans, ((long long)t) * (r - rnk[i]));
it++;
}
}
cout << ans;
return 0;
}
Compilation message (stderr)
palindrome.cpp:3: warning: ignoring #pragma optimize [-Wunknown-pragmas]
3 | #pragma optimize("O2")
|
# | 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... |