#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
/*typedef __uint128_t L;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull reduce(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
};
FastMod FM(2);*/
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 3e5 + 10;
int base[2] = {313, 369}, mod[2] = {(int)1e9 + 7, (int)1e9 + 9};
int pw[maxn][2];
struct HSH{
int hsh[maxn][2];
HSH(){};
HSH(string s){
memset(hsh, 0, sizeof hsh);
int n = s.size();
for (int i = 1; i <= n; i++){
for (int j = 0; j < 2; j++){
hsh[i][j] = (1ll * hsh[i-1][j] * base[j] + s[i-1] - 'a' + 1) % mod[j];
}
}
}
pii gethsh(int l, int r){
l--;
int res[2];
for (int i = 0; i < 2; i++){
res[i] = (1ll * hsh[r][i] - 1ll * hsh[l][i] * pw[r-l][i] % mod[i] + 1ll * mod[i]) % mod[i];
}
return {res[0], res[1]};
}
};
int n, suf[maxn], rnk[maxn << 1], tmp[maxn], lcp[maxn], dsu[maxn], a[maxn], cnt[maxn];
int W = 1;
string s;
int getdsu(int v){
return (dsu[v] == -1? v: dsu[v] = getdsu(dsu[v]));
}
void merge(int u, int v){
u = getdsu(u);
v = getdsu(v);
dsu[v] = u;
cnt[u] += cnt[v];
}
bool scmp(int x, int y){
return (rnk[x] == rnk[y]? rnk[x+W] < rnk[y+W]: rnk[x] < rnk[y]);
}
void suffarr(string &s){
int n = s.size();
for (int i = 0; i < n; i++){
rnk[i] = s[i] - 'a' + 1;
suf[i] = i;
}
for (;; W <<= 1){
sort(suf, suf + n, scmp);
tmp[suf[0]] = 1;
for (int i = 1; i < n; i++){
tmp[suf[i]] = tmp[suf[i-1]] + scmp(suf[i-1], suf[i]);
}
for (int i = 0; i < n; i++) rnk[i] = tmp[i];
if (rnk[suf[n-1]] == n) return;
}
}
void buildlcp(string &s){
int n = s.size();
int k = 0;
for (int i = 0; i < n; i++){
if (rnk[i] == n) continue;
while (i + k < n && suf[rnk[i]] + k < n && s[i+k] == s[suf[rnk[i]]+k]) k++;
lcp[rnk[i]] = k;
if (k) k--;
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
pw[0][0] = pw[0][1] = 1;
for (int i = 1; i < maxn; i++){
for (int j = 0; j < 2; j++){
pw[i][j] = 1ll * pw[i-1][j] * base[j] % mod[j];
}
}
cin >> s;
string t = s;
reverse(all(t));
HSH S(s);
HSH T(t);
n = s.size();
suffarr(s);
buildlcp(s);
for (int i = 1; i <= n; i++){
int l = 0, r = min(i, n-i+2);
while(l + 1 < r){
int mid = (l + r) >> 1;
int tmp = n + 2 - i;
if (S.gethsh(i-mid, i+mid-1) == T.gethsh(tmp-mid, tmp+mid-1)) l = mid;
else r = mid;
}
a[i] = l;
}
vector<pair<pii,int>> Q;
for (int i = 1; i <= n; i++){
if (i != n) Q.push_back({{lcp[i], i}, 1});
Q.push_back({{a[i], rnk[i-1]}, 2});
}
memset(dsu, -1, sizeof dsu);
memset(cnt, 0, sizeof cnt);
sort(all(Q), greater<pair<pii,int>>());
ll ans = 0;
for (auto [x, y]: Q){
if (y == 1){
merge(x.S, x.S + 1);
int v = getdsu(x.S);
ans = max(ans, 2ll * cnt[v] * x.F);
}
else{
int v = getdsu(x.S);
cnt[v]++;
ans = max(ans, 2ll * cnt[v] * x.F);
}
}
for (int i = 1; i <= n; i++){
int l = 0, r = min(i, n-i+1);
while(l + 1 < r){
int mid = (l + r) >> 1;
int tmp = n + 1 - i;
if (S.gethsh(i-mid, i+mid) == T.gethsh(tmp-mid, tmp+mid)) l = mid;
else r = mid;
}
a[i] = l+1;
}
Q.clear();
for (int i = 1; i <= n; i++){
if (i != n) Q.push_back({{lcp[i], i}, 1});
Q.push_back({{a[i], rnk[i-1]}, 2});
}
memset(dsu, -1, sizeof dsu);
memset(cnt, 0, sizeof cnt);
sort(all(Q), greater<pair<pii,int>>());
for (auto [x, y]: Q){
if (y == 1){
merge(x.S, x.S + 1);
int v = getdsu(x.S);
ans = max(ans, 1ll * (2 * x.F - 1) * cnt[v]);
}
else{
int v = getdsu(x.S);
cnt[v]++;
ans = max(ans, 1ll * (2 * x.F - 1) * cnt[v]);
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
9684 KB |
Output is correct |
2 |
Correct |
8 ms |
9684 KB |
Output is correct |
3 |
Correct |
8 ms |
9684 KB |
Output is correct |
4 |
Correct |
8 ms |
9684 KB |
Output is correct |
5 |
Correct |
9 ms |
9684 KB |
Output is correct |
6 |
Correct |
8 ms |
9732 KB |
Output is correct |
7 |
Correct |
9 ms |
9684 KB |
Output is correct |
8 |
Correct |
8 ms |
9668 KB |
Output is correct |
9 |
Correct |
8 ms |
9676 KB |
Output is correct |
10 |
Correct |
8 ms |
9676 KB |
Output is correct |
11 |
Correct |
8 ms |
9684 KB |
Output is correct |
12 |
Correct |
8 ms |
9672 KB |
Output is correct |
13 |
Correct |
8 ms |
9684 KB |
Output is correct |
14 |
Correct |
8 ms |
9684 KB |
Output is correct |
15 |
Correct |
8 ms |
9620 KB |
Output is correct |
16 |
Correct |
8 ms |
9684 KB |
Output is correct |
17 |
Correct |
8 ms |
9680 KB |
Output is correct |
18 |
Correct |
8 ms |
9680 KB |
Output is correct |
19 |
Correct |
8 ms |
9668 KB |
Output is correct |
20 |
Correct |
9 ms |
9668 KB |
Output is correct |
21 |
Correct |
8 ms |
9684 KB |
Output is correct |
22 |
Correct |
9 ms |
9684 KB |
Output is correct |
23 |
Correct |
9 ms |
9672 KB |
Output is correct |
24 |
Correct |
8 ms |
9684 KB |
Output is correct |
25 |
Correct |
8 ms |
9684 KB |
Output is correct |
26 |
Correct |
8 ms |
9684 KB |
Output is correct |
27 |
Correct |
8 ms |
9672 KB |
Output is correct |
28 |
Correct |
8 ms |
9672 KB |
Output is correct |
29 |
Correct |
8 ms |
9660 KB |
Output is correct |
30 |
Correct |
8 ms |
9684 KB |
Output is correct |
31 |
Correct |
8 ms |
9684 KB |
Output is correct |
32 |
Correct |
8 ms |
9672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
9796 KB |
Output is correct |
2 |
Correct |
9 ms |
9812 KB |
Output is correct |
3 |
Correct |
9 ms |
9812 KB |
Output is correct |
4 |
Correct |
9 ms |
9796 KB |
Output is correct |
5 |
Correct |
10 ms |
9812 KB |
Output is correct |
6 |
Correct |
10 ms |
9804 KB |
Output is correct |
7 |
Correct |
10 ms |
9812 KB |
Output is correct |
8 |
Correct |
10 ms |
9812 KB |
Output is correct |
9 |
Correct |
10 ms |
9812 KB |
Output is correct |
10 |
Correct |
9 ms |
9800 KB |
Output is correct |
11 |
Correct |
9 ms |
9812 KB |
Output is correct |
12 |
Correct |
9 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
10420 KB |
Output is correct |
2 |
Correct |
23 ms |
10344 KB |
Output is correct |
3 |
Correct |
30 ms |
10252 KB |
Output is correct |
4 |
Correct |
27 ms |
10324 KB |
Output is correct |
5 |
Correct |
23 ms |
10248 KB |
Output is correct |
6 |
Correct |
25 ms |
10248 KB |
Output is correct |
7 |
Correct |
24 ms |
10304 KB |
Output is correct |
8 |
Correct |
20 ms |
10324 KB |
Output is correct |
9 |
Correct |
20 ms |
10332 KB |
Output is correct |
10 |
Correct |
25 ms |
10316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
239 ms |
15124 KB |
Output is correct |
2 |
Correct |
215 ms |
14792 KB |
Output is correct |
3 |
Correct |
257 ms |
14408 KB |
Output is correct |
4 |
Correct |
252 ms |
14448 KB |
Output is correct |
5 |
Correct |
220 ms |
14452 KB |
Output is correct |
6 |
Correct |
211 ms |
14408 KB |
Output is correct |
7 |
Correct |
223 ms |
14460 KB |
Output is correct |
8 |
Correct |
151 ms |
14408 KB |
Output is correct |
9 |
Correct |
227 ms |
14452 KB |
Output is correct |
10 |
Correct |
237 ms |
14572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
774 ms |
26676 KB |
Output is correct |
2 |
Correct |
680 ms |
26844 KB |
Output is correct |
3 |
Correct |
865 ms |
26772 KB |
Output is correct |
4 |
Correct |
789 ms |
26772 KB |
Output is correct |
5 |
Correct |
796 ms |
26780 KB |
Output is correct |
6 |
Correct |
741 ms |
26864 KB |
Output is correct |
7 |
Correct |
729 ms |
26852 KB |
Output is correct |
8 |
Correct |
492 ms |
26768 KB |
Output is correct |
9 |
Correct |
488 ms |
26780 KB |
Output is correct |
10 |
Correct |
835 ms |
26780 KB |
Output is correct |
11 |
Correct |
699 ms |
26780 KB |
Output is correct |
12 |
Correct |
758 ms |
26900 KB |
Output is correct |