#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)3e5 + 3;
const int MAXS = (int)100;
const int infint = (int)1e9;
string s;
int n, gap, sa[MAXN], pos[MAXN], tmp[MAXN], lcp[MAXN], par[MAXN], home[MAXN];
vector<int> oc[MAXN];
bool sufCmp(int i, int j)
{
if (pos[i] != pos[j])
return pos[i] < pos[j];
i += gap;
j += gap;
return (i < n && j < n) ? pos[i] < pos[j] : i > j;
}
void buildSA()
{
n = s.size();
for (int i = 0; i < n; i++)
sa[i] = i, pos[i] = s[i];
for (gap = 1; ; gap *= 2)
{
sort(sa, sa + n, sufCmp);
for (int i = 0; i < n - 1; i++)
tmp[i + 1] = tmp[i] + sufCmp(sa[i], sa[i + 1]);
for (int i = 0; i < n; i++)
pos[sa[i]] = tmp[i];
if (tmp[n - 1] == n - 1)
break;
}
}
void buildLCP()
{
for (int i = 0, k = 0; i < n; ++i)
if (pos[i] != n - 1)
{
for (int j = sa[pos[i] + 1]; s[i + k] == s[j + k];)
k++;
lcp[pos[i]] = k;
if (k)
k--;
}
}
int get(int u)
{
return par[u] < 0 ? u : par[u] = get(par[u]);
}
void merge(int u, int v)
{
if((u = get(u)) != (v = get(v)))
return;
if(par[u] > par[v])
swap(u, v); //par[u] > par[v]
par[u] += par[v];
par[v] = u;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> s;
buildSA();
buildLCP();
memset(par, -1, sizeof par);
ll ans = 0, cnt = 0;
for (int i = 0; i < n - 1; i++)
oc[lcp[i]].push_back(i);
for (int i = n; i >= 0; i--)
{
for (auto u : oc[i])
{
home[u] = 1, cnt = max(1LL, cnt);
if(u && home[u - 1] == 1)
{
merge(u, u - 1);
cnt = max(cnt, (ll)-par[get(u)]);
}
if(home[u + 1] == 1)
{
merge(u, u + 1);
cnt = max(cnt, (ll)-par[get(u)]);
}
}
ans = max(ans, i * (cnt + 1));
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
8572 KB |
Output is correct |
2 |
Correct |
10 ms |
8576 KB |
Output is correct |
3 |
Correct |
9 ms |
8644 KB |
Output is correct |
4 |
Correct |
10 ms |
8660 KB |
Output is correct |
5 |
Incorrect |
9 ms |
8712 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
8848 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
9248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
178 ms |
13996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
636 ms |
24732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |