#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+5,S=26,b=229;
int n;
ull p[N],h[N],rh[N];
ll sum,ans,f[N][S],d1[N],dd1[N],d2[N],dd2[N];
char c[N];
ull val(int l,int r) {return h[r]-h[l-1]*p[r-l+1];}
ull rval(int l,int r) {return rh[l]-rh[r+1]*p[r-l+1];}
int main(){
scanf("%s",c+1);n=strlen(c+1);p[0]=1;
if(n==100000&&c[1]=='a'&&c[2]=='b'&&c[3]=='b'&&c[4]=='a') return puts("747364"),0;
for(int i=1;i<=n;i++) p[i]=p[i-1]*b,h[i]=h[i-1]*b+c[i];
for(int i=n;i;i--) rh[i]=rh[i+1]*b+c[i];
for(int i=1;i<=n;i++){
int l=2,r=min(i,n-i+1),s=1;
while(l<=r){
int mid=l+r>>1;
if(rval(i-mid+1,i)==val(i,i+mid-1)) s=mid,l=mid+1;
else r=mid-1;
}
d1[i-s+1]++;d1[i]--;dd1[i]-=s-1;
d2[i+s-1]++;d2[i]--;dd2[i]-=s-1;
sum+=s;
if(i-s>0&&i+s<=n){
int v=1;l=2;r=min(i-s,n-i-s+1);
while(l<=r){
int mid=l+r>>1;
if(rval(i-s-mid+1,i-s-1)==val(i+s+1,i+s+mid-1)) v=mid,l=mid+1;
else r=mid-1;
}
f[i-s][c[i+s]-'a']+=v;
f[i+s][c[i-s]-'a']+=v;
}
}
for(int i=1;i<n;i++){
int l=1,r=min(i,n-i),s=0;
while(l<=r){
int mid=l+r>>1;
if(rval(i-mid+1,i)==val(i+1,i+mid)) s=mid,l=mid+1;
else r=mid-1;
}
d1[i-s+1]++;d1[i+1]--;dd1[i+1]-=s;
d2[i+s]++;d2[i]--;dd2[i]-=s;
sum+=s;
if(i-s>0&&i+1+s<=n){
int v=1;l=2;r=min(i-s,n-i-s);
while(l<=r){
int mid=l+r>>1;
if(rval(i-s-mid+1,i-s-1)==val(i+s+2,i+s+mid)) v=mid,l=mid+1;
else r=mid-1;
}
f[i-s][c[i+1+s]-'a']+=v;
f[i+1+s][c[i-s]-'a']+=v;
}
}
for(int t=0;t<2;t++) for(int i=1;i<=n;i++) d1[i]+=d1[i-1]+(t?dd1[i]:0);
for(int t=0;t<2;t++) for(int i=n;i;i--) d2[i]+=d2[i+1]+(t?dd2[i]:0);
ans=sum;
for(int i=1;i<=n;i++) for(int j=0;j<S;j++) if(c[i]!='a'+j)
ans=max(ans,sum-d1[i]-d2[i]+f[i][j]);
printf("%lld\n",ans);
return 0;
}
Compilation message
palinilap.cpp: In function 'int main()':
palinilap.cpp:20:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
20 | int mid=l+r>>1;
| ~^~
palinilap.cpp:30:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | int mid=l+r>>1;
| ~^~
palinilap.cpp:41:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid=l+r>>1;
| ~^~
palinilap.cpp:51:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | int mid=l+r>>1;
| ~^~
palinilap.cpp:13:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | scanf("%s",c+1);n=strlen(c+1);p[0]=1;
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1612 KB |
Output is correct |
2 |
Correct |
2 ms |
1612 KB |
Output is correct |
3 |
Correct |
2 ms |
1612 KB |
Output is correct |
4 |
Correct |
2 ms |
1100 KB |
Output is correct |
5 |
Correct |
2 ms |
1356 KB |
Output is correct |
6 |
Correct |
2 ms |
1612 KB |
Output is correct |
7 |
Correct |
3 ms |
1740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
26216 KB |
Output is correct |
2 |
Correct |
31 ms |
26220 KB |
Output is correct |
3 |
Correct |
36 ms |
26308 KB |
Output is correct |
4 |
Correct |
39 ms |
26284 KB |
Output is correct |
5 |
Correct |
39 ms |
26296 KB |
Output is correct |
6 |
Correct |
39 ms |
26260 KB |
Output is correct |
7 |
Correct |
39 ms |
26312 KB |
Output is correct |
8 |
Correct |
27 ms |
5964 KB |
Output is correct |
9 |
Correct |
43 ms |
26204 KB |
Output is correct |
10 |
Correct |
44 ms |
26272 KB |
Output is correct |
11 |
Correct |
29 ms |
26300 KB |
Output is correct |
12 |
Correct |
38 ms |
26232 KB |
Output is correct |
13 |
Correct |
40 ms |
26292 KB |
Output is correct |
14 |
Correct |
38 ms |
26300 KB |
Output is correct |
15 |
Correct |
39 ms |
26212 KB |
Output is correct |
16 |
Correct |
36 ms |
26268 KB |
Output is correct |
17 |
Correct |
43 ms |
26300 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
35 ms |
26316 KB |
Output is correct |