# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1115117 | VinhLuu | Palindromes (APIO14_palindrome) | C++17 | 1073 ms | 90024 KiB |
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
using namespace std;
const int N = 2e5 + 5;
int n, a[N];
namespace sub1{
ll ans;
void solve(){
for(int i = 1; i <= n; i ++){
stack<int> st;
for(int j = i; j <= n; j ++){
bool gg = true;
int mid = (i + j) / 2;
if((j - i + 1) & 1){
int pl = mid - 1, pr = mid + 1;
while(pl >= i && pr <= j){
if(a[pl] != a[pr]){
gg = false;
break;
}
pl--;
pr++;
}
}else{
int pl = mid, pr = mid + 1;
while(pl >= i && pr <= j){
if(a[pl] != a[pr]){
gg = false;
break;
}
pl--;
pr++;
}
}
if(gg){
int cnt = 0;
for(int k = 1; k <= n - (j - i + 1) + 1; k ++){
bool ff = true;
for(int h = k; h <= k + (j - i + 1) - 1; h ++){
if(a[h] != a[i + h - k]){
ff = false;
break;
}
}
if(ff) cnt++;
}
ans = max(ans, 1ll * cnt * (j - i + 1));
}
}
}
cout << ans;
}
}
namespace sub2{
#define ull unsigned long long
ull lt[N], f[N], g[N], base = 137ull;
ull get(int l,int r){
return f[r] - 1ull * f[l] * lt[r - l + 1];
}
ull rev(int l,int r){
return g[l] - 1ull * g[r + 1] * lt[r - l + 1];
}
map<ull,int> mp;
ll ans;
void solve(){
lt[0] = 1;
for(int i = 1; i <= n; i ++){
lt[i] = 1ull * lt[i - 1] * base;
f[i] = 1ull * f[i - 1] * base + a[i];
}
for(int i = n; i >= 1; i --) g[i] = 1ull * g[i + 1] * base + a[i];
for(int i = 1; i <= n; i ++){
int pl = i, pr = i;
while(pl >= 1 && pr <= n){
if(a[pl] != a[pr]) break;
int val = mp[get(pl, pr)] + 1;
mp[get(pl, pr)]++;
ans = max(ans, 1ll * val * (pr - pl + 1));
pl--;
pr++;
}
pl = i, pr = i + 1;
while(pl >= 1 && pr <= n){
if(a[pl] != a[pr]) break;
int val = mp[get(pl, pr)] + 1;
mp[get(pl, pr)]++;
ans = max(ans, 1ll * val * (pr - pl + 1));
pl--;
pr++;
}
}
cout << ans;
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "palindrome"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
string s; cin >> s;
n = s.size();
s = " " + s;
for(int i = 1; i <= n; i ++) a[i] = (int)(s[i] - 'a' + 1);
sub2 :: solve();
}
Compilation message (stderr)
# | 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... |