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("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const ll p=29;
const ll mod=(1ll<<61)-1;
const int mxn=3e5+5;
ll powv[mxn];
ll hashr[mxn],hashl[mxn];
map<ll,int> mp;
void build(string str){
int n=str.length();
powv[0]=1;
for(int i=1;i<=n;i++){
powv[i]=(__int128)powv[i-1]*p%mod;
}
for(int i=1;i<n;i++){
hashr[i]=((__int128)hashr[i-1]*p%mod+(str[i]-'a'+1))%mod;
}
for(int i=n-1;i>=1;i--){
hashl[i]=((__int128)hashl[i+1]*p%mod+(str[i]-'a'+1))%mod;
}
}
ll get1(int l,int r){
ll ans=(hashl[l]-(__int128)hashl[r+1]*powv[r-l+1]%mod)%mod;
if(ans<0) ans+=mod;
return ans;
}
ll get2(int l,int r){
ll ans=(hashr[r]-(__int128)hashr[l-1]*powv[r-l+1]%mod)%mod;
if(ans<0) ans+=mod;
return ans;
}
bool same(int l1,int r1,int l2,int r2){
ll v1=get1(l1,r1);
ll v2=get2(l2,r2);
return v1==v2;
}
int main() {_
string str;
cin>>str;
int n=(int)str.length();
str="#"+str;
build(str);
priority_queue<pair<int,pair<int,int>>> pq;
for(int i=1;i<=n;i++){
{
int l=0,r=min(i-1,n-i);
while(l<r){
int mid=(l+r+1)/2;
if(same(i-mid,i-1,i+1,i+mid)){
l=mid;
}
else{
r=mid-1;
}
}
ll val=get2(i-l,i+l);
mp[val]++;
if(mp[val]==1) pq.push({2*l+1,{i-l,i+l}});
}
{
int l=0,r=min(i-1,n-i+1);
while(l<r){
int mid=(l+r+1)/2;
if(same(i-mid,i-1,i,i+mid-1)){
l=mid;
}
else{
r=mid-1;
}
}
if(l>0){
ll val=get2(i-l,i+l-1);
mp[val]++;
if(mp[val]==1) pq.push({2*l,{i-l,i+l-1}});
}
}
}
ll ans=1;
while(!pq.empty()){
auto tmp=pq.top();
pq.pop();
int len=tmp.f;
int l=tmp.s.f;
int r=tmp.s.s;
ll val=get2(l,r);
ans=max(ans,1ll*mp[val]*len);
l++;
r--;
if(l<=r){
ll val2=get2(l,r);
mp[val2]+=mp[val];
if(mp[val2]==mp[val]) pq.push({r-l+1,{l,r}});
}
}
cout<<ans<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message (stderr)
palindrome.cpp: In function 'void setIO(std::string)':
palindrome.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |