This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
#define pp pop_back
#define sz(a) (int)(a.size())
#define mp make_pair
#define F first
#define S second
#define next _next
#define prev _prev
#define left _left
#define right _right
#define y1 _y1
#define all(x) x.begin(), x.end()
#define what_is(x) #x << " is " << (x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = (int)5e5 + 123;
const ll INF = (ll)1e18 + 123;
const int inf = (int)1e9 + 123;
void megaRandom() {
unsigned int FOR;
asm("rdtsc" : "=A"(FOR));
srand(FOR);
}
int mult(int a, int b, int MOD) {
return 1ll * a * b % MOD;
}
int add(int a, int b, int MOD) {
a += b;
if(a >= MOD) a -= MOD;
return a;
}
int subs(int a, int b, int MOD) {
a -= b;
if(a < 0) a += MOD;
return a;
}
string s;
int h[3][N], P[3], pw[3][N], MOD[3];
int hr[3][N];
void build() {
h[1][0] = h[2][0] = s[0];
for(int it = 1; it <= 2; it ++)
for(int i = 1; i < sz(s); i ++)
h[it][i] = add(mult(h[it][i - 1], P[it], MOD[it]), s[i], MOD[it]);
string b = s;
reverse(all(b));
hr[1][0] = hr[2][0] = b[0];
for(int it = 1; it <= 2; it ++)
for(int i = 1; i < sz(b); i ++)
hr[it][i] = add(mult(hr[it][i - 1], P[it], MOD[it]), b[i], MOD[it]);
}
pii getHash(int l, int r) {
//assert(l >= 0 && r < sz(s) && l <= r);
int A = subs(h[1][r], mult(h[1][l - 1], pw[1][r - l + 1], MOD[1]), MOD[1]);
int B = subs(h[2][r], mult(h[2][l - 1], pw[2][r - l + 1], MOD[2]), MOD[2]);
return {A, B};
}
pii getHashR(int l, int r) {
int l1 = sz(s) - r - 1, r1 = sz(s) - l - 1;
l = l1, r = r1;
//assert(l >= 0 && r < sz(s) && l <= r);
int A = subs(hr[1][r], mult(hr[1][l - 1], pw[1][r - l + 1], MOD[1]), MOD[1]);
int B = subs(hr[2][r], mult(hr[2][l - 1], pw[2][r - l + 1], MOD[2]), MOD[2]);
return {A, B};
}
int length(int i, int j) {
if(s[i] != s[j])
return 0;
int l = 0, r = min(i, sz(s) - j) + 1;
while(r - l > 1) {
int mid = (l + r) >> 1;
if(i - mid >= 0 && j + mid < sz(s) && getHash(i - mid, i) == getHashR(j, j + mid))
l = mid;
else
r = mid;
}
return l;
}
map<pii, int> newId;
int vtr;
vector<int> g[N];
int chest[N], dlina[N];
int pr[N];
void calcOdd() {
for(int i = 0; i < sz(s); i ++) {
int tmp = length(i, i), bmp = tmp;
int prev = -1;
while(tmp >= 0) {
int kepka = newId[getHash(i - tmp, i + tmp)];
if(!kepka) {
newId[getHash(i - tmp, i + tmp)] = ++ vtr;
dlina[vtr] = tmp * 2 + 1;
if(prev != -1) {
g[vtr].pb(prev);
pr[prev] = vtr;
}
prev = vtr;
} else {
if(prev != -1) {
g[kepka].pb(prev);
pr[prev] = kepka;
}
break;
}
tmp --;
}
chest[newId[getHash(i - bmp, i + bmp)]] ++;
}
}
int sub[N];
ll res;
void dfs(int x) {
sub[x] += chest[x];
for(auto to : g[x]) {
dfs(to);
sub[x] += sub[to];
}
res = max(res, 1ll * sub[x] * dlina[x]);
}
void clear() {
memset(sub, 0, sizeof sub);
for(int i = 1; i <= vtr; i ++)
g[i].clear();
vtr = 0;
memset(chest, 0, sizeof chest);
memset(dlina, 0, sizeof dlina);
memset(pr, 0, sizeof pr);
newId.clear();
}
void calcEven() {
for(int i = 1; i < sz(s); i ++) {
if(s[i - 1] != s[i])
continue;
int tmp = length(i - 1, i), bmp = tmp;
int prev = -1;
while(tmp >= 0) {
int kepka = newId[getHash(i - tmp - 1, i + tmp)];
if(!kepka) {
newId[getHash(i - tmp - 1, i + tmp)] = ++ vtr;
dlina[vtr] = (tmp + 1) * 2;
if(prev != -1) {
g[vtr].pb(prev);
pr[prev] = vtr;
}
prev = vtr;
} else {
if(prev != -1) {
g[kepka].pb(prev);
pr[prev] = kepka;
}
break;
}
tmp --;
}
chest[newId[getHash(i - bmp - 1, i + bmp)]] ++;
}
}
int main() {
megaRandom();
ios_base::sync_with_stdio(0);
cin.tie(0);
P[1] = 31, P[2] = 39;
MOD[1] = (int)1e9 + 7, MOD[2] = (int)1e9 + 9;
for(int it = 1; it <= 2; it ++) {
pw[it][0] = 1;
for(int i = 1; i <= N - 123; i ++)
pw[it][i] = mult(pw[it][i - 1], P[it], MOD[it]);
}
cin >> s;
build();
calcOdd();
for(int i = 1; i <= vtr; i ++) {
if(pr[i]) continue;
dfs(i);
}
clear();
calcEven();
for(int i = 1; i <= vtr; i ++) {
if(pr[i]) continue;
dfs(i);
}
cout << res;
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |