제출 #367252

#제출 시각아이디문제언어결과실행 시간메모리
367252Bill_00회문 (APIO14_palindrome)C++14
47 / 100
109 ms131076 KiB
#include <bits/stdc++.h> #define NN 100000 #define pb push_back typedef int ll; using namespace std; long long ans; struct pal{ ll cnt; pal *a[26]; pal *b[18]; ll h; }*root=new pal(),*root1=new pal(); pal *u,*ptr[NN]; void ins(pal *rt,ll k){ if(rt->a[k]==NULL){ rt->a[k]=new pal(); for(ll i=0;i<18;i++){ (rt->a[k])->b[i]=new pal(); } (rt->a[k])->b[0]=rt; for(ll i=1;i<18;i++){ (rt->a[k])->b[i]=((rt->a[k])->b[i-1])->b[i-1]; } (rt->a[k])->h=(rt->h)+1; } u=rt->a[k]; } int solve(pal *rt,ll length,bool b){ for(ll i=0;i<26;i++){ if(rt->a[i]!=NULL){ rt->cnt+=(solve(rt->a[i],length+1,b)); } } ans=max(ans,(long long)((long long)(rt->cnt)*(long long)(b==1?(length*2-1):(length*2)))); int o=rt->cnt; delete(rt); rt=NULL; return o; } int main(){ for(ll i=0;i<18;i++){ root->b[i]=new pal(); root->b[i]=root; } string s; cin >> s; ll n=s.size(); vector<ll>d1(n); for (ll i = 0, l = 0, r = -1; i < n; i++) { ll k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1); if(r>=i){ if(d1[l+r-i]<=(r-i+1)){ u=ptr[l+r-i]; } else{ u=ptr[l+r-i]; for(ll j=17;j>=0;j--){ if(((u->b[j])->h)>=(r-i+1)){ u=u->b[j]; } } } } else ins(root,(s[i]-'a')); while (0 <= i - k && i + k < n && s[i - k] == s[i + k]){ ins(u,(s[i-k]-'a')); k++; } d1[i] = k--; ptr[i]=u; (u->cnt)++; if (i + k > r) { l = i - k; r = i + k; } } solve(root,0,1); for(ll i=0;i<18;i++){ root1->b[i]=new pal(); root1->b[i]=root1; } vector<ll>d2(n); for(ll i=0,l=0,r=-1;i<n;i++){ ll k=((i>r)?0:min(d2[l+r-i+1],r-i+1)); if(r>=i){ if(d2[l+r-i+1]<=r-i+1){ u=ptr[l+r-i+1]; } else{ u=ptr[l+r-i+1]; for(ll j=17;j>=0;j--){ if(((u->b[j])->h)>=(r-i+1)){ u=u->b[j]; } } } } else u=root1; while(0<=i-k-1 && i+k<n && s[i-k-1]==s[i+k]){ ins(u,(s[i+k]-'a')); k++; } d2[i]=k--; ptr[i]=u; (u->cnt)++; if(i+k>r){ l=i-k-1; r=i+k; } } solve(root1,0,0); cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...