HDU 4513 Series Story--Perfect formation ii_ string

Source: Internet
Author: User
Tags first row

A series of stories about Jill--Perfect Formation II time limit:3000/1000 MS (java/others) Memory limit:65535/32768 K (java/others)
Total submission (s): 4024 accepted Submission (s): 1602


Problem Description Gigo again came up with a new perfect formation game.
Suppose there are n individuals standing in front of him in order, their height is h[1], h[2] ... h[n], Gigo hope to pick out some people, let these people form a new formation, if the new formation meet the following three requirements, it is the new perfect formation:

1, the selected person to maintain the original formation of the relative order unchanged, and must be in the original formation of continuous;
2, left-right symmetry, with the formation of M-personal form of new formations, then the 1th person and the height of the same m, the 2nd person and the m-1 personal height of the same, and so on, of course, if M is odd, the middle of the person can be arbitrary;
3, from the left to the middle of that person, height should be guaranteed not to decline, if using H to indicate the height of the new formation, then h[1] <= h[2] <= h[3] ... <= h[mid].

Now Gigo wants to know how many people can be chosen to form a new perfect formation.
Input input data The first row contains an integer t, which indicates a total of T-Group test data (T <= 20);
Each group of data first is an integer n (1 <= n <= 100000), indicating the number of the original formation, the next line input n integers, indicating the original formation from left to right station of the height of people (<= H <= 250, do not exclude particularly short and tall).
Output can be composed of the largest number of perfect formation, each set of output occupies one row.
Sample Input

2 3 51 52 51 4 51 52 52 51
Sample Output
3 4
Source 2013 Tencent Programming Marathon second (March 22) can be seen from the request is a palindrome a string of numbers, can still use the manacher algorithm, in the calculation of the P[i] array, if the new array is not not a position of the number (2), Then we have to judge it with the front.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=1E5+10;
int NUM[MAXN];
int num_new[maxn<<1];
int p[maxn<<1];
int n;
int init () {
	num_new[0]=-1;
	num_new[1]=-2;
	int j=2;
	for (int i=0;i<n;i++) {
		num_new[j++]=num[i];
		num_new[j++]=-2;
	}
	num_new[j]=-2;
	return j;
}
int Manacher () {
	int len=init ();
	int id;
	int maxx=0;
	int max_len=-1;
	for (int i=1;i<len;i++) {
		if (I<maxx) p[i]=min (p[2*id-i],maxx-i);
		else p[i]=1;
		while (Num_new[i+p[i]]==num_new[i-p[i]]) {
			if (num_new[i+p[i]]!=-2) {
				if (num_new[i+p[i]]<=num_new[i+p [i]-2]) p[i]++;
				else break;
			}
			p[i]++;
		}
		if (Maxx<i+p[i]) {
			maxx=i+p[i];
			id=i;
		}
		Max_len=max (max_len,p[i]-1);
	}
	return max_len;
}
int main () {
	int test;
	scanf ("%d", &test);
	while (test--) {
		scanf ("%d", &n);
		for (int i=0;i<n;i++) {
			scanf ("%d", &num[i]);
		printf ("%d\n", Manacher ());
	}
	return 0;
}

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.