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)