HDU-4513 Series Stories-Perfect Formations II (manacher)

Source: Internet
Author: User

1, find a longest palindrome substring, require the middle of the value of the largest, and then to the sides of the decrement.

2. The judging condition should be changed to: ma[i+mp[i]]==ma[i-mp[i]]&&ma[i-mp[i]]<=ma[i-mp[i]+2]

3.

#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;//find the longest palindrome stringConst intmaxn=100005;intma[maxn*2];intmp[maxn*2];voidManacher (intS[],intLen) {    intL=0; Ma[l++]=-1;//flag ' $ 'ma[l++]=255;//sign ' # '     for(intI=0; i<len;i++) {ma[l++]=S[i]; Ma[l++]=255; } Ma[l]=1;//sign ' + '    intmx=0, id=0;  for(intI=0; i<l;i++) {Mp[i]=mx>i?min (mp[2*id-i],mx-i):1;  while(ma[i+mp[i]]==ma[i-mp[i]]&&ma[i-mp[i]]<=ma[i-mp[i]+2]) mp[i]++; if(i+mp[i]>MX) {MX=i+Mp[i]; ID=i; }    }}/*abaabai:0 1 2 3 4 5 6 7 8 9 Ten 13ma[i]:$ # a # b # a # a # b # a #Mp [i]:1 1 2 1 4 1 2 7 2 1 4 1 2 1*/intS[MAXN];intMain () {intT,n,i; scanf ("%d",&u);  while(t--) {scanf ("%d",&N);  for(i=0; i<n;++i) scanf ("%d",&S[i]);        Manacher (S,n); intans=0;  for(i=0;i<2*n+2;++i) ans=max (ans,mp[i]-1); printf ("%d\n", ans); }    return 0;}

HDU-4513 Series Stories-Perfect Formations II (manacher)

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.