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>
#ifdef DEBUG
#include "../templates/debug.h"
#else
#define deb(x...)
#endif
using namespace std;
const int C = 26;
struct node{
int child[C];
int cnt = 0;
int dist = 0;
};
node root1[(int)3e5], root2[(int)3e5];
int r1 = 1, r2 = 1;
void make_node_r1(int id, int d){
for(int i = 0;i<C;i++){
root1[id].child[i] = 0;
}
root1[id].cnt = 0;
root1[id].dist = d;
}
void make_node_r2(int id, int d){
for(int i = 0;i<C;i++){
root2[id].child[i] = 0;
}
root2[id].cnt = 0;
root2[id].dist = d;
}
string s;
int ans = 1;
auto is_pali(int l,int r){
if(l > r)return true;
if(s[l] != s[r])return false;
return is_pali(l + 1, r - 1);
}
void add1(int l,int r){
int val = 1;
for(int i = l;i<=r;i++){
int c = s[i] - 'a';
if(!root1[val].child[c]){
root1[val].child[c] = ++r1;
make_node_r1(root1[val].child[c], root1[val].dist + 2);
}
val= root1[val].child[c];
++root1[val].cnt;
ans = max(ans, root1[val].cnt*root1[val].dist);
}
}
void add2(int l,int r){
int val = 1;
for(int i = l;i<=r;i++){
int c = s[i] - 'a';
if(!root2[val].child[c]){
root2[val].child[c] = ++r2;
make_node_r2(root2[val].child[c], root2[val].dist + 2);
}
val= root2[val].child[c];
++root2[val].cnt;
ans = max(ans, root2[val].cnt*root2[val].dist);
}
}
signed main(){
make_node_r1(1,-1);
make_node_r2(1,0);
cin >> s;
int n = s.size();
vector<int> leq(n,0);
vector<int> req(n,0);
for(int i = 1;i<n;i++){
if(s[i] == s[i - 1])leq[i] = 1 + leq[i - 1];
}
for(int i = n - 2;i>=0;i--){
if(s[i] == s[i + 1])req[i] = 1 + req[i + 1];
}
int SP = 0;
for(int i = 0;i<n;i++){
auto check = [&](int mid)->bool{
if(i - mid < 0)return false;
if(i + mid > n - 1)return false;
return is_pali(i - mid, i + mid);
};
int l = min(leq[i], req[i]) + 1,r = n - 1;
if(l <= r && check(l)){
while(l<r){
int mid = (l + r + 1)/2;
if(check(mid)){
l = mid;
}
else
r = mid - 1;
}
SP += (l + 1);
// add1(i,i + l);
}
if(i > 0 && s[i] == s[i - 1]){
l = min(leq[i - 1], req[i]) + 1,r = n - 1;
if(l <= r && i - 1 - l >=0 && i + l < n && is_pali(i - 1 - l,i + l)){
while(l < r){
int mid = (l + r + 1)/2;
if(i - 1 - mid >= 0 && i + mid < n && is_pali(i - 1 - mid, i + mid)){
l = mid;
}
else
r = mid - 1;
}
SP += (l + 1);
// add2(i,i + l);
}
}
}
// cout << SP << "\n";
// deb(ans);
// vector<vector<int>> temp(26);
// for(int i = 0;i<n;i++){
// temp[s[i] - 'a'].push_back(req[i] + 1);
// }
// for(int j = 0;j<26;j++){
// vector<int> cc(n + 2, 0);
// for(auto e : temp[j]){
// cc[e + 1]--;
// cc[0]++;
// }
// for(int i = 1;i<=n + 1;i++)cc[i] += cc[i - 1];
// for(int i = 0;i<=n;i++){
// ans = max(ans, cc[i]*i);
// }
// }
// cout << ans << "\n";
}
# | 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... |