#include <iostream>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=1e5+5, p=29, mod=1e9+7;
inline int sum(int a, int b){
if(a+b>=mod){
return a+b-mod;
}
else if(a+b<0){
return a+b+mod;
}
return a+b;
}
inline int mul(int a, int b){
return (ll)a*b%mod;
}
int pref[maxn], suff[maxn];
int n;
string s;
int pot[maxn];
ll sw[maxn];
ll oduz[maxn];
ll dodaj[maxn][p];
ll sol;
void precompute(){
pot[0]=1;
for(int i=1; i<maxn; i++){
pot[i]=mul(pot[i-1], p);
}
}
bool provjeri(int x, int y, int kolko){
return sum(pref[x+1], -mul(pref[x-kolko], pot[kolko+1]))==sum(suff[n-y], -mul(suff[n-y-kolko-1], pot[kolko+1]));
}
int binary(int x, int y){
int lo=0, hi=min(x+1, n-y), mid;
while(lo<hi){
mid=(lo+hi)/2;
if(provjeri(x, y, mid)){
lo=mid+1;
}
else{
hi=mid;
}
}
return lo;
}
void rijesi(int x, bool p){
int l, d;
if(p){
l=x;
d=x+1;
}
else{
l=x-1;
d=x+1;
}
int val;
if(l<0 || d>=n){
val=0;
}
else{
val=binary(l, d);
}
l-=val;
d+=val;
sol+=x-l;
// cout << x << " " << p << " " << x-l << endl;
// cout << x << " " << p << " " << l << " " << d << '\n';
if(p){
sw[l+1]++;
sw[x+1]--;
sw[x+2]--;
sw[d+1]++;
}
else{
sw[l+1]++;
sw[x+1]-=2;
sw[d+1]++;
oduz[x]+=x-l;
}
}
void rijesi2(int x, bool p){
int l, d;
if(p){
l=x;
d=x+1;
}
else{
l=x-1;
d=x+1;
}
if(l<0 || d>=n){
return;
}
int val;
val=binary(l, d);
l-=val;
d+=val;
if(l<0 || d>=n){
return;
}
dodaj[l][s[d]-'a']++;
dodaj[d][s[l]-'a']++;
if(l-1<0 || d+1>=n){
return;
}
val=binary(l-1, d+1);
dodaj[l][s[d]-'a']+=val;
dodaj[d][s[l]-'a']+=val;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
precompute();
cin >> s;
n=s.size();
for(int i=0; i<n; i++){
pref[i+1]=sum(mul(pref[i], p), s[i]-'a');
suff[i+1]=sum(mul(suff[i], p), s[n-i-1]-'a');
}
for(int i=0; i<n; i++){
rijesi(i, 0);
rijesi(i, 1);
}
ll val=0;
ll upd=0;
for(int i=0; i<n; i++){
upd+=sw[i];
val-=upd;
oduz[i]+=val;
}
/* for(int i=0; i<n; i++){
cout << oduz[i] << " ";
}
cout << '\n';*/
for(int i=0; i<n; i++){
rijesi2(i, 0);
rijesi2(i, 1);
}
ll maksi=0;
for(int i=0; i<n; i++){
for(int j=0; j<p; j++){
maksi=max(maksi, oduz[i]+dodaj[i][j]);
}
}
cout << sol+maksi << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
768 KB |
Output is correct |
5 |
Correct |
1 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2048 KB |
Output is correct |
2 |
Correct |
4 ms |
2048 KB |
Output is correct |
3 |
Correct |
5 ms |
2048 KB |
Output is correct |
4 |
Correct |
4 ms |
1536 KB |
Output is correct |
5 |
Correct |
5 ms |
1792 KB |
Output is correct |
6 |
Correct |
5 ms |
2048 KB |
Output is correct |
7 |
Correct |
5 ms |
2048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
25976 KB |
Output is correct |
2 |
Correct |
91 ms |
25976 KB |
Output is correct |
3 |
Correct |
99 ms |
26104 KB |
Output is correct |
4 |
Correct |
108 ms |
26104 KB |
Output is correct |
5 |
Correct |
110 ms |
26148 KB |
Output is correct |
6 |
Correct |
107 ms |
26108 KB |
Output is correct |
7 |
Correct |
106 ms |
26104 KB |
Output is correct |
8 |
Correct |
64 ms |
3448 KB |
Output is correct |
9 |
Correct |
106 ms |
26104 KB |
Output is correct |
10 |
Correct |
109 ms |
26104 KB |
Output is correct |
11 |
Correct |
101 ms |
26144 KB |
Output is correct |
12 |
Correct |
125 ms |
26104 KB |
Output is correct |
13 |
Correct |
113 ms |
26232 KB |
Output is correct |
14 |
Correct |
111 ms |
26232 KB |
Output is correct |
15 |
Correct |
107 ms |
26104 KB |
Output is correct |
16 |
Correct |
123 ms |
26104 KB |
Output is correct |
17 |
Correct |
106 ms |
26104 KB |
Output is correct |
18 |
Correct |
110 ms |
26104 KB |
Output is correct |
19 |
Correct |
104 ms |
26104 KB |
Output is correct |