#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for(auto& a : x)
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 200005;
int n;
string s;
int d1[mxN];
int d2[mxN];
void manacher(string a){
int N = sz(a);
int L = 0, R = -1;
ff(i,0,N - 1){
int j = (i > R ? 1 : min(d1[L + R - i], R - i + 1));
while(i >= j && i + j < N && a[i - j] == a[i + j])j += 1;
d1[i] = j--;
if(i + j > R){
L = i - j;
R = i + j;
}
}
L = 0, R = -1;
ff(i,0,N - 1){
int j = (i > R ? 0 : min(d2[L + R - i + 1], R - i + 1));
while(i > j && i + j < N && a[i - j - 1] == a[i + j])j += 1;
d2[i] = j--;
if(i + j > R){
L = i - j - 1;
R = i + j;
}
}
}
ll kol[mxN];
vector<int> L1[mxN];
vector<int> R1[mxN];
vector<int> L2[mxN];
vector<int> R2[mxN];
ll in[mxN][2];
int add(int a, int b){ a += b; if(a >= mod)a -= mod; return a; }
int sub(int a, int b){ a -= b; if(a < 0)a += mod; return a; }
int mul(int a, int b){ return (1ll * a * b) % mod; }
int power(int a, int b){
if(!b)return 1;
int p = power(a, b / 2);
p = mul(p, p);
if(b % 2 == 1)p = mul(p, a);
return p;
}
int inv(int a){
return power(a, mod - 2);
}
const int p1 = 37;
int pw[mxN];
int invz[mxN];
int pref[mxN];
int getP(int l, int r){
return mul(sub(pref[r], (l == 0 ? 0 : pref[l - 1])), invz[l]);
}
int sufi[mxN];
int getS(int l, int r){
return mul(sub(sufi[l], sufi[r + 1]), invz[n - r - 1]);
}
int calc(int a, int b ){
int l = 1, r = n, ans = 0;
while(l <= r){
int mid = (l + r) / 2;
if(a - mid + 1 >= 0 && b + mid - 1 < n && getP(a - mid + 1, a) == getS(b, b + mid - 1)){
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
return ans;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> s; n = sz(s);
pw[0] = 1;
ff(i,1,n)pw[i] = mul(pw[i - 1], p1);
invz[n] = inv(pw[n]);
fb(i,n - 1,0)invz[i] = mul(invz[i + 1], p1);
ff(i,0,n - 1)pref[i] = add((i == 0 ? 0 : pref[i - 1]), mul(s[i] - 'a' + 1, pw[i]));
fb(i,n - 1,0)sufi[i] = add(sufi[i + 1], mul(s[i] - 'a' + 1, pw[n - i - 1]));
manacher(s);
ll uk = 0;
ff(i,0,n - 1)uk += d1[i] + d2[i];
ff(i,0,n - 1){
L1[i + d1[i] - 1].pb(i);
L2[i + d2[i] - 1].pb(i);
R1[i - d1[i] + 1].pb(i);
R2[i - d2[i]].pb(i);
kol[i] = uk;
}
ll sum = 0, br = 0;
ff(i,0,n - 1){
if(d2[i] > 0){
in[i + d2[i] - 1][0] += (i + d2[i]);
in[i + d2[i] - 1][1] += 1;
sum += (i + d2[i]); br += 1;
}
kol[i] += 1ll * br * i - sum;
in[i + d1[i] - 1][0] += (i + d1[i]);
in[i + d1[i] - 1][1] += 1;
sum += (i + d1[i]); br += 1;
sum -= in[i][0];
br -= in[i][1];
}
ff(i,0,n - 1)ff(j,0,1)in[i][j] = 0;
sum = 0, br = 0;
fb(i,n - 1,0){
kol[i] += sum - 1ll * br * i;
in[i - d1[i] + 1][0] += (i - d1[i]);
in[i - d1[i] + 1][1] += 1;
sum += (i - d1[i]); br += 1;
if(d2[i] > 0){
in[i - d2[i]][0] += (i - 1 - d2[i]);
in[i - d2[i]][1] += 1;
sum += (i - 1 - d2[i]); br += 1;
}
sum -= in[i][0];
br -= in[i][1];
}
ff(i,0,n - 1){
char old = s[i]; ll mx = 0;
for(char a = 'a'; a <= 'z'; a++){
if(old == a)continue;
s[i] = a; ll cur = 0;
for(auto j : L1[i - 1]){
int l = 2 * j - i, r = i;
if(0 <= l && l < n && 0 <= r && r < n && l <= r && s[l] == s[r]){
l -= 1; r += 1;
cur += 1 + calc(l, r);
}
}
for(auto j : L2[i - 1]){
int l = 2 * j - i - 1, r = i;
if(0 <= l && l < n && 0 <= r && r < n && l <= r && s[l] == s[r]){
l -= 1; r += 1;
cur += 1 + calc(l, r);
}
}
for(auto j : R1[i + 1]){
int l = i, r = 2 * j - i;
if(0 <= l && l < n && 0 <= r && r < n && l <= r && s[l] == s[r]){
l -= 1; r += 1;
cur += 1 + calc(l, r);
}
}
for(auto j : R2[i + 1]){
int l = i, r = 2 * j - i - 1;
if(0 <= l && l < n && 0 <= r && r < n && l <= r && s[l] == s[r]){
l -= 1; r += 1;
cur += 1 + calc(l, r);
}
}
mx = max(mx, cur);
}
s[i] = old; kol[i] += mx;
}
ll rez = uk;
ff(i,0,n - 1)rez = max(rez, kol[i]);
cout << rez << '\n';
return 0;
}
/*
// probati bojenje sahovski
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
26968 KB |
Output is correct |
2 |
Correct |
4 ms |
26968 KB |
Output is correct |
3 |
Correct |
4 ms |
27100 KB |
Output is correct |
4 |
Correct |
4 ms |
26972 KB |
Output is correct |
5 |
Correct |
4 ms |
27088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
27480 KB |
Output is correct |
2 |
Correct |
6 ms |
27480 KB |
Output is correct |
3 |
Correct |
7 ms |
27484 KB |
Output is correct |
4 |
Correct |
6 ms |
27484 KB |
Output is correct |
5 |
Correct |
7 ms |
27484 KB |
Output is correct |
6 |
Correct |
9 ms |
27480 KB |
Output is correct |
7 |
Correct |
7 ms |
27740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
37244 KB |
Output is correct |
2 |
Correct |
52 ms |
34968 KB |
Output is correct |
3 |
Correct |
46 ms |
35152 KB |
Output is correct |
4 |
Correct |
92 ms |
37724 KB |
Output is correct |
5 |
Correct |
86 ms |
37720 KB |
Output is correct |
6 |
Correct |
95 ms |
37752 KB |
Output is correct |
7 |
Correct |
85 ms |
37716 KB |
Output is correct |
8 |
Correct |
29 ms |
34896 KB |
Output is correct |
9 |
Correct |
88 ms |
37756 KB |
Output is correct |
10 |
Correct |
87 ms |
37744 KB |
Output is correct |
11 |
Correct |
49 ms |
34896 KB |
Output is correct |
12 |
Correct |
77 ms |
35648 KB |
Output is correct |
13 |
Correct |
80 ms |
36956 KB |
Output is correct |
14 |
Correct |
83 ms |
37884 KB |
Output is correct |
15 |
Correct |
82 ms |
37464 KB |
Output is correct |
16 |
Correct |
56 ms |
34908 KB |
Output is correct |
17 |
Correct |
85 ms |
40016 KB |
Output is correct |
18 |
Correct |
88 ms |
37980 KB |
Output is correct |
19 |
Correct |
86 ms |
40112 KB |
Output is correct |