HDU ACM 4513 Series Story--Perfect formation ii-> find the longest palindrome (Manacher algorithm)

Source: Internet
Author: User

Analysis: The problem can be solved by the method of finding the longest palindrome string, the longest palindrome string using the Manacher algorithm, O (n) time complexity.
Note: while (A[i-len[i]]==a[i+len[i]] && a[i-len[i]]<=a[i-len[i]+2]) Here are more judgments a[i-len[i]]<=a[i-len[i]+2] That is, the limit of the problem from left to right to ensure that height does not drop, because the palindrome string in the calculation of the addition of additional characters, so here is i-len[i]+2 instead of i-len[i]+1, to avoid the added characters.
#include <iostream>using namespace std; #define N 100010int len[n<<1];int a[n<<1];int manacher (int N) { int I,ans,mx,po;ans=po=mx=0;for (i=1;i<= (n<<1) +2;i++) {if (mx>i) len[i]=mx-i<len[(po<<1)-i]? mx-i:len[(po<<1)-i];elselen[i]=1;while (A[i-len[i]]==a[i+len[i]] && a[i-len[i]]<=a[i-len[i]+2])   //Because there are padding characters ' # ', so be +2. Len[i]++;if (I+LEN[I]>MX) {mx=i+len[i];p o=i;} Ans=ans>len[i]?ans:len[i];} return ans-1;} int main () {int t,n,i;scanf ("%d", &t), while (t--) {scanf ("%d", &n); A[0]=-100;for (i=1;i<=n<<1;i+=2) {A [i]=-200;              Equivalent to ' # ' scanf ("%d", &a[i+1]);} a[i]=-200;                 Equivalent to ' # ' a[i+1]=-300;printf ("%d\n", Manacher (n));} return 0;}


HDU ACM 4513 Series Story--Perfect formation ii-> find the longest palindrome (Manacher algorithm)

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.