#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e5+5,S=26,b=233;
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;
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:19:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | int mid=l+r>>1;
| ~^~
palinilap.cpp:29:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int mid=l+r>>1;
| ~^~
palinilap.cpp:40:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | int mid=l+r>>1;
| ~^~
palinilap.cpp:50:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | 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 |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 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 |
1 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 |
2 ms |
1612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
26180 KB |
Output is correct |
2 |
Correct |
33 ms |
26304 KB |
Output is correct |
3 |
Correct |
29 ms |
26188 KB |
Output is correct |
4 |
Correct |
39 ms |
26212 KB |
Output is correct |
5 |
Correct |
38 ms |
26204 KB |
Output is correct |
6 |
Correct |
39 ms |
26144 KB |
Output is correct |
7 |
Correct |
38 ms |
26164 KB |
Output is correct |
8 |
Correct |
22 ms |
5908 KB |
Output is correct |
9 |
Correct |
39 ms |
26188 KB |
Output is correct |
10 |
Correct |
38 ms |
26208 KB |
Output is correct |
11 |
Correct |
29 ms |
26188 KB |
Output is correct |
12 |
Correct |
44 ms |
26172 KB |
Output is correct |
13 |
Correct |
40 ms |
26204 KB |
Output is correct |
14 |
Correct |
39 ms |
26192 KB |
Output is correct |
15 |
Correct |
41 ms |
26188 KB |
Output is correct |
16 |
Correct |
34 ms |
26188 KB |
Output is correct |
17 |
Correct |
35 ms |
26188 KB |
Output is correct |
18 |
Incorrect |
37 ms |
26172 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |