28分求助

P4555 [国家集训队] 最长双回文串

%%%
by _saltFish_ @ 2023-02-15 13:01:30


```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5+5; string pre(string s){ if(s.size()==0) return "^$"; string ret="^"; for(int i=0;i<s.size();i++){ret+='#';ret+=s[i];} return (ret+="#"); } int p[N],L[N],R[N]; int manacher(string str){ int n=str.size()-1,c=0,r=0,ans=0; for(int i=1;i<=n;i++){ int j=(c<<1)-i; if(r>=i) p[i]=min(r-i,p[j]); else {p[i]=1;} while(str[i+p[i]]==str[i-p[i]]) p[i]++; if(i+p[i]>r){ c=i; r=i+p[i]; } L[i+p[i]-1]=max(L[i+p[i]-1],p[i]-1); R[i-p[i]+1]=max(R[i-p[i]+1],p[i]-1); } for(int i=1;i<=n;i+=2) R[i]=max(R[i],R[i-2]-2); for(int i=n;i>=1;i-=2) L[i]=max(L[i],L[i+2]-2); for(int i=1;i<=n;i+=2){ if(R[i]&&L[i]) ans=max(ans,L[i]+R[i]); } return ans; } string str; signed main(){ ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>str; cout<<manacher(pre(str)); return 0; } ```
by _saltFish_ @ 2023-02-15 13:08:59


@[xiezheyuan](/user/413065)
by _saltFish_ @ 2023-02-15 13:09:15


|