This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vi vector<int>
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ull H
using namespace std;
const int MAXN = 3e5 + 10;
const int MOD[2] = {(int)(1e9 + 7), (int)(1e9 + 9)};
struct H{
ll h[2];
H(int x = 0, int y = 0){
h[0] = x;
h[1] = y;
}
};
H operator+(const H& a, const H& b){
H c = a;
for (int i = 0; i < 2; i++){
c.h[i] = (a.h[i] + b.h[i]) % MOD[i];
}
return c;
}
H operator-(const H& a, const H& b){
H c = a;
for (int i = 0; i < 2; i++){
c.h[i] = (a.h[i] - b.h[i] + MOD[i]) % MOD[i];
}
return c;
}
H operator*(const H& a, const H& b){
H c = a;
for (int i = 0; i < 2; i++){
c.h[i] = (a.h[i] * 1ll * b.h[i]) % MOD[i];
}
return c;
}
bool operator==(const H& a, const H& b){
for (int i = 0; i < 2; i++){
if (a.h[i] != b.h[i]){
return 0;
}
}
return 1;
}
ull pw[MAXN + 10];
ull pref[MAXN];
ull suff[MAXN];
int n;
ull h(int i, int j){
return (pref[j] - pref[i - 1]) * pw[MAXN - i + 1];
}
ull rh(int i, int j){
return (suff[i] - suff[j + 1]) * pw[MAXN + j - n];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef ON_PC
freopen("input.txt", "r", stdin);
#endif
// freopen("output.txt", "w", stdout);
pw[0] = {1, 1};
for (int i = 1; i < MAXN + 10; i++){
pw[i] = pw[i - 1] * H{57, 113};
}
int T = 1;
// cin >> T;
while (T--){
string s;
cin >> s;
n = s.size();
s = " " + s;
for (int i = 1; i <= n; i++){
pref[i] = pref[i - 1] + (s[i] - 'a' + 1) * pw[i - 1];
}
for (int i = n; i >= 1; i--){
suff[i] = suff[i + 1] + (s[i] - 'a' + 1) * pw[n - i];
}
ll res = 0;
for (int f = 0; f < 2; f++){
vector<int> d(n + 1);
for (int i = 1; i <= n; i++){
int l = 0, r = min(i, n - (i + f) + 1) + 1;
while (r - l > 1){
int m = (l + r) / 2;
if (rh(i - m + 1, i) == h(i + f, i + f + m - 1)){
l = m;
} else {
r = m;
}
}
d[i] = l;
}
for (int i = 1; i <= n; i++){
res = max(res, (ll)(2 * d[i] - !f));
}
vector<int> order(n);
for (int i = 1; i <= n; i++){
order[i - 1] = i;
}
function<int(int, int)> lcp = [&](int i, int j){
int l = 0, r = min(d[i], d[j]) + 1;
while (r - l > 1){
int m = (l + r) / 2;
if (h(i - m + 1, i) == h(j - m + 1, j)){
l = m;
} else {
r = m;
}
}
return l;
};
sort(all(order), [&](int i, int j){
int l = lcp(i, j);
if (l == min(d[i], d[j])){
if (d[i] != d[j]){
return d[i] < d[j];
}
return i < j;
}
return s[i - l] < s[j - l];
});
vector<int> lcp_res(n);
for (int i = 0; i + 1 < n; i++){
lcp_res[i] = lcp(order[i], order[i + 1]);
}
vector<int> lb(n);
vector<int> rb(n);
vector<int> st;
for (int i = 0; i < n; i++){
while (!st.empty() && lcp_res[st.back()] >= lcp_res[i]){
st.pop_back();
}
lb[i] = 0;
if (!st.empty()){
lb[i] = st.back() + 1;
}
st.push_back(i);
}
st.clear();
for (int i = n - 1; i >= 0; i--){
while (!st.empty() && lcp_res[st.back()] >= lcp_res[i]){
st.pop_back();
}
rb[i] = n - 1;
if (!st.empty()){
rb[i] = st.back();
}
st.push_back(i);
}
st.clear();
for (int i = 0; i < n; i++){
res = max(res, (2 * lcp_res[i] - !f) * 1ll * (rb[i] - lb[i] + 1));
// int mn = d[order[i]];
// for (int j = i; j < n; j++){
// res = max(res, (j - i + 1) * 1ll * (2 * mn - !f));
// if (j + 1 < n){
// mn = min(mn, lcp_res[j]);
// }
// }
}
}
cout << res << "\n";
}
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... |