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)3e5 + 123;
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];
hr[1][0] = hr[2][0] = s[sz(s) - 1];
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]);
hr[it][i] = add(mult(hr[it][i - 1], P[it], MOD[it]), s[sz(s) - i - 1], MOD[it]);
}
}
}
int hashPair(pii A) {
return (A.F << 16) ^ (A.S >> 16);
}
int getHash(int l, int 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 hashPair(mp(A, B));
}
int getHashR(int l, int r) {
int l1 = sz(s) - r - 1, r1 = sz(s) - l - 1;
l = l1, r = r1;
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 hashPair(mp(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;
}
unordered_map<ll, int> newId;
int vtr;
vector<int> g[N];
int chest[N], dlina[N];
int pr[N];
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 calc(bool op) {
for(int i = op; i < sz(s); i ++) {
if(s[i - op] != s[i])
continue;
int tmp = length(i - op, i), bmp = tmp;
int prev = -1;
while(tmp >= 0) {
int kepka = newId[getHash(i - tmp - op, i + tmp)];
if(!kepka) {
newId[getHash(i - tmp - op, i + tmp)] = ++ vtr;
dlina[vtr] = ((tmp + op) << 1) + (op ^ 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 - op, i + bmp)]] ++;
}
}
int main() {
P[1] = 139, P[2] = 59;
MOD[1] = (int)1e9 + 7, MOD[2] = (int)1e9 + 3;
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();
calc(0);
calc(1);
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... |