This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFOR(i,start,end) for(int i = start; i>end; i--)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define all(a) a.begin(), a.end()
#define mt make_tuple
#define mp make_pair
#define v vector
#define sf scanf
#define pf printf
#define dvar(x) cout << #x << " := " << x << "\n"
#define darr(x,n) FOR(i,0,n) cout << #x << "[" << i << "]" << " := " << x[i] << "\n"
typedef long long ll;
typedef long double ld;
typedef pair<int, int > pii;
typedef pair<ll, ll> pll;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void minn(T &a, T b) { a = min(a, b); }
template<class T> void maxx(T &a, T b) { a = max(a, b); }
void io() {
#ifdef LOCAL_PROJECT
freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
#else
/* online submission */
#endif
ios_base::sync_with_stdio(false); cin.tie(NULL);
}
#define MOD 2038072819LL
#define PRI 610639LL
const ll INF = 1e18;
/****************************************************************/
const int MAXN = 100099;
string S;
int N;
ll p[MAXN];
ll forHash[MAXN], revHash[MAXN];
inline ll getfh(int a, int b) {
return (MOD + forHash[b] - (a == 0 ? 0 : forHash[a - 1])) % MOD;
}
inline ll getrh(int a, int b) {
return (MOD + revHash[b] - (a == 0 ? 0 : revHash[a - 1])) % MOD;
}
inline bool equ(int a, int b, int c, int d) { // test a<-b == c->d
return (getrh(a, b) * p[b]) % MOD == (getfh(c, d) * p[N - 1 - c]) % MOD;
}
inline void computeHash() {
p[0] = 1;
FOR(i, 1, N) p[i] = (PRI*p[i - 1]) % MOD;
FOR(i, 0, N) {
forHash[i] = ((ll)S[i] * p[i]) % MOD;
if (i) forHash[i] = (forHash[i] + forHash[i - 1]) % MOD;
}
FOR(i, 0, N) {
revHash[i] = ((ll)S[i] * p[N - 1 - i]) % MOD;
if (i) revHash[i] = (revHash[i] + revHash[i - 1]) % MOD;
}
}
ll lval[MAXN], lcnt[MAXN], rval[MAXN], rcnt[MAXN];
ll add[MAXN][26];
int main() {
io();
cin >> S;
N = S.length();
computeHash();
ll total = 0;
FOR(i, 0, N) FORE(j, i, min(i + 1, N - 1)) {
// binary search for largest palindrom l->i j->r
int lo = 0, hi = min(i + 1, N - j);
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
if (equ(i - mid + 1, i, j, j + mid - 1)) lo = mid;
else hi = mid - 1;
}
int sz = lo;
int l = i - sz + 1, r = j + sz - 1;
total += sz;
// if (lo != 0)
// pf("%d-%d-%d-%d\n", l, i, j, r);
if (i == j && sz > 1) {
lval[l] += l, lcnt[l] += 1;
lval[i] -= l, lcnt[i] -= 1;
rval[i + 1] += r, rcnt[i + 1] += 1;
rval[r + 1] -= r, rcnt[r + 1] -= 1;
}
else if(i!=j && sz>0){
lval[l] += l, lcnt[l] += 1;
lval[j] -= l, lcnt[j] -= 1;
rval[j] += r, rcnt[j] += 1;
rval[r + 1] -= r, rcnt[r + 1] -= 1;
}
int x = l - 1, y = r + 1;
if (x < 0 || y >= N) continue;
lo = 0, hi = min(x, N - y - 1);
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
if (equ(x - mid, x - 1, y + 1, y + mid)) lo = mid;
else hi = mid - 1;
}
add[x][S[y] - 'a'] += lo + 1;
add[y][S[x] - 'a'] += lo + 1;
}
ll lv = 0, lc = 0, rv = 0, rc = 0;
ll best = 0;
FOR(i, 0, N) {
ll diff = 0;
lv += lval[i];
lc += lcnt[i];
rv += rval[i];
rc += rcnt[i];
diff -= ((ll)i + 1LL)*lc - lv;
diff -= rv + rc*(1LL - (ll)i);
FOR(c, 0, 26) if(c!=S[i]-'a')
maxx(best, diff + add[i][c]);
}
cout << total + best << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |