/*
_ _ _______ _ _
| | / / | _____| | | / /
| | / / | | | | / /
| |/ / | |_____ | |/ /
| |\ \ | _____| | |\ \
| | \ \ | | | | \ \
| | \ \ | |_____ | | \ \
|_| \_\ |_______| |_| \_\
*/
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
typedef pair < ll, int > PLI;
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define right(x) x << 1 | 1
#define left(x) x << 1
#define forn(x, a, b) for (int x = a; x <= b; ++x)
#define for1(x, a, b) for (int x = a; x >= b; --x)
#define mkp make_pair
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define y1 kekekek
#define fname ""
const ll ool = 1e18 + 9;
const int oo = 1e9 + 9, base = 1e9 + 7;
const ld eps = 1e-7;
const int N = 2e5 + 6;
string t;
ll pref[N], suff[N], cnt1[N], cnt2[N], val1[N], val2[N], d1[N], d2[N], p[N], pr = 31, addl[N][26], addr[N][26];
char s[N];
ll get1(int l, int r) {
return (pref[r] - pref[l - 1] * p[r - l + 1] % base + base) % base;
}
ll get2(int l, int r) {
return (suff[l] - suff[r + 1] * p[r - l + 1] % base + base) % base;
}
int main() {
#ifdef krauch
freopen("input.txt", "r", stdin);
#else
//freopen(fname".in", "r", stdin);
//freopen(fname".out", "w", stdout);
#endif
cin >> t;
int n = sz(t);
forn(i, 0, n - 1) s[i + 1] = t[i];
p[0] = 1;
forn(i, 1, n) {
pref[i] = (pref[i - 1] * pr % base + s[i] - 'a') % base;
p[i] = p[i - 1] * pr % base;
}
for1(i, n, 1) {
suff[i] = (suff[i + 1] * pr % base + s[i] - 'a') % base;
}
int l = 0, r = -1;
forn(i, 1, n) {
if (i <= r) d1[i] = min(d1[l + r - i], 1ll * r - i + 1);
while (i + d1[i] <= n && i - d1[i] > 0 && s[i + d1[i]] == s[i - d1[i]]) ++d1[i];
if (i + d1[i] - 1 > r) {
l = i - d1[i] + 1;
r = i + d1[i] - 1;
}
if (i + d1[i] <= n && i - d1[i] > 0) {
int tl = i + d1[i] + 1, tr = min(1ll * n, i + d1[i] + (i - d1[i] - 1));
while (tl < tr) {
int mid = (tl + tr) >> 1;
if (get1(i + d1[i] + 1, mid) == get2(i - d1[i] - 1 - (mid - (i + d1[i] + 1)), i - d1[i] - 1)) tl = mid + 1;
else tr = mid;
}
if (tl <= n && get1(i + d1[i] + 1, tl) == get2(i - d1[i] - 1 - (tl - (i + d1[i] + 1)), i - d1[i] - 1)) ++tl;
addr[i + d1[i]][s[i - d1[i]] - 'a'] += tl - (i + d1[i]);
addl[i - d1[i]][s[i + d1[i]] - 'a'] += tl - (i + d1[i]);
}
cnt1[i - d1[i] + 1]++;
val1[i - d1[i] + 1]++;
val1[i] -= d1[i];
cnt1[i]--;
cnt2[i + 1]++;
val2[i + 1] += d1[i] - 1;
cnt2[i + d1[i]]--;
// cerr << d1[i] << " ";
}
// cerr << "\n";
l = 0, r = -1;
forn(i, 1, n) {
if (i <= r) d2[i] = min(d2[l + r - i + 1], 1ll * r - i + 1) + 1;
else d2[i] = 1;
while (i + d2[i] - 1 <= n && i - d2[i] > 0 && s[i + d2[i] - 1] == s[i - d2[i]]) ++d2[i];
--d2[i];
if (i + d2[i] - 1 > r) {
l = i - d2[i];
r = i + d2[i] - 1;
}
if (i + d2[i] <= n && i - d2[i] - 1 > 0) {
int tl = i + d2[i] + 1, tr = min(1ll * n, i + d2[i] + (i - d2[i] - 2));
while (tl < tr) {
int mid = (tl + tr) >> 1;
if (get1(i + d2[i] + 1, mid) == get2(i - d2[i] - 2 - (mid - (i + d2[i] + 1)), i - d2[i] - 2)) tl = mid + 1;
else tr = mid;
}
if (tl <= n && get1(i + d2[i] + 1, tl) == get2(i - d2[i] - 2 - (tl - (i + d2[i] + 1)), i - d2[i] - 2)) ++tl;
addr[i + d2[i]][s[i - d2[i] - 1] - 'a'] += tl - (i + d2[i]);
addl[i - d2[i] - 1][s[i + d2[i]] - 'a'] += tl - (i + d2[i]);
}
cnt1[i - d2[i]]++;
val1[i - d2[i]]++;
val1[i] -= d2[i] + 1;
cnt1[i]--;
cnt2[i]++;
val2[i] += d2[i];
cnt2[i + d2[i]]--;
// cerr << d2[i] << " ";
}
// cerr << "\n";
ll sum = 0;
forn(i, 1, n) sum += d1[i] + d2[i];
ll ans = sum;
ll res1 = 0, x1 = 0, res2 = 0, x2 = 0;
forn(i, 0, n) {
res1 += x1;
res2 -= x2;
x1 += cnt1[i];
x2 += cnt2[i];
res1 += val1[i];
res2 += val2[i];
if (!i) continue;
forn(j, 0, 25) {
ans = max(ans, sum - res1 - res2 + addl[i][j] + addr[i][j]);
//if (i == 4 && j == 0) cerr << res1 << " " << res2 << " " << addl[i][j] << " " << addr[i][j] << "\n";
}
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
97532 KB |
Output is correct |
2 |
Correct |
1 ms |
97532 KB |
Output is correct |
3 |
Correct |
0 ms |
97532 KB |
Output is correct |
4 |
Correct |
0 ms |
97532 KB |
Output is correct |
5 |
Correct |
0 ms |
97532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
97532 KB |
Output is correct |
2 |
Correct |
3 ms |
97532 KB |
Output is correct |
3 |
Correct |
6 ms |
97532 KB |
Output is correct |
4 |
Correct |
3 ms |
97532 KB |
Output is correct |
5 |
Correct |
5 ms |
97532 KB |
Output is correct |
6 |
Correct |
6 ms |
97532 KB |
Output is correct |
7 |
Correct |
6 ms |
97532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
97848 KB |
Output is correct |
2 |
Correct |
39 ms |
97848 KB |
Output is correct |
3 |
Correct |
35 ms |
97848 KB |
Output is correct |
4 |
Correct |
69 ms |
97848 KB |
Output is correct |
5 |
Correct |
56 ms |
97848 KB |
Output is correct |
6 |
Correct |
76 ms |
97848 KB |
Output is correct |
7 |
Correct |
80 ms |
97848 KB |
Output is correct |
8 |
Correct |
18 ms |
97848 KB |
Output is correct |
9 |
Correct |
74 ms |
97848 KB |
Output is correct |
10 |
Correct |
87 ms |
97848 KB |
Output is correct |
11 |
Correct |
36 ms |
97848 KB |
Output is correct |
12 |
Correct |
68 ms |
97848 KB |
Output is correct |
13 |
Correct |
75 ms |
97848 KB |
Output is correct |
14 |
Correct |
70 ms |
97848 KB |
Output is correct |
15 |
Correct |
76 ms |
97848 KB |
Output is correct |
16 |
Correct |
38 ms |
97848 KB |
Output is correct |
17 |
Correct |
62 ms |
97848 KB |
Output is correct |
18 |
Correct |
77 ms |
97848 KB |
Output is correct |
19 |
Correct |
76 ms |
97848 KB |
Output is correct |