Test instructions: Suppose there are n individuals in the order of height respectively h[1], h[2] ... h[n], from which some people form a new formation, the new formation if meet the following requirements, is the new perfect formation:
1. Continuous
2, the formation of palindrome string
3, from the left to the middle of the person, height should be guaranteed not to fall
Ask for the maximum number of people who formed the perfect formation
The puzzle: The Manacher algorithm's deformation.
First, let's explain the manacher algorithm: it seems to me to be an optimization of violence.
We first unify the odd and even palindrome to become an odd palindrome, which is to add some impossible numbers between two numbers.
For example: 1 2 3 3 2 5 2 3 1-> (more clearly signed) $ # 1 # 2 # 3 # 3 # 2 # 5 # 2 # 3 # 1 # & (where the number of the end-to-end # guarantee the correct answer), note the end-to-end increase & with $ to not handle the boundary 。 Then we only need to count the length of the palindrome string half (including the middle one character) and then minus one is the length of the total palindrome string here.
First use the DP array to record the palindrome string half length, ID record the maximum DP subscript, MAXID record the maximum DP can be tube to the last distance, and then use the nature of palindrome to reduce the number of times per comparison
In no more than Maxid position (more than is not sure whether the match succeeded) J (I relative to the ID of the corresponding position) can be directly assigned to I, and then I again violence to find the palindrome is not found intact. In the end, we're going to have to increase our judgment in the violence.
#include <Set>#include<map>#include<queue>#include<stack>#include<cmath>#include<vector>#include<string>#include<cstdio>#include<cstring>#include<stdlib.h>#include<iostream>#include<algorithm>using namespacestd;#defineEPS 1E-8/*Note that there may be output -0.000*/#defineSGN (x) (X<-eps -1:x<eps? 0:1)//X is a comparison of two floating-point numbers, note the return integer#defineCvs (x) (x > 0.0 x+eps:x-eps)//floating point Conversion#defineZero (x) (((x) >0? ( x):-(x)) <eps)//determine if it equals 0#defineMul (A, B) (a<<b)#defineDir (A, B) (a>>b)typedefLong Longll;typedef unsignedLong Longull;Const intinf=1<< -;Const DoublePi=acos (-1.0);Const intmod=1e9+7;Const intmax=300010;intNum[max];intInit (intN//add other characters to make odd even palindrome into odd palindrome string{ for(inti=n-1; i>=0;--i) {num[(i<<1)+2]=Num[i]; num[(i<<1)|1]=0;//characters that cannot appear} num[(n<<1)|1]=0; num[0]=1;//Bordernum[(n<<1)+2]=2; return(n<<1)+3;}intDp[max];//match length per positionintManacher (intN) { intmanx=0; intId=0, maxid=0; dp[0]=1; for(intI=1; i<n;++i) {if(maxid>=i) dp[i]=min (dp[(id<<1)-i],maxid-i+1); ElseDp[i]=1; while(num[i+dp[i]]==num[i-dp[i]]&& (num[i+dp[i]]==0|| num[i+dp[i]]<=num[i+dp[i]-2]))//notice there needs to be modifieddp[i]++; if(maxid<i+dp[i]-1) {Maxid=i+dp[i]-1; ID=i; } if(manx<dp[i]-1) Manx=dp[i]-1; } returnManx;}intMain () {intT,n; scanf ("%d",&t); while(t--) {scanf ("%d",&N); for(intI=0; i<n;++i) scanf ("%d",&Num[i]); N=Init (n); printf ("%d\n", Manacher (n)); } return 0;}
HDU 4513 Story-Perfect Formation II (Manacher variant)