이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//In the name of God
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll maxn = 3e5 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
#define lc (id << 1)
#define rc (lc | 1)
ll n, p[maxn], rnk[maxn], r2[maxn], lcp[maxn];
string s;
ll h;
vector<ll> vec[maxn];
bool cmp(ll i, ll j){
if(rnk[i] < rnk[j]) return 1;
if(rnk[i] > rnk[j]) return 0;
i += h;
j += h;
if(i >= n) i -= n;
if(j >= n) j -= n;
return (rnk[i] < rnk[j]);
}
ll seg[maxn << 2];
void bld(ll id, ll s, ll e){
if(e - s == 1){
seg[id] = lcp[s];
return;
}
ll mid = (s + e) >> 1;
bld(lc, s, mid);
bld(rc, mid, e);
seg[id] = min(seg[lc], seg[rc]);
return;
}
ll get(ll id, ll s, ll e, ll l, ll r){
if(r <= s || e <= l) return inf;
if(l <= s && e <= r) return seg[id];
ll mid = (s + e) >> 1;
return min(get(lc, s, mid, l, r), get(rc, mid, e, l, r));
}
int main(){
fast_io;
cin >> s;
s += '#';
n = Sz(s);
for(ll i = 0; i < n; i++){
rnk[i] = ll(s[i] - 'a');
p[i] = i;
}
h = 0;
sort(p, p + n, cmp);
r2[p[0]] = 0;
for(ll i = 1; i < n; i++){
r2[p[i]] = r2[p[i - 1]];
if(cmp(p[i - 1], p[i])) r2[p[i]]++;
}
for(ll i = 0; i < n; i++){
rnk[i] = r2[i];
}
h = 1;
while(h < n){
sort(p, p + n, cmp);
r2[p[0]] = 0;
for(ll i = 1; i < n; i++){
r2[p[i]] = r2[p[i - 1]];
if(cmp(p[i - 1], p[i])) r2[p[i]]++;
}
for(ll i = 0; i < n; i++){
rnk[i] = r2[i];
}
h <<= 1;
}
for(ll i = 0, k = 0; i < n; i++){
if(rnk[i] == n - 1){
k = 0;
continue;
}
ll j = p[rnk[i] + 1];
while(i + k < n && j + k < n && s[i + k] == s[j + k]) k++;
lcp[rnk[i]] = k;
if(k) k--;
}
bld(1, 0, n);
n--;
vector<int> d1(n);
for (int i = 0, l = 0, r = -1; i < n; i++) {
int k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1);
while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) {
k++;
}
d1[i] = k--;
if (i + k > r) {
l = i - k;
r = i + k;
}
}
vector<int> d2(n);
for (int i = 0, l = 0, r = -1; i < n; i++) {
int k = (i > r) ? 0 : min(d2[l + r - i + 1], r - i + 1);
while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) {
k++;
}
d2[i] = k--;
if (i + k > r) {
l = i - k - 1;
r = i + k;
}
}
set<ll> st;
ll ans = 0;
for(ll i = n; i--;){
for(ll j : vec[i]){
st.erase(j);
}
vec[i].clear();
st.insert(i);
vec[i - d1[i]].pb(i);
ll t = (*st.rbegin() - i) * 2 + 1;
if(t <= lcp[rnk[i] - 1]) continue;
ll l = rnk[i], r = n + 2;
while(r - l > 1){
ll mid = (l + r) >> 1;
if(get(1, 0, n + 1, rnk[i], mid) >= t) l = mid;
else r = mid;
}
ans = max(ans, t * (r - rnk[i]));
}
st.clear();
for(ll i = n - 1; i--;){
for(ll j : vec[i]){
st.erase(j);
}
if(d2[i + 1]) st.insert(i);
if(d2[i + 1]) vec[i - d2[i + 1]].pb(i);
if(st.empty()) continue;
ll t = (*st.rbegin() - i) * 2 + 2;
if(t <= lcp[rnk[i] - 1]) continue;
ll l = rnk[i], r = n + 2;
while(r - l > 1){
ll mid = (l + r) >> 1;
if(get(1, 0, n + 1, rnk[i], mid) >= t) l = mid;
else r = mid;
}
ans = max(ans, t * (r - rnk[i]));
}
cout << ans;
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... |