Bzoj 2565 longest Double palindrome string hash+ two points

Source: Internet
Author: User

Topic: Given a string, to find the longest substring, the string can be decomposed into two palindrome substrings

Stupid, I forgot how manacher wrote = = Helpless hash+ two points

First multiply the string with a delimiter and then find the longest palindrome radius centered at each point

Then consider how two palindrome strings merge into one


We find that the j-i of the I-centered palindrome and the J-centric palindrome combine to the length of

The prerequisite for merging is the intersection of a palindrome with a two-point center.

So for each J we ask for the leftmost I with the intersection

Maintain a suffix min just screw it up.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 200200# Define BASE 131using namespace Std;typedef unsigned long long ll;int n,ans;int F[m],min_pos[m];char s[m];ll hash1[m],hash2 [M],power[m];bool Judge (int mid,int len) {ll _hash1=hash1[mid+len-1]-hash1[mid-1]*power[len];ll _hash2=hash2[mid-len +1]-hash2[mid+1]*power[len];return _hash1==_hash2;} int bisecition (int x) {int l=1,r=min (x,n-x+1), while (l+1<r) {int mid=l+r>>1;if (Judge (x,mid)) L=mid;elser=mid;} Return Judge (x,r)? R:l;} int main () {int i;static char str[m];scanf ("%s", str+1); for (s[1]= ' # ', i=1;str[i];i++) s[i<<1]=str[i],s[i<< 1|1]= ' # '; N=strlen (s+1); for (power[0]=1,i=1;i<=n;i++) power[i]=power[i-1]*base;for (i=1;i<=n;i++) hash1[i]= Hash1[i-1]*base+s[i];for (i=n;i;i--) hash2[i]=hash2[i+1]*base+s[i];memset (min_pos,0x3f,sizeof min_pos); for (i=1;i <=n;i++) {f[i]=bisecition (i); Min_pos[i+f[i]-1]=min (min_pos[i+f[i]-1],i);} for (i=n-1;i;i--) min_pos[i]=min (min_pos[i],min_pos[I+1]); for (i=1;i<=n;i++) {int Temp=min_pos[i-f[i]+1];ans=max (ans,i-temp<<1);} cout<< (ans>>1) <<endl;return 0;}


Bzoj 2565 longest Double palindrome string hash+ two points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.