This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In The Name Of God
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define sqr(A) ((A) * (A))
#define X first
#define Y second
#define MP make_pair
#define bsz __builtin_popcount
#define all(A) A.begin(), A.end()
using namespace std;
using namespace __gnu_pbds;
template < typename T >
using ordered_set = tree < T , null_type , less < T > , rb_tree_tag , tree_order_statistics_node_update >;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int MOD2 = 1e9 + 9;
const int PR = 727;
const int PR2 = 23;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const int N = 1e4 + 20;
int hsh[N], hsh2[N], pw[N], pw2[N], n;
map<pii, ll> cnt, len;
string s;
ll ans;
int getHash(int l, int r) {
if (l == 0) return hsh[r];
return hsh[r] - hsh[l - 1] * pw[r - l + 1];
}
int getHash2(int l, int r) {
if (l == 0) return hsh2[r];
return hsh2[r] - hsh2[l - 1] * pw2[r - l + 1];
}
bool isPal(int l, int r) {
if (r - l == 0) return 1;
int t = r - l + 1;
if (t % 2)
return (getHash(l, l + t / 2 - 1) == getHash(l + t / 2 + 1, r)) & (getHash2(l, l + t / 2 - 1) == getHash2(l + t / 2 + 1, r));
else
return (getHash(l, l + t / 2 - 1) == getHash(l + t / 2, r)) & (getHash2(l, l + t / 2 - 1) == getHash2(l + t / 2, r));
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> s; n = s.size();
pw[0] = 1; pw2[0] = 1;
hsh[0] = s[0]; hsh2[0] = s[0];
for (int i = 1; i < n; i++) {
pw[i] = pw[i - 1] * PR;
hsh[i] = PR * hsh[i - 1] + s[i];
pw2[i] = pw2[i - 1] * PR2;
hsh2[i] = PR2 * hsh2[i - 1] + s[i];
}
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++)
if (isPal(i, j)) {
int tmp = getHash(i, j);
int tmp2 = getHash2(i, j);
cnt[MP(tmp, tmp2)]++;
len[MP(tmp, tmp2)] = j - i + 1;
}
for (auto x : cnt)
ans = max(ans, x.Y * len[x.X]);
cout << ans << endl;
}
# | 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... |