/*input
www
a
aba
abacaba
acaba
ba
bacaba
caba
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
namespace SuffixArray {
int pos[N], sa[N], tmp[N], lcp[N], gap, n;
string s;
bool SuffixCmp(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(string x) {
n = (int)x.size(), s = x;
for(int i = 0; i < n; ++i) pos[i] = s[i], sa[i] = i;
for(gap = 1; ; gap *= 2) {
sort(sa, sa + n, SuffixCmp);
for(int i = 1; i < n; ++i) tmp[i] = tmp[i - 1] + SuffixCmp(sa[i - 1], sa[i]);
for(int i = 0; i < n; ++i) pos[sa[i]] = tmp[i];
if(tmp[n - 1] == n - 1) break;
}
}
void Kasai() {
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 != 0) --k;
}
}
}
namespace MaxRange {
int lef[N], rig[N], mn[18][N];
int get(int l, int r) {
int dist = log2(r - l + 1);
return min(mn[dist][l], mn[dist][r - (1 << dist) + 1]);
}
long long Solve(vector<int> &x, vector<int> &val) {
int n = x.size();
for(int i = 1; i < n - 1; ++i)
mn[0][i] = x[i];
for(int i = 1; (1 << i) < n - 1; ++i)
for(int j = 1; j + (1 << i) < n; ++j)
mn[i][j] = min(mn[i - 1][j], mn[i - 1][j + (1 << (i - 1))]);
long long Ans = 0;
for(int i = 1; i < n - 1; ++i) {
int le = 1, ri = i - 1, l, r;
if(i == 1) l = 1;
else if(x[i - 1] < val[i]) l = i;
else {
while(le < ri) {
int mid = (le + ri) >> 1;
if(get(mid, i - 1) < val[i]) le = mid + 1;
else ri = mid;
}
l = le;
}
if(i == n - 2) r = i;
else if(x[i] < val[i]) r = i;
else {
le = i, ri = n - 2;
while(le < ri) {
int mid = (le + ri + 1) >> 1;
if(get(i, mid) < val[i]) ri = mid - 1;
else le = mid;
}
r = le + 1;
}
Ans = max(Ans, 1ll * (r - l + 1) * val[i]);
}
return Ans;
}
}
int main(){
iostream::sync_with_stdio(false); cin.tie(0);
string s; cin >> s;
int n = (int)s.size();
SuffixArray::buildSA(s);
SuffixArray::Kasai();
vector<int> v, val;
v.reserve(n + 2), val.reserve(n + 2);
v.emplace_back(- 1), val.emplace_back(0);
for(int i = 0; i < n; ++i) {
v.emplace_back(SuffixArray::lcp[i]);
val.emplace_back(n - SuffixArray::sa[i]);
}
v.emplace_back(- 1), val.emplace_back(0);
printf("%lld\n", MaxRange::Solve(v, val));
// cerr << fixed << setprecision(6) << (double)clock() / CLOCKS_PER_SEC << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
484 KB |
Output is correct |
3 |
Correct |
1 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Incorrect |
1 ms |
708 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
1584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
201 ms |
9660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
653 ms |
29228 KB |
Output is correct |
2 |
Incorrect |
482 ms |
29556 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |