This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
const int nmax = 3e5 + 5;
const ll oo = 1e18;
const int lg = 18;
const ll base = 311;
const ll mod = 734065025437;
#define pii pair<int, int>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';
using namespace std;
int n, cur = 0;
string s;
ll pw[nmax];
ll rmq[2][nmax][lg + 1];
int D_odd[nmax];
int D_even[nmax];
void Calc_D_odd() {
int L = 1;
int R = 0;
for(int i = 1 ; i <= n; i++) {
if(i > R) D_odd[i] = 0;
else D_odd[i] = min(R - i, D_odd[L + (R - i)]);
while(i - D_odd[i] - 1 > 0 && i + D_odd[i] + 1 <= n && s[i - D_odd[i] - 1] == s[i + D_odd[i] + 1]) {
D_odd[i]++;
}
if(i + D_odd[i] > R) {
R = i + D_odd[i];
L = i - D_odd[i];
}
}
}
void Calc_D_even() {
int L = 1;
int R = 0;
for(int i = 1 ; i < n; i++) {
int j = i + 1;
if(j > R) D_even[i] = 0;
else D_even[i] = min(R - j + 1, D_even[L + (R - j)]);
while(i - D_even[i] > 0 && j + D_even[i] <= n && s[i - D_even[i]] == s[j + D_even[i]]) {
D_even[i]++;
}
if(i + D_even[i] > R) {
R = i + D_even[i];
L = j - D_even[i];
}
}
}
ll combine(ll a, ll b, int x){
return (a * pw[x] + b) % mod;
}
void Init(){
pw[0] = 1;
for(int i = 1, j; i <= n; ++i){
pw[i] = pw[i - 1] * base % mod;
rmq[0][i][0] = s[i] - 'a' + 3;
rmq[1][i][0] = s[n - i + 1] - 'a' + 3;
}
for(int j = 1; j <= lg; ++j){
for(int i = 1; i + (1 << j) - 1 <= n; ++i){
for(int t = 0; t < 2; ++t){
rmq[t][i][j] = combine(rmq[t][i][j - 1], rmq[t][i + (1 << (j - 1))][j - 1], 1 << (j - 1));
}
}
}
}
ll get(int l, int r, int t){
int k = r - l + 1;
ll res = 0;
for(int j = 0; j <= __lg(k); ++j){
if(k >> j & 1){
res = combine(res, rmq[t][l][j], 1 << j);
l += 1 << j;
}
}
return res;
}
bool check(int l, int r){
return get(l, r, 0) == get(n - r + 1, n - l + 1, 1);
}
map<ll, bool> mp;
map<ll, int> ind;
int cnt[nmax];
ll f[nmax];
vector<int> tmp;
vector<int> adj[nmax];
void update(int l, int r){
bool ok = 1;
while(l <= r){
ll x = get(l, r, 0);
if(!mp[x]){
ind[x] = ++cur;
tmp.push_back(cur);
if(ok){
f[cur]++;
ok = 0;
}
cnt[cur] = r - l + 1;
mp[x] = 1;
}
else{
int id = ind[x];
tmp.push_back(id);
if(ok) f[id]++;
break;
}
++l, --r;
}
if(l > r) tmp.push_back(0);
int x = (int)tmp.size() - 1;
for(int i = 0; i < x; ++i){
int x = tmp[i], y = tmp[i + 1];
adj[y].push_back(x);
}
tmp.clear();
}
void dfs(int u){
for(auto v : adj[u]){
dfs(v);
f[u] += f[v];
}
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> s;
n = s.size(); s = ' ' + s + ' ';
Init();
Calc_D_odd();
Calc_D_even();
for(int i = 1; i <= n; ++i){
update(i - D_odd[i], i + D_odd[i]);
if(i < n) update(i - D_even[i] + 1, i + D_even[i]);
}
dfs(0);
ll ans = 0;
for(int i = 1; i <= cur; ++i){
ans = max(ans, 1ll * f[i] * cnt[i]);
}
cout << ans;
}
/*
*/
Compilation message (stderr)
palindrome.cpp: In function 'void Init()':
palindrome.cpp:65:20: warning: unused variable 'j' [-Wunused-variable]
65 | for(int i = 1, j; i <= n; ++i){
| ^
palindrome.cpp: At global scope:
palindrome.cpp:139:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
139 | main(){
| ^~~~
# | 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... |