#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#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;
inline int mult(int a, int b, int MOD) {
return 1ll * a * b % MOD;
}
inline int add(int a, int b, int MOD) {
a += b;
if(a >= MOD) a -= MOD;
return a;
}
inline 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]);
}
}
}
inline ll hashPair(pii A) {
return ((ll)A.F << 16) ^ ((ll)A.S >> 16);
}
inline ll 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));
}
inline ll 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));
}
inline 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) {
if(!newId.count(getHash(i - tmp - op, i + tmp))) {
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) {
int kepka = newId[getHash(i - tmp - op, i + tmp)];
g[kepka].pb(prev);
pr[prev] = kepka;
}
break;
}
tmp --;
}
chest[newId[getHash(i - bmp - op, i + bmp)]] ++;
}
}
int main() {
P[1] = 31, P[2] = 97;
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]);
}
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> s;
newId.max_load_factor(0.25);
newId.reserve(sz(s) << 1);
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 |
1 |
Correct |
13 ms |
9720 KB |
Output is correct |
2 |
Correct |
13 ms |
9952 KB |
Output is correct |
3 |
Correct |
13 ms |
9952 KB |
Output is correct |
4 |
Correct |
13 ms |
10092 KB |
Output is correct |
5 |
Correct |
13 ms |
10092 KB |
Output is correct |
6 |
Correct |
12 ms |
10092 KB |
Output is correct |
7 |
Correct |
12 ms |
10092 KB |
Output is correct |
8 |
Correct |
13 ms |
10092 KB |
Output is correct |
9 |
Correct |
13 ms |
10092 KB |
Output is correct |
10 |
Correct |
13 ms |
10092 KB |
Output is correct |
11 |
Correct |
13 ms |
10092 KB |
Output is correct |
12 |
Correct |
12 ms |
10092 KB |
Output is correct |
13 |
Correct |
13 ms |
10092 KB |
Output is correct |
14 |
Correct |
13 ms |
10092 KB |
Output is correct |
15 |
Correct |
13 ms |
10092 KB |
Output is correct |
16 |
Correct |
13 ms |
10092 KB |
Output is correct |
17 |
Correct |
13 ms |
10092 KB |
Output is correct |
18 |
Correct |
13 ms |
10092 KB |
Output is correct |
19 |
Correct |
13 ms |
10092 KB |
Output is correct |
20 |
Correct |
13 ms |
10092 KB |
Output is correct |
21 |
Correct |
13 ms |
10092 KB |
Output is correct |
22 |
Correct |
13 ms |
10092 KB |
Output is correct |
23 |
Correct |
13 ms |
10092 KB |
Output is correct |
24 |
Correct |
13 ms |
10092 KB |
Output is correct |
25 |
Correct |
13 ms |
10092 KB |
Output is correct |
26 |
Correct |
13 ms |
10092 KB |
Output is correct |
27 |
Correct |
14 ms |
10092 KB |
Output is correct |
28 |
Correct |
14 ms |
10092 KB |
Output is correct |
29 |
Correct |
13 ms |
10092 KB |
Output is correct |
30 |
Correct |
14 ms |
10092 KB |
Output is correct |
31 |
Correct |
13 ms |
10092 KB |
Output is correct |
32 |
Correct |
13 ms |
10096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
10220 KB |
Output is correct |
2 |
Correct |
14 ms |
10220 KB |
Output is correct |
3 |
Correct |
15 ms |
10220 KB |
Output is correct |
4 |
Correct |
14 ms |
10220 KB |
Output is correct |
5 |
Correct |
14 ms |
10220 KB |
Output is correct |
6 |
Correct |
14 ms |
10220 KB |
Output is correct |
7 |
Correct |
13 ms |
10220 KB |
Output is correct |
8 |
Correct |
14 ms |
10220 KB |
Output is correct |
9 |
Correct |
13 ms |
10220 KB |
Output is correct |
10 |
Correct |
13 ms |
10220 KB |
Output is correct |
11 |
Correct |
14 ms |
10220 KB |
Output is correct |
12 |
Correct |
14 ms |
10220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
11756 KB |
Output is correct |
2 |
Correct |
25 ms |
11756 KB |
Output is correct |
3 |
Correct |
32 ms |
11756 KB |
Output is correct |
4 |
Correct |
33 ms |
11756 KB |
Output is correct |
5 |
Correct |
24 ms |
11756 KB |
Output is correct |
6 |
Correct |
26 ms |
11756 KB |
Output is correct |
7 |
Correct |
25 ms |
11756 KB |
Output is correct |
8 |
Correct |
22 ms |
11756 KB |
Output is correct |
9 |
Correct |
22 ms |
11756 KB |
Output is correct |
10 |
Correct |
26 ms |
11756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
184 ms |
27500 KB |
Output is correct |
2 |
Correct |
180 ms |
27500 KB |
Output is correct |
3 |
Correct |
290 ms |
27500 KB |
Output is correct |
4 |
Correct |
304 ms |
27500 KB |
Output is correct |
5 |
Correct |
161 ms |
27500 KB |
Output is correct |
6 |
Correct |
158 ms |
27500 KB |
Output is correct |
7 |
Correct |
219 ms |
27500 KB |
Output is correct |
8 |
Correct |
129 ms |
27500 KB |
Output is correct |
9 |
Correct |
163 ms |
27500 KB |
Output is correct |
10 |
Correct |
185 ms |
27500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
588 ms |
62224 KB |
Output is correct |
2 |
Correct |
576 ms |
62224 KB |
Output is correct |
3 |
Correct |
947 ms |
62224 KB |
Output is correct |
4 |
Correct |
949 ms |
62224 KB |
Output is correct |
5 |
Correct |
521 ms |
62224 KB |
Output is correct |
6 |
Correct |
643 ms |
62224 KB |
Output is correct |
7 |
Correct |
716 ms |
62224 KB |
Output is correct |
8 |
Correct |
404 ms |
62224 KB |
Output is correct |
9 |
Correct |
403 ms |
62224 KB |
Output is correct |
10 |
Correct |
584 ms |
62224 KB |
Output is correct |
11 |
Correct |
611 ms |
62224 KB |
Output is correct |
12 |
Correct |
432 ms |
62224 KB |
Output is correct |