#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 = 2e5+10 , maxk = 30 , mbn = 293 ;
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] ;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
2 ms |
632 KB |
Output is correct |
3 |
Correct |
2 ms |
632 KB |
Output is correct |
4 |
Correct |
2 ms |
632 KB |
Output is correct |
5 |
Correct |
2 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2168 KB |
Output is correct |
2 |
Correct |
5 ms |
2168 KB |
Output is correct |
3 |
Correct |
5 ms |
2168 KB |
Output is correct |
4 |
Correct |
4 ms |
1528 KB |
Output is correct |
5 |
Correct |
4 ms |
1912 KB |
Output is correct |
6 |
Correct |
5 ms |
2168 KB |
Output is correct |
7 |
Correct |
4 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
31352 KB |
Output is correct |
2 |
Correct |
86 ms |
31360 KB |
Output is correct |
3 |
Correct |
82 ms |
31352 KB |
Output is correct |
4 |
Correct |
77 ms |
31352 KB |
Output is correct |
5 |
Correct |
91 ms |
31352 KB |
Output is correct |
6 |
Correct |
79 ms |
31352 KB |
Output is correct |
7 |
Correct |
77 ms |
31352 KB |
Output is correct |
8 |
Correct |
78 ms |
31240 KB |
Output is correct |
9 |
Correct |
76 ms |
31352 KB |
Output is correct |
10 |
Correct |
76 ms |
31352 KB |
Output is correct |
11 |
Correct |
90 ms |
31340 KB |
Output is correct |
12 |
Correct |
86 ms |
31260 KB |
Output is correct |
13 |
Correct |
106 ms |
31420 KB |
Output is correct |
14 |
Correct |
76 ms |
31352 KB |
Output is correct |
15 |
Correct |
89 ms |
31352 KB |
Output is correct |
16 |
Correct |
108 ms |
31496 KB |
Output is correct |
17 |
Correct |
80 ms |
31384 KB |
Output is correct |
18 |
Incorrect |
94 ms |
31224 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |