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>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const int M = 1e9 + 9;
const int base = 37;
const int N = 3e5 + 2;
int n, p[N], h[N], h1[N], d[N], num = 0;
pi sa[N];
str s;
ll rs = 0;
#define geth(l, r) ((h[(r)] - 1LL * h[(l) - 1] * p[(r) - (l) + 1] % M + M) % M)
#define geth1(l, r) ((h1[(l)] - 1LL * h1[(r) + 1] * p[(r) - (l) + 1] % M + M) % M)
int lcp(const int &a, const int &b) {
int l = 1, r = min(n - a + 1, n - b + 1), ans = 0;
WHILE(l <= r) {
int mid = l + (r - l) / 2;
if (geth(a, a + mid - 1) == geth(b, b + mid - 1)) {
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
return ans;
}
bool cmp(const pi &a, const pi &b) {
int len = min({a.nd - a.st + 1, b.nd - b.st + 1, lcp(a.st, b.st)});
if (len == a.nd - a.st + 1 && len == b.nd - b.st + 1) return 0;
if (len == a.nd - a.st + 1) return 1;
if (len == b.nd - b.st + 1) return 0;
return s[a.st + len] < s[b.st + len];
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> s;
n = sz(s);
s = "#" + s + "#";
p[0] = 1;
FOR(i, 1, n) {
h[i] = (1LL * h[i - 1] * base % M + s[i] - 'a' + 1) % M;
p[i] = 1LL * p[i - 1] * base % M;
}
FOS(i, n, 1) h1[i] = (1LL * h1[i + 1] * base % M + s[i] - 'a' + 1) % M;
FOR(i, 1, n) {
int l = 1, r = min(i - 1, n - i), ans = 0;
WHILE(l <= r) {
int mid = l + (r - l) / 2;
if (geth(i - mid, i + mid) == geth1(i - mid, i + mid)) {
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
sa[i] = {i, i + ans};
}
sort(sa + 1, sa + 1 + n, cmp);
FOS(i, n, 1) {
d[i] = 1;
if (i < n && lcp(sa[i].st, sa[i + 1].st) >= sa[i].nd - sa[i].st + 1) d[i] = d[i + 1] + 1;
rs = max(rs, 1LL * (2 * (sa[i].nd - sa[i].st) + 1) * d[i]);
}
FOR(i, 1, n - 1) {
if (s[i] != s[i + 1]) continue;
int l = 1, r = min(i - 1, n - i - 1), ans = 0;
WHILE(l <= r) {
int mid = l + (r - l) / 2;
if (geth(i - mid, i + 1 + mid) == geth1(i - mid, i + 1 + mid)) {
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
sa[++num] = {i + 1, i + 1 + ans};
}
sort(sa + 1, sa + 1 + num, cmp);
FOS(i, num, 1) {
d[i] = 1;
if (i < num && lcp(sa[i].st, sa[i + 1].st) >= sa[i].nd - sa[i].st + 1) d[i] = d[i + 1] + 1;
rs = max(rs, 2LL * (sa[i].nd - sa[i].st + 1) * d[i]);
}
cout << rs;
return 0;
}
/*
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
Compilation message (stderr)
palindrome.cpp: In function 'int main()':
palindrome.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
palindrome.cpp:87:5: note: in expansion of macro 'FOR'
87 | FOR(i, 1, n) {
| ^~~
palindrome.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
| ^
palindrome.cpp:91:5: note: in expansion of macro 'FOS'
91 | FOS(i, n, 1) h1[i] = (1LL * h1[i + 1] * base % M + s[i] - 'a' + 1) % M;
| ^~~
palindrome.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
palindrome.cpp:92:5: note: in expansion of macro 'FOR'
92 | FOR(i, 1, n) {
| ^~~
palindrome.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
| ^
palindrome.cpp:105:5: note: in expansion of macro 'FOS'
105 | FOS(i, n, 1) {
| ^~~
palindrome.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
| ^
palindrome.cpp:110:5: note: in expansion of macro 'FOR'
110 | FOR(i, 1, n - 1) {
| ^~~
palindrome.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
| ^
palindrome.cpp:124:5: note: in expansion of macro 'FOS'
124 | FOS(i, num, 1) {
| ^~~
# | 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... |