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>
#define pb push_back
#define S second
#define F first
#define ll long long
#define ld long double
using namespace std;
typedef pair<int,int> pii ;
typedef pair<ll,pii> piii ;
//setprecision(8)
//freopen("chip.in","r",stdin);freopen("chip.out","w",stdout);
const int maxn = 1e5+10 , maxk = 28 , mbn = 311 ;
string s ;
int n ;
ll dpl[maxn] , dpr[maxn] , Ans[maxk+3][maxn] , pr[2][maxn] , pl[2][maxn] , hl[maxn] , hr[maxn] , pw[maxn] ;//0 khodesh 1 hesabi
bool check(int i ,int j,int l,int r)
{
ll A = hl[j]-hl[i-1]*pw[j-i+1] ;
ll B = hr[l]-hr[r+1]*pw[r-l+1] ;
return (A==B) ;
}
int Bs(int l ,int r , int i , int j)
{
if(i+1==j)
return i ;
int mid = (i+j)>>1 ;
if(check(l-mid,l,r,r+mid))
return Bs(l,r,mid,j);
return Bs(l,r,i,mid);
}
int get(int i ,int j)
{
if((i<=0||j>n)||s[i-1]!=s[j-1])
return -1 ;
return Bs(i,j,0,min(i-1,n-j)+1);
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie();
cin>>s;
n = s.size(),pw[0]=1;
for(int y = 1 ; y <= n ; y++)
hl[y] = (hl[y-1]*mbn+s[y-1]-'a'+1) ;
for(int y = n ; y ; y--)
hr[y] = (hr[y+1]*mbn+s[y-1]-'a'+1) ;
for(int y = 1 ; y <= n ; y++)
pw[y] = (mbn*pw[y-1]) ;
for(int y = 1 ; y <= n ; y++)
{
// cout<<endl;
int fa = get(y,y) ;
// cout<<y<<' '<<fa<<' ' ;
pl[1][y]++;pl[1][y+fa+1]--;
pl[0][y]-=(y-1),pl[0][y+fa+1]+=(y-1),pl[0][y+fa+1]+=fa+1;
pr[1][y]++,pr[1][y-fa-1]--;
pr[0][y]-=(n+1-y-1),pr[0][y-fa-1]+=(n+1-y-1),pr[0][y-fa-1]+=fa+1;
for(int x = 0 ; x < maxk ; x++)
Ans[x][y]+=fa+1 ;
if(y-fa==1||y+fa==n)
continue ;
int fa2 = get(y-fa-2,y+fa+2)+2;
// cout<<fa2;
Ans[s[y+fa]-'a'+1][y-fa-1]+=fa2;
Ans[s[y-fa-2]-'a'+1][y+fa+1]+=fa2 ;
}
for(int y = 1 ; y < n ; y++)
{
// cout<<endl;
int fa = get(y,y+1) ;
// cout<<y<<' '<<fa<<' ';
if(fa!=-1)
{
pl[1][y+1]++,pl[1][y+fa+2]--;
pl[0][y+1]-=y,pl[0][y+fa+2]+=y,pl[0][y+fa+2]+=fa+1;
pr[1][y]++,pr[1][y-fa-1]--;
pr[0][y]-=(n+1-y-1),pr[0][y-fa-1]+=(n+1-y-1),pr[0][y-fa-1]+=fa+1 ;
}
if(y-fa==1||y+1+fa==n)
continue ;
int fa2 = get(y-fa-2,y+1+fa+2)+2;
Ans[s[y+1+fa]-'a'+1][y-fa-1]+=fa2;
Ans[s[y-fa-2]-'a'+1][y+2+fa]+=fa2;
}
for(int y = 1 ; y <= n ; y++)
pl[0][y]+=pl[0][y-1],pl[1][y]+=pl[1][y-1],dpl[y]=pl[0][y]+y*pl[1][y] ;
for(int y = n ; y ; y--)
pr[0][y]+=pr[0][y+1],pr[1][y]+=pr[1][y+1],dpr[y]=pr[0][y]+(n+1-y)*pr[1][y];
ll ans = dpl[n] ;
for(int y = 1 ; y <= n ; y++)
for(int k = 0 ; k < maxk ; k++)
ans = max(ans , dpl[y-1]+dpr[y+1]+Ans[k][y]);
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |