#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define debug(y) cout<<y,exit(0)
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 1e5 + 9;
const ll base = 31;
const ll mod = 1e9 + 7;
ll n,Hash[N][2],pw[N],have[2][N],creat[N][26],ans,got[N];
string s[2];
void performHash(){
for (ll i = 1;i <= n;i++)
for (ll j = 0;j < 2;j++){
Hash[i][j] = Hash[i - 1][j]*base + s[j][i] - 'a' + 1;
Hash[i][j] %= mod;
}
}
ll GetHash(ll l,ll r,ll c){
return (Hash[r][c] - Hash[l - 1][c]*pw[r - l + 1] + mod*mod)%mod;
}
bool IsPalin(ll l,ll r){
return GetHash(l,r,0) == GetHash(n - r + 1,n - l + 1,1);
}
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define task "test"
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin>>s[0]; n = s[0].size();
s[1] = s[0]; reverse(s[1].begin(),s[1].end());
s[0] = " " + s[0]; s[1] = " " + s[1];
performHash(); pw[0] = 1;
for (ll i = 1;i <= n;i++) pw[i] = (pw[i - 1]*base) % mod;
//debug(IsPalin(1,3));
for (ll i = 1;i <= n;i++)
for (ll j = i;j <= i + 1;j++){
//if (j > n || s[0][i] != s[0][j]) continue;
ll l = (s[0][i] == s[0][j]),h = min(i,n - j + 1),m;
while(l <= h){
m = (l + h)/2;
if (IsPalin(i - m + 1,j + m - 1)) l = m + 1;
else h = m - 1;
}
ll rad = h; ans += rad; //debug(rad);
have[0][i]++; have[0][i - rad]--;
have[1][j]++; have[1][j + rad]--;
if (i == j) got[i] += rad - 1;
if (i - rad <= 0 || j + rad > n) continue;
l = 0; h = min(i - rad - 1,n - j - rad);
ll x = i - rad,y = j + rad;
while(l <= h){
m = (l + h)/2;
ll kq1 = GetHash(x - m,x - 1,0);
ll kq2 = GetHash(n - y - m + 1,n - y,1);
if (kq1 == kq2) l = m + 1;
else h = m - 1;
}
//debug(h);
//cout<<x<<" "<<y; return 0;
creat[x][s[0][y] - 'a'] += h + 1;
creat[y][s[0][x] - 'a'] += h + 1;
}
//debug(creat[2]['c' - 'a']);
//for (ll i = 1;i <= n;i++) cout<<have[0][i]<<" "; cout<<"\n";
for (ll i = 2;i <= n;i++) have[1][i] += have[1][i - 1];
//for (ll i = 1;i <= n;i++) cout<<have[0][i]<<" "; return 0;
for (ll i = 2;i <= n;i++) have[1][i] += have[1][i - 1];
for (ll i = n - 1;i > 0;i--) have[0][i] += have[0][i + 1];
for (ll i = n - 1;i > 0;i--) have[0][i] += have[0][i + 1];
//for (ll i = 1;i <= n;i++) cout<<have[0][i]<<" "; return 0;
for (ll i = 1;i <= n;i++){
ll kq = have[0][i + 1] + have[1][i - 1],mx = 0;
for (ll j = 0;j < 26;j++) mx = max(mx,creat[i][j]);
ans = max(ans,kq + mx + 1 + got[i]); //cout<<i<<" "<<kq + mx<<"\n";
}
cout<<ans;
}
/// be confident
Compilation message
palinilap.cpp: In function 'int main()':
palinilap.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
palinilap.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1632 KB |
Output is correct |
2 |
Correct |
2 ms |
1620 KB |
Output is correct |
3 |
Correct |
3 ms |
1616 KB |
Output is correct |
4 |
Correct |
2 ms |
1108 KB |
Output is correct |
5 |
Correct |
3 ms |
1364 KB |
Output is correct |
6 |
Correct |
3 ms |
1620 KB |
Output is correct |
7 |
Correct |
3 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
25680 KB |
Output is correct |
2 |
Correct |
40 ms |
25732 KB |
Output is correct |
3 |
Correct |
45 ms |
25764 KB |
Output is correct |
4 |
Correct |
63 ms |
25724 KB |
Output is correct |
5 |
Correct |
58 ms |
25908 KB |
Output is correct |
6 |
Correct |
51 ms |
25688 KB |
Output is correct |
7 |
Correct |
56 ms |
25672 KB |
Output is correct |
8 |
Correct |
26 ms |
5340 KB |
Output is correct |
9 |
Correct |
61 ms |
25704 KB |
Output is correct |
10 |
Correct |
55 ms |
25676 KB |
Output is correct |
11 |
Correct |
41 ms |
25684 KB |
Output is correct |
12 |
Correct |
66 ms |
25740 KB |
Output is correct |
13 |
Correct |
61 ms |
25660 KB |
Output is correct |
14 |
Correct |
54 ms |
25680 KB |
Output is correct |
15 |
Correct |
58 ms |
25740 KB |
Output is correct |
16 |
Correct |
49 ms |
25656 KB |
Output is correct |
17 |
Correct |
51 ms |
25688 KB |
Output is correct |
18 |
Correct |
54 ms |
25700 KB |
Output is correct |
19 |
Correct |
52 ms |
25676 KB |
Output is correct |